3

I am using Drupal 7. I installed the computed field module and have a computed field in a content type. I need to be able to access the actual name of a node reference field that is in the same content type so i can then work with the data in the computed field. Currently I can only get the number of the node reference field using this in the computed code (php) field:

    $entity_field[0]['value'] = array_pop(field_get_items($entity_type, $entity, 'field_charity_reference'));

I need to be able to access a 'drill' into the the node reference to get the rendered title, which is a human readable name.

When using views and custom php I can use dpm($data); to explore the data and use this to work out I can access the rendered title by using:

    $data->field_field_charity_reference[0]['rendered']['#title']

Since originally posting this question I have found i can use dpm(field_get_items($entity_type, $entity, 'field_charity_reference')); in the 'Display Code (PHP)' input in the computed field to give a devel printout of the field I wish to access data from.

I would like to know how to drill into the results returned by field_get_items($entity_type, $entity, 'field_charity_reference') in order to output the name of the node reference field. Thanks

2

Procedurally:

if ($field_items = field_get_items('node', $node, 'field_charity_reference')) {
  $referenced_node = node_load($field_items[0]['target_id']);
  $title = $referenced_node->title;
}

Or OO'ly with the Entity API module:

$node_wrapper = entity_metadata_wrapper('node', $node);
$title = $node_wrapper->field_charity_reference->value()->title;

If you go the OO route this post will probably come in handy

  • Thanks very much for this Clive. Just before I saw this, I also came upon my own way of achieving the results i need, which I will write up as a separate answer. – Paul Trotter Jul 16 '13 at 15:25
  • The procedural answer gave me Notice: Undefined variable: node in eval() (line 1 .... computed_field.module(344) : eval()'d code). and EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7663 ....common.inc). I think that this could be because we are using node not entity? – Paul Trotter Jul 16 '13 at 15:36
  • The OO answer gave me a similar first error message and then EntityMetadataWrapperException: Unknown data property field_charity_reference. in EntityStructureWrapper->getPropertyInfo() (line 339 of ../entity.wrapper.inc). again I suspect that this code might work by using node not entity. Since we have a working solution to this problem already I won't commit time to getting this code to work, but thanks Clive for this contribution. – Paul Trotter Jul 16 '13 at 15:38
  • @Paul Yeah you won't be able to use that verbatim, it'll need to be changed to match your exact situation. In your case, replace 'node' with $entity_type and $node with $entity in both cases and they'll work – Clive Jul 16 '13 at 15:41
  • Thanks Clive. I've realized that my answer below works fine for displaying the computed value. But fails when I try to use it in the Computed Code (PHP) field so that the value is stored and can be used by views. Could you suggest whether your code is likely to work in Computed Code (PHP) field? – Paul Trotter Jul 16 '13 at 15:55
0

If you only wish to display the data you can add the following code to the Display Code (PHP) field of the computed value:

     $getdata = field_get_items($entity_type, $entity, 'field_charity_reference');
     $drilldata = $getdata[0]['node'];

     $display_output = $drilldata->title;

It is worth noting that I got unexpected results / errors when trying to work in the Computed Code (PHP) field of the computed value. It seems that above code will not actually store your data, so it can be used by views etc. I suspect to actually store the values you need to use the Computed Code (PHP) field.

Please also note that it did trying to use $drilldata = $getdata[0]['node']['title']; gave the warning 'Fatal error: Cannot use object of type stdClass as array'. Only drilling as far as the node and then getting the title in the last line worked for me.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.