Hi all

I've been having problems (Undefined property in computed_field_tokens) with using tokens to access computed fields and in preparing to submit this bug report it turned out that the issue only occurs when the computed field is part of a field collection (so apologies if the bug is actually in Field Collection).

In any case, in order to try and figure out where the problem comes from, I created a new site with Drupal 7.18 and enabled the Computed Filed 7.x-1.0-beta1, Token 7.x-1.4 and Devel 7.x-1.3 modules.

I then create a new content type with an integer field, create a new content of this type, use Devel to inspect its tokens and there is no error.

When I add a computed field to the content type and inspect the tokens again, I see the token for the new computed field, and there is no error.

I then enabled Field Collection 7.x-1.0-beta4 (which required Entity API 7.x-1.0-rc3), created a new field collection "items" with fields "field_items_integer" and "field_items_computed", and then if I use Devel to inspect its tokens the following errors occur and I cannot use the computed value:

Notice: Undefined property: stdClass::$field_items_computed in computed_field_tokens() (line 425 of /srv/www/gc/htdocs/sites/all/modules/computed_field/computed_field.module).
Notice: Undefined property: stdClass::$field_items_computed in computed_field_tokens() (line 440 of /srv/www/gc/htdocs/sites/all/modules/computed_field/computed_field.module).

(twice)

Can this be fixed in Computed Field or should I report it to Field Collection?

Thanks in advance.
Cheers,
Miguel

Comments

thebruce’s picture

Hi Miguel,

They removed the token implementation in the dev branch, if you can't or won't switch out i have a fix. I can't create a patch since it has been vanquished from the repo, but this should do the trick:

// ~from ln 421 down to end of function
foreach ($tokens as $name => $original) {
    // For normal display output
    if (in_array($name, $computed_fields)) {
      $field_name = str_replace('-', '_', $name);
      $property = $field_name . "[$lang][0]";
      if (property_exists($data[$type], $property)) {
        $entity_field_item = $data[$type]->{$field_name}[$lang][0];
        $field = $fields[$field_name];
        // Check if the value is to be formatted by a display function outside the DB
        $display_func = 'computed_field_' . $field_name . '_display';
        if (function_exists($display_func))  {
          $display_output = $display_func($field, $entity_field_item);
        }
        else {
          eval($field['settings']['display_format']);
        }
        $replacements[$original] = $sanitize ? check_plain($display_output) : $display_output;
      }
    }
    // For raw value output
    elseif (in_array(str_replace(':rawvalue', '', $name), $computed_fields)) {
      $field_name = str_replace('-', '_', str_replace(':rawvalue', '', $name));
      $property = $field_name . "[$lang][0]";
      if (property_exists($data[$type], $property)) {
        $replacements[$original] = $sanitize ? check_plain($data[$type]->{$field_name}[$lang][0]['value']) : $data[$type]->{$field_name}[$lang][0]['value'];
      }
    }
  }
  return $replacements;
}

I just check to see if the property exists on the entity in both places, if the property isn't there, we can be reasonably sure that the field hasn't been attached and so at least at this point no values can be obtained.

I hope this helps, or as the maintainers have suggested field tokens in the latest dev branch gets you what you need.

Thanks
thebruce