How to use 2 twig template for for one field (or how to add a variable to a template from a template)

Solutions

How to add a variable to the field template from the node template?

What you see in the node template from the field is before the field preprocess is run.

The global preprocess for fields starts like this:

function template_preprocess_field(&$variables, $hook) {
  $element = $variables['element'];

  // Creating variables for the template.
  $variables['entity_type'] = $element['#entity_type'];
  $variables['field_name'] = $element['#field_name'];
  $variables['field_type'] = $element['#field_type'];
  $variables['label_display'] = $element['#label_display'];

Here can see, how the variables for the field template are built.

Example:

To add a variable in node set the variable with a '#' prefix:

function mytheme_preprocess_node(&$variables) {
  if (isset($variables['content']['my_field'])) {
    $variables['content']['my_field']['#var1'] = 'a value';
  }
}

And then in the field template:

function mytheme_preprocess_field(&$variables) { 
  if (isset($variables['element']['#var1'])) {
    $variables['var1'] = $variables['element']['#var1'];
  }
}

The variable is then available in the field twig:

{{ var1 }}

To solve your specific problem I would suggest you copy the field in node preprocess and then set for both copies different variables.

Tags: Theming / Drupal 8

Similar questions

How do I make twig variables available in node.html.twig accessible in my html.html.twig?
I have a boolean field on my Content Type called 'field_bootstrap'. I have some twig logic in my node.html.twig file that, if the field is set to true, includes Bootstrap via HTML inclusion. What I need is the ability to use my {{ node.field_bootstrap }} logic in the html.html.twig template, instead of the node-level template. This would allow me t...
How do I add a variable for a Twig template in my preprocess function?
I'm using the following code to preprocess the variables for my template: The #uri variable is already present, and if I just modify it everything works fine. But I need to add additional variables here as I need two different styles of the same image. But simply adding those variables to the array doesn't work. If I dump the available variables in...
How to add variable using mytheme_preprocess_field and print using twig template?
We have a content type Article that has two custom fields: field_header and field_link. When field_header is not populated, we want to perform validation in twig template to add a CSS class. When we tried to set a custom variable using $variables['myVariable'] = TRUE and try to print the value in the twig template, it is returning a NULL value. How...
Is it possible to add a php number counter variable to a twig template?
I have a custom template setup for the paragraph module in Drupal 8. It so the user can add an accordion to the body copy if wanted. I have an accessible code snippet that I have written and use whenever I need accordions. It works fine except when I use it normally, its on a custom coded page so I can change the ID value on each one. As this will ...
How to provide custom template declared inside my theme .theme file as variable inside page twig template
In my theme, I implemented HOOK_theme to declare custom twig template. How can I provide/preprocess this template as variable ready to to output inside my page.twig.html template ?
Programmatically delete one entry from one field on one entity
What's the proper way to programmatically delete one specific field entry for a multi-entry field from one specific entity? (that's specific fields and specific entities, not field types or instances and entity types) Here's some possibilities I've looked into which don't work: So to clarify, I've got code that finds (and loads) an entity which has...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.