Get taxonomy terms added to nodes 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()-&gt;getStorage('taxonomy_term')-&gt;loadTree('field_category');

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


I am trying to get all the taxonomy term values, but just the entity field values.
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(&amp;$variables) {

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

  if ($variables['node']-&gt;hasField('field_permit_city')) {
    //this will give you the referenced terms on the node as objects
    $term_objects = $variables['node']-&gt;get('field_permit_city')-&gt;referencedEntities();
    $term_labels = [];
    foreach ($term_objects as $term_object) {
      print $term_object-&gt;id(); //the id of the term
      print $term_object-&gt;label(); //the term title
      $term_labels[] = $term_object-&gt;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 %}
    &lt;p&gt;{{ term }}&lt;/p&gt;
{% endfor %}