0

I am trying to get all the taxonomy terms added to a node. How would I get only the taxonomy term values that are added to a code in a mymodule_preprocess_node hook?

Current term list:

  • value1 (tid=1)
  • value2 (tid=2)
  • value3 (tid=3)

Node list:

  • node1 has field_category = 1
  • node2 has field_category = 3
  • node3 has field_category = 1,2,3

Here is what I tried, but cannot find a way to filter and only return the taxonomy term values that are added to a node.

// Returns entire list of values, but we want only ones added to Basic page 
$term_tree = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadTree('field_category');

$nodes = \Drupal::entityTypeManager()->getStorage('node');

I am trying to get all the taxonomy term values, but just the entity field values.

  • @leymannx thanks for the link, but that one only addresses how to access the field values. My question is how to return all the taxonomy terms when there are more than terms added to a node, which i consider the next step after get the field value. – usernameabc Jan 2 at 23:15
  • Then your question actually is "How do I load a taxonomy term by term ID"? – leymannx Jan 2 at 23:29
2

Another example: As with all referenced entities you can load them quickly using the referencedEntities() method.

Also, if you are not using the Devel module and Devel Kint submodule, you should check it out. You can then use ksm() to explore variables and see available methods.

function hook_preprocess_node(&$variables) {

  //make sure you have Devel module installed, then you can use ksm() to explore variables
  //ksm($variables['node']);

  if ($variables['node']->hasField('field_permit_city')) {
    //this will give you the referenced terms on the node as objects
    $term_objects = $variables['node']->get('field_permit_city')->referencedEntities();
    $term_labels = [];
    foreach ($term_objects as $term_object) {
      print $term_object->id(); //the id of the term
      print $term_object->label(); //the term title
      $term_labels[] = $term_object->label(); //build an array of term labels
    }
    //send an array of term titles over to the node twig template
    $variables['my_custom_template_variable'] = $term_labels;
  }
}

Then in your node.html.twig template you can iterate through the labels and do the stuff.

{% for term in my_custom_template_variable %}
    <p>{{ term }}</p>
{% endfor %}
2

To get all the taxonomy terms added to a node.

In hook_preprocess_node you can use something like:
add use Drupal\taxonomy\Entity\Term; to the top of your file.

function mymodule_preprocess_node(&$variables) {
  $node = $variables['node'];
  if ($node->bundle() == 'your_content_type_name') {
    //- Array of terms
    $terms = [];
    $categories = $node->field_category->getValue();
    foreach ($categories as $category) {
      $term = Term::load($category['target_id']);
      //- You can get the name of term by.
      /**
       * $term->getName()
       */
      //- You can get the description of term by.
      /**
       * $term->getDescription()
       */
      $terms[] = $term;
    }
  }
}

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.