11

Drupal\node\Entity\Node Object ( [in_preview] => [values:protected] => Array ( [vid] => Array ( [x-default] => 1 )

        [langcode] => Array
            (
                [x-default] => en
            )

        [field_destination] => Array
            (
                [x-default] => Array
                    (
                        [0] => Array
                            (
                                [target_id] => 2
                            )

                    )

            )

Not able to get field_destination value directly. It's a taxonomy term attached with the content type. Any help appriciated.

user32012
  • 129
  • 1
  • 1
  • 5

6 Answers6

17

To build on VJamie's answer.

You will need to either set a use statement at the top of your script;

use Drupal\taxonomy\Entity\Term;

Or, prefix the class instance with the namespace;

$term = \Drupal\taxonomy\Entity\Term::load($node->get('field_destination')->target_id);

That will get rid of the fatals.

Community
  • 1
  • 1
Christian
  • 3,917
  • 2
  • 23
  • 40
14

You can also use some methods from EntityReferenceFieldItemList: Gets the entities referenced by this field, preserving field item deltas:

$node->get('field_destination')->referencedEntities();

Hope it will be useful for you

bkildow
  • 5,143
  • 4
  • 29
  • 37
wau
  • 426
  • 3
  • 8
  • 1
    If I'm not mistaken, `$node->get('field_destination')->getEntity();` will simply return `$node` itself. The `referencedEntities()` method is very useful though. – marcvangend Oct 05 '17 at 11:13
  • @marcvangend is correct, [getEntity()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21FieldItemList.php/function/FieldItemList%3A%3AgetEntity/8.4.x) returns the field parent, i.e. its Node. [referencedEntities()](https://api.drupal.org/api/drupal/core%21lib%21Drupal%21Core%21Field%21EntityReferenceFieldItemList.php/function/EntityReferenceFieldItemList%3A%3AreferencedEntities/8.4.x) is the correct answer. – Tim Oct 24 '17 at 12:22
  • Thanks a lot for your answers. I will remove getEntity() from my answer to avoid confusion. – wau Oct 25 '17 at 06:29
  • A highly accurate answer – FreeLightman Feb 11 '18 at 10:36
  • This is the best answer—delightfully succinct. – richbs Sep 13 '18 at 15:34
  • I think $node->get('field_destination')->entity also give the same result – khaled_webdev Feb 24 '22 at 08:09
9

The following code will get you the term object you need.

$term = Term::load($node->get('field_destination')->target_id);

If you need the name of that term you can do the following

$name = $term->getName();

Hope this helps out!

VJamie
  • 616
  • 3
  • 14
  • How ever Term::load causing an issue.Fatal error: Class 'Drupal\rest\Plugin\Block\Term' not found . – user32012 May 31 '16 at 15:55
  • 1
    @VJamie I tried adding `use \Drupal\taxonomy\Entity;` and used `Term::load($node->get('field_destination')->target_id);` but it failed. What class did you load? – usernameabc Aug 09 '18 at 17:00
2

Do this

use Drupal\taxonomy\Entity\Term;
$term = Term::load($node->get('field_destination')->target_id);
$termname = $term->getName();

In drupal8 we used to follow oops approach to get the values.

vinay kumar
  • 164
  • 6
1

This is the correct way on how to achieve it

use Drupal\taxonomy\Entity\Term;

function modulename_node_presave(Drupal\Core\Entity\EntityInterface $entity) {
    switch ($entity->bundle()) {
        case 'programs':
            $term = Term::load($entity->get('field_program_names')->target_id);
            $name = $term->getName();
            $entity->setTitle($name);
            break;
    }
}
0

entity property can be accessed directly from any reference type field.

$node = 'myNode';
$termEntity = $node->get('field_taxonomy_reference')->entity;
if ($termEntity instanceof TermInterface) {
  $termLabel = $termEntity->label();
}
hugronaphor
  • 948
  • 8
  • 23