81700

Get Taxonomy Term ID by Node in Drupal 8

Question:

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

Answer1:

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!

Answer2:

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.

Recommend

  • Apache Solr: Faceted Search on multivalued fields
  • How can I rewrite a wordpress URL to use the first sub directory as a querystring? [closed]
  • Kubernetes Architecture - Kubernetes Cluster Management And Initializing Nodes
  • Add a custom taxonomy filter after product category filter in Woocommerce Admin products list
  • Loosing Nav Active State in Wordpress Dynamic Menu
  • PHP Prevent creation of attributes NOT defined in class
  • 2 Layouts for a WooCommerce shop
  • pass two id s in extjs 4 tree multiSelect and pass one id in single click
  • Order by custom Woocommerce product sorting in a WP_Query
  • Get taxonomy links filtered by multiple taxonomy types
  • Wordpress term_exist not echo'ing anything?
  • Drupal: multilanguage.. looking for the right module to build multiple versions of the node
  • Custom category for custom post type
  • Call a function after retrofit and rxjava completes
  • Javascript regex URL matching
  • LOAD DATA LOCAL INFILE stops imports at 69k rows
  • Install OpenCv 2.4.2 with Cuda on Visual studio 9 2008: errors in build
  • Opens the splashscreen then shuts down IOS Delphi XE8
  • Invalid argument supplied for foreach() .. Wordpress
  • how to find colums having missing data in sparklyr
  • Drupal 6: defining js and css for particular pages
  • List Files in a Directory Sorted by Creation Time
  • Function to check if jquery plugins are already initialized
  • PHP cURL timeout ignored
  • Ignore “The” when sorting a View in Drupal
  • Use of qualified name in function parameter
  • Is there any way to use wpdb prepare statements for array implode(' OR ', $myArray)?
  • CakePHP ACL tutorial initDB function warnings
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Acquiring multiple attributes from .xml file in c#
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • How can I remove ASP.NET Designer.cs files?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • How can i traverse a binary tree from right to left in java?
  • How to Embed XSL into XML
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal