Account created on 3 January 2007, about 18 years ago
#

Recent comments

🇭🇰Hong Kong jmary

The preprocess function should be:

function acc_preprocess_field(&$variables)
{
  $element = $variables['element'];
  // Check if the field is the one we are interested in
  if ($element['#field_name'] === 'field_complexity') {
    // Assuming you have an entity in $element
    $entity = $element['#object'];
    // Check if the field exists on this entity
    if ($entity->hasField('field_complexity') && !$entity->field_complexity->isEmpty()) {
      // Get the referenced taxonomy term.
      $term_reference_item = $entity->get('field_complexity')->first();
      // Get the term entity.
      $term = $term_reference_item->entity;
      // Get term ID.
      $term_id = $term->id();
      // Add term ID to variables.
      $variables['acc_term_id'] = $term_id;
    }
  }
}

Then the variable is available in my template field: field--node--field-complexity--issues.html.twig

Also, I had to check the permissions and ownership of the templates. The reason my templates were not used was because they were not readable by www-data.

Sorry for the noise. It might help someone facing similar issues.

Production build 0.71.5 2024