I have two content types: cars and mileage. Car nodes have an integer field (field_mpg) and mileage nodes have an integer field (field_miles) and a node reference (field_car) . Mileage also has a computed field (field_gas_used).

I would like to use a Computed Code similar to:

    $gas_used = $entity->field_mpg[LANGUAGE_NONE][0]['value']) * $entity->field_miles[LANGUAGE_NONE][0]['value']);
    $entity_field[0]['value'] = $gas_used;

This give the error:

Notice: Undefined property: stdClass::$field_mpg in eval() (line 15 of /var/www/html/drupal-7.2/sites/all/modules/computed_field/computed_field.module(439) : eval()'d code).

It kind of makes sense because $field_mpg is not defined for mileage nodes.

Is there a work around?

Comments

waverate’s picture

Status: Active » Fixed

Solution is #189976: Node Object from node reference field?.

Example below updated for D7.

$car_id = $entity->field_car[LANGUAGE_NONE][0]['nid'];
$car_node = node_load($car_id, NULL, TRUE);

$gas_used = $car_node->field_mpg[LANGUAGE_NONE][0]['value']) * $entity->field_miles[LANGUAGE_NONE][0]['value']);
$entity_field[0]['value'] = $gas_used;

Status: Fixed » Closed (fixed)

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

TTNT’s picture

Version: 7.x-1.x-dev » 7.x-1.0
Issue summary: View changes
Status: Closed (fixed) » Needs review

I can't get this to work in D7. Is there anyone who did? Or maybe someone has another idea as to how we can use fields within referenced content? Thanks!