Templates in subtheme

Created on 31 August 2023, over 1 year ago
Updated 1 September 2023, over 1 year ago

Problem/Motivation

I'm trying to fine tune the design in a site where I want to display some bugs with their complexity to be solved. Therefore I have a term reference field, and I need to get the term id and build a class name with that I wish to make available in my twig template. So that if the issue is easy, i can have a yellow background, changing to red with the difficulty and to be green when it is solved.

Steps to reproduce

So I have created a preprocess function:

function acc_preprocess_node(&$variables)
{
  $node = $variables['node'];
  // Check if the field exists on this node bundle
  if ($node->hasField('field_complexity') && !$node->field_complexity->isEmpty()) {
    // Get the referenced taxonomy term.
    $term_reference_item = $node->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;
    //dump($variables);
  }
}

That function works. The dump shows what is expected.
Cache rebuilt, I can see that the template is loaded, but the value of my term_id is not loaded into it, whereas I have set:

{%
set term_classes = [
    'field--term-id-' ~ acc_term_id,
  ]
%}

Later, the class is displayed as "field--term-id-", the variable is not accessible in the scope of my template. Why ?

Then I tried to see if it was available from the template html.html.twig which I have copied in my subtheme template/layout folder.

Problem is that this template is never included, in place of it, the barrio template is loaded
<!-- BEGIN OUTPUT from 'core/themes/bootstrap_barrio/templates/classy/layout/html.html.twig' -->

Period. It seems that the logic at work doesn't care if there is a qualified template in the subtheme.

I call this a bug, as the purpose of a subtheme is to be able to override anything, especially templates.

Proposed resolution

- Explain how to pass the content of $variables to any template.
- Enforce the use of subtheme template when they exist with the correct name.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Closed: works as designed

Version

5.5

Component

Miscellaneous

Created by

🇭🇰Hong Kong jmary

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @jmary
  • Status changed to Closed: works as designed over 1 year ago
  • 🇭🇰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