3

I have a content type with a taxonomy term. I can load the first EntityReferenceItem using

$node->country->get(0)

then I can tid doing ->target_id (and then manually load the Term object via Term::load.

But is there a function of EntityReferenceItem that I can call to get it to load the corresponding term? I'm thinking something like:

  $node->country->get(0)->getReferenced()

I don't see anything at the Drupal API reference

7
$node->country->entity.

See http://wizzlern.nl/drupal/drupal-8-entity-cheat-sheet, where it lists the properties of common field types.

If you want to see the properties of a field type, one way to do that is by looking at the propertyDefinitions() method of the field type class, in this case EntityReferenceItem.

  • What is there multiple values allowed? ->country[0]->entity ? – Nic May 5 '16 at 14:12
  • not specifying a delta means [0],works always, also on multi-value fields. Delta works with [1] or ->get(1). – Berdir May 5 '16 at 14:42
0

Update code for 8.6, following example of term reference field

$tid = $para->get('field_category')->getString();
$tname = $para->get('field_category')
      ->get(0)
      ->get('entity')
      ->getString();

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.