Collections functions
General functions
Node functions
Render functions
Theme permission functions
User functions
Resource functions

migrate_fixed_to_text()

Description

Migrate fixed list field data to text field data for a given resource reference. Useful when changing resource type field from a data type
that can contain multiple values such as a dynamic keywords field. This script will concatenate the existing values and leave one remaining
node for the new text field.

- additional checks maybe need before calling this to ensure the fields are / were of the expected type.
see examples in pages/tools/migrate_fixed_to_text.php
of category tree branches to be preserved e.g. "level1/value, level2/value"

Parameters

ColumnTypeDefaultDescription
$resource_type_field mixed Resource type field id. ** The field type should have been changed to a text type in advance
$resource mixed Resource reference to be processed.
$category_tree mixed Was the field data being migrated previously of type category tree? Specifying true will allow the format
$separator mixed ' Default is comma and space e.g. "value1, value2"
: ' get_resource_nodes$resource
true
# SORT_ASC;

Return

bool True on success else false.

Location

include/node_functions.php lines 2781 to 2806

Definition

 
function migrate_fixed_to_text(int $resource_type_fieldint $resourcebool $category_treestring $separator ', ') : bool
    
{
    
$current_nodes get_resource_nodes($resource$resource_type_fieldtrueSORT_ASC); # Ordering will be as displayed on the view page.

    
if (count($current_nodes) < 2)
        {
        return 
true# No need to make changes as no node / only one node present.
        
}

    if (
$category_tree)
        {
        
$all_treenodes get_cattree_nodes_ordered($resource_type_field$resourcefalse);
        
$treenodenames get_cattree_node_strings($all_treenodestrue);
        
$new_value implode($separator$treenodenames);
        }
    else
        {
        
$current_nodes_names array_column($current_nodes'name');
        
$current_nodes_translated array_map("i18n_get_translated"$current_nodes_names);
        
$new_value implode($separator$current_nodes_translated);
        }

    
delete_resource_nodes($resourcearray_column($current_nodes'ref'), false);
    
$savenode set_node(null$resource_type_field$new_valuenull0);
    return 
add_resource_nodes($resource, [$savenode], truefalse);
    }

This article was last updated 24th April 2024 21:35 Europe/London time based on the source file dated 15th April 2024 11:30 Europe/London time.