Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
I'm trying to add a computed field to a Field collection where some data would come from the host entity. Something like:
// Data from the field collection: it works!
$value_from_collection = $entity->field_value_1[LANGUAGE_NONE][0]['value'];
// Data from the host node: I don't know what to put here
$value_from_node = $host->field_value_2[LANGUAGE_NONE][0]['value'];
$output = ($value_from_collection + $value_from_node);
I've been fiddling with entity_load(), entity_metadata_wrapper(), to no avail: I'm no php guru.
Any idea?
Comments
Comment #1
Tharna CreditAttribution: Tharna commentedYou can access host using $entity->hostEntity() method.
Comment #2
yul63 CreditAttribution: yul63 commentedThank you very much, it works.
For other php semi-litterates like me, the function that encloses the values is not needed in the context of a computed field in a Field collection. So
will get the value of the field in the host entity.
Side note out of topic: if there is no value in a non mandatory field queried by the code, the computed field will start spewing error messages even though it might work.
will return an empty string if there's nothing in the field.
Comment #3
jmuzz CreditAttribution: jmuzz commented