Advertising sustains the DA. Ads are hidden for members. Join today

Computed Field

Merge mutiple fields into one

Last updated on
30 April 2025

You are browsing documentation for an older version of Drupal, which is not supported any longer, and the information may not be correct. See current documentation for Contributed modules

I have 2 option cck fields, one is a predefined list, the other is a text field. Both are set to unlimited number of values. I first tried to use array_merge, since it would be a more "elegant" solution; but I couldn't make it fly. This was my first attempt.

// set temp variable
$count = 0;

// put array into computed field array pt1
foreach (element_children($node->field_option) as $key) {
	$node_field[$count]['value'] = $node->field_option[$key]['value'];
	$count++;
}

// put array into computed field array pt2
foreach (element_children($node->field_option_list) as $key) {
	$node_field[$count]['value'] = $node->field_option_list[$key]['value'];
	$count++;
}



To make this into a comma separated list, here is the code. This works with "Store using the database", while the top one doesn't for some reason. This is what I'm using now

// set temp variable
$temp = array();

// put array into computed field array pt1
foreach (element_children($node->field_option) as $key) {
	$temp[] = $node->field_option[$key]['value'];
}

// put array into computed field array pt2
foreach (element_children($node->field_option_list) as $key) {
	$temp[] = $node->field_option_list[$key]['value'];
}

//kill empty values in the array
foreach ($temp as $key => $value) {
	if (is_null($value) || $value=="") {
		unset($temp[$key]);
	}
} 

//implode to comma list
$node_field[0]['value'] = implode(', ', $temp);

Help improve this page

Page status: Not set

You can: