Let say I have a node template which is calling twice the same field like the following:

{# First call #}
{{ content.my_field }}
{# Second call #}
{{ content.my_field }}

I also have a preprocessing function which is adding VAR1 and VAR2 to the 'my_field' array like the following:

function mymod_preprocess_field(&$vars) {
  if (isset($vars['element'])) {
    if ($fn=='my_field'){
      $vars['VAR1'] = 'a value';
      $vars['VAR2'] = 'another value';

I would like that the first call of 'My_field' use the default twig template and the 'VAR1' value... As you can imagine, I could do it as this is the normal way Drupal is working. But I didn't success to make use of an alternate twig template for the second call (and also, I would like this second template use 'VAR2')

My first idea was to put the following just before the first call:

{{ set content.my_field.is_first_call = true }}

and to use only the default template (I mean not 2 templates) with something like

{% if is_first_call %}
  ... code to print the first call
{% else %}
  ... code to print the second call
{% endif %}

But I couldn't succeed to transmit a value (is_first_call) from the node template to the default my_field template

Final note: I have added the 'VAR1/VAR2' story because I have also tested the {{ content.my_field | without ('a_check_var')}} filter: the idea was to remove a_check_var from the array to use a construct like {% if a_check_var %} in the default template. Unfortunately, my preprocess function which is adding 'VAR1/VAR2' runs after the without filter

1 Answer 1


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.


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.

  • Many thanks! I could succeed by moving the code from my preprocess_field function to my preprocess_node function. This is not what you have explained, but your proposal was working and give me the idea to do so. This is why I validated it.
    – Baud
    Commented Sep 13, 2016 at 12:45
  • Great, the example is about the idea how node and field template work together. Then there are a lot of things you can do with this.
    – 4uk4
    Commented Sep 13, 2016 at 12:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.