Not sure if this is a bug or not, but since I cannot find any other explanation I will assume it is. I am using a function in my module to compute the value of a computed field (See following function). I have confirmed that the function is being run when the node is updated, and the correct output is being generated by the function. The only problem is that the generated data ($output) is not being written to DB table.
Any ideas?
function computed_field_namecards_namecard_full_content_compute($entity, $entity_type, $entity_lang, &$entity_field, $langcode) {
// dsm($entity_lang);
// dsm($entity_field);
$output = '';
$lang = $entity_lang->language;
$node_ref_nids = array();
$fields = array(
'namecards_namecard_given_name',
'namecards_namecard_nickname',
'namecards_namecard_organization',
'namecards_namecard_department',
'namecards_namecard_event',
'namecards_namecard_position',
'namecards_namecard_phone',
'namecards_namecard_fax',
'namecards_namecard_mobile',
'namecards_namecard_email',
'namecards_namecard_address',
);
// Add surname.
$output .= check_plain($entity_lang->title) . ' ';
// Add remaining fields.
foreach ($fields as $field_name) {
$count = count($entity_lang->{$field_name}[$lang]);
for ($i = 0; $i < $count; $i++) {
if ($field_name != 'namecards_namecard_address') {
// Get value of all fields except address, as address
// has a different structure and is thus handled separately.
foreach ($entity_lang->{$field_name}[$lang][$i] as $key => $value) {
if ($key == 'value') {
$output .= check_plain($value) . ' ';
}
elseif ($key == 'nid' && preg_match('/^[0-9]{1,}$/', $value) > 0) {
// Collect nid of referenced node.
$node_ref_nids[] = (int) $value;
}
}
}
else {
// Get values of address field.
foreach ($entity_lang->namecards_namecard_address[$lang][$i] as $value) {
$output .= check_plain($value) . ' ';
}
}
}
}
// Get the values (i.e. titles) of all referenced nodes.
if (count($node_ref_nids) > 0) {
$results = db_query('SELECT title FROM {node} WHERE nid IN (:node_ref_nids)', array(':node_ref_nids' => $node_ref_nids));
foreach ($results as $result) {
$output .= check_plain($result->title) . ' ';
}
}
dsm($output);
// Set value of computed field.
$entity_field[0]['value'] = $output;
}
Comments
Comment #1
begun commentedTurns out that the problem was with the ordering of the arguments in the function call. Correct order is as below.
It is not clear in the field description on the form that these arguments must be included in computed_field_XXXXX_compute() for the function to work.
Have attached a patch which changes the wording of:
"Alternately, this code can be supplied by your own custom function named: computed_field_XXXXX_compute()"
into:
"Alternately, this code can be supplied by your own custom function named: computed_field_XXXXX_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items)"
Comment #2
giorgio79 commentedWorks for me, thanks
Comment #3
kaizerking commentedis this patch committed?
Comment #4
drewish commentedI simplified Begun's patch to remove all the whitespace changes that obscure that this is a very simple fix.
Comment #5
drewish commentedI didn't properly attribute the authorship in that last patch.
Comment #6
alexiswatson commentedReviewed and tested. +1 to RTBC.
Comment #7
colanCommitted in 8bc34b2.
Comment #8.0
(not verified) commentedNot sure whether this should be called a bug or not. Put in explanation as to why I call it a bug.