title: Drupal 8 Taxonomy term in twig
body: <p>I want to enhance the title of a page with a taxonomy term value that has been set in a field.</p>
  
  <p>So the title looks like this:</p>
  
  <h1>Title - [taxonomy-term-value]</h1>
  
  <p>To modify the title, i overwrote</p>
  
  <pre><code>field--node--title--[content-type].html.twig
  </code></pre>
  
  <p>So far so good, access to the node itself and the other fields are requested through the element object:</p>
  
  <pre><code>element['#object'].get('field_my_field').get(0)
  </code></pre>
  
  <p>To get the first element. </p>
  
  <pre><code>element['#object'].get('field_my_field').get(0)['target_id']
  </code></pre>
  
  <p>returns the tid of the taxonomy term. But </p>
  
  <pre><code>element['#object'].get('field_my_field').get(0)['name']
  </code></pre>
  
  <p>returns an empty element. </p>
  
  <p>How can the field value be retrieved? I cannot imagine that this should require php code.</p>
  
  <p><strong>Update:</strong>
  I figured out a way, but it seems a bit complicated:</p>
  
  <p>Since the taxonomy terms are not resolved in #object, i had to add a hook_preprocess:field function to prepare a variable with the necessary data:</p>
  
  <pre><code>function myTheme_preprocess_field(&amp;$variables, $hook){
    if ($variables['field_name'] == 'title'){
      $variables['my_field'] = Term::load($variables['element']['#object']-&gt;get('my_field')-&gt;get(0)-&gt;getValue()['target_id']);
  }
  </code></pre>
  
  <p>The field can now be accessed in twig as follows:</p>
  
  <pre><code>{{ my_field.name.getValue()[0]['value'] }}
  </code></pre>
  
  <p>To me this seems to be like a lot of work, so a more straight forward approach is much appreciated.</p>