0

I am using Computed Field to get some values;

I have a Template node that holds an Entity Reference multivalues field to a series of other nodes, all referenced nodes of the same type. The referenced nodes have a text field which value I need to get.

Until now I've managed to get the "target_id" of the referenced nodes, but I can't manage to get the values:

$tpl = field_get_items($entity_type, $entity, 'field_plan_tpl_select_trainings');
$nids = array();

foreach ($tpl as $field_item) {
            $nids[] = $field_item['target_id'];
}

What is the next step in order to get the values of a specific field for all the referenced nodes? I mention that the field has only one value.

2
  • Why don't you use Views?
    – Elin Y.
    Commented Jul 2, 2014 at 15:51
  • Because he needs to give those values to a the field he is computing, I understood give me some minutes and i will give you the answer
    – svelandiag
    Commented Jul 2, 2014 at 17:06

1 Answer 1

0

the "target_id" is the nid (node id) of the referenced entity, now if you want to get the values of a specific field of the referenced entities you can do the following:

$values = array();

foreach($tpl as $field_item){
$values[] = node_load($field_item['target_id'])->field_your_field;
}

you just need to load the entity and then get the field you are looking for. Devel module will make your life easier in that purpose

If the entity you are referencing is not a "node" so you can load the entity using entity_load() and then use entity_metadata_wrapper() for get the fields and all the info you need from each entity.

6
  • it seems like it's getting the values (I see that using devel), but when I try to write the first value of the array in the database ($entity_field[0]['value'] = $values[0];), I'm getting a notice and the value "Array" is writen: "Notice: Array to string conversion in execute()"
    – GHG
    Commented Jul 3, 2014 at 6:50
  • 1
    I figured out; I added "[LANGUAGE_NONE][0]['value']" after field name and now I get exactly the value of the field: "field_your_field[LANGUAGE_NONE][0]['value'];"
    – GHG
    Commented Jul 3, 2014 at 7:33
  • Ho that's great, good to know. you can use entity_metadata_wrapper(); it will make your life easier working with entity properties...
    – svelandiag
    Commented Jul 3, 2014 at 17:17
  • And how can I use entity_metadata_wrapper() in this case: using entity_load() or node_load()? Write an example so I can understand. Thanks
    – GHG
    Commented Jul 4, 2014 at 10:50
  • open another question and I will answer it, post the link of the question here. Greetings
    – svelandiag
    Commented Jul 4, 2014 at 13:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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