4

I'm trying to get Taxonomy data by particular node.

How can I get Taxonomy Term Id by using Node object ?

Drupal ver. 8.3.6

Arfeen
  • 2,553
  • 5
  • 29
  • 48
  • Possible duplicate of [drupal 8 get taxonomy term value in node](https://stackoverflow.com/questions/37122908/drupal-8-get-taxonomy-term-value-in-node) – Moby M Aug 16 '17 at 07:19
  • Probably no, the other answer belongs to previous version. The solution does not fit to current Drupal. (8.3.6 particularly) – Arfeen Aug 16 '17 at 15:53
  • what u have tried?update the code. – Moby M Aug 17 '17 at 05:17

4 Answers4

6

You could do something like that:

$termId = $node->get('field_yourfield')->target_id;

Then you can load the term with

Term::load($termId);

Hope this helps.

Kevin Wenger
  • 1,484
  • 1
  • 13
  • 29
exit
  • 61
  • 5
  • 2
    You can also load the term from the node such as `node->get('field_yourfield')->entity` and from here retrieve the name such as `node->get('field_yourfield')->entity->name->value`. – Kevin Wenger Aug 18 '17 at 12:41
  • `->target_id` is great if you just have a single term, unfortunately if, say, the user can tick multiple checkboxes, it only ever gives you the first one. I can't see anything obvious in the source code or with kint(), so back to ->referencedEntities() or getValue() it is… – William Turrell Sep 18 '19 at 17:36
5

If you want to get Taxonomy Term data you can use this code:

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

Hope it will be useful for you.

PS: If you need just Term's id you can use this:

$node->get('field_yourfield')->getValue();

You will get something like this:

[0 => ['target_id' => 23], 1 => ['target_id'] => 25]

In example my field has 2 referenced taxonomy terms. Thanks!

wau
  • 426
  • 3
  • 8
1

@Kevin Wenger's comment helped me. I'm totally basing this answer on his comment.

In your code, when you have access to a fully loaded \Drupal\node\Entity\Node you can access all the (deeply) nested properties.

In this example, I've got a node which has a taxonomy term field "field_site". The "field_site" term itself has a plain text field "field_site_url_base". In order to get the value of the "field_site_url_base", I can use the following:

$site_base_url = $node->get('field_site')->entity->field_site_url_base->value;
0

How to extract multiple term IDs easily if you know a little Laravel (specifically Collections):

Setup: composer require tightenco/collect to make Collections available in Drupal.

// see @Wau's answer for this first bit...
// remember: if you want the whole Term object, use ->referencedEntities()
$field_value = $node->get('field_yourfield')->getValue();

// then use collections to avoid loops etc.
$targets = collect($field_value)->pluck('target_id')->toArray();

// $targets = [1,2,3...]

or maybe you'd like the term IDs comma-separated? (I used this for programmatically passing contextual filter arguments to a view, which requires , (OR) or + (AND) to specify multiple values.)

$targets = collect($field_value)->implode('target_id', ',');

// $targets = "1,2,3"
William Turrell
  • 3,227
  • 7
  • 39
  • 57