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

begun’s picture

Priority: Critical » Normal
Status: Active » Needs review
StatusFileSize
new4.75 KB

Turns out that the problem was with the ordering of the arguments in the function call. Correct order is as below.

function computed_field_namecards_namecard_full_content_compute(&$entity_field, $entity_type, $entity, $field, $instance, $langcode, $items) {
  // Set value of computed field.
  $entity_field[0]['value'] = ''; 
}

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)"

giorgio79’s picture

Status: Needs review » Reviewed & tested by the community

Works for me, thanks

kaizerking’s picture

is this patch committed?

drewish’s picture

Component: Code » Documentation
StatusFileSize
new2.03 KB

I simplified Begun's patch to remove all the whitespace changes that obscure that this is a very simple fix.

drewish’s picture

StatusFileSize
new2.02 KB

I didn't properly attribute the authorship in that last patch.

alexiswatson’s picture

Reviewed and tested. +1 to RTBC.

colan’s picture

Status: Reviewed & tested by the community » Fixed

Committed in 8bc34b2.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Issue summary: View changes

Not sure whether this should be called a bug or not. Put in explanation as to why I call it a bug.