2

I have this code to get entity meta wrapper and print text fields. But I can't see how to print entity reference values.

$wrapper = entity_metadata_wrapper('my_entity', $entity);
print $wrapper->field_my_field->value(); 
3
+50

You can access entity reference values by calling entity metadata wrapper API on the reference field.

For instance, if you have a node with a field which references a profile2 entity,

$wrapper = entity_metadata_wrapper('node', node_load(1));
$profile_wrapper = entity_metadata_wrapper('profile2', $wrapper->field_profile->value());
print $profile_wrapper->field_about->value();

or if you prefer rendering the entity,

print render($profile_wrapper->view());
| improve this answer | |
1

You can access entity reference values without creating a new wrapper. Entity metadata wrappers can be chained easily.

If a field is a reference field or a taxonomy term etc, the value will be the ID of the referenced entity. If you want to get the title/name of the entity, you can use the label() method.

A sample code:

$wrapper = entity_metadata_wrapper('my_entity', $entity_id);
// To print the title/name of the referenced entity
print $wrapper->field_my_reference_field->label(); 
// To print the value of a field on the referenced entity
print $wrapper->field_my_reference_field->field_on_referenced_entity->value();
| improve this answer | |

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.