0

I have a custom field with formatter which is currently outputting via field.html.twig

I've modified the theme function to

function mymodule_theme()
{
    return [
        'field__field_family_and_education' => ['base_hook' => 'field'],
    ];
}

and the twig debug indicates it's now using this template, however, despite copying the field.html.twig to field--field-family-and-education.html.twig the variables are not being passed to the template. I want the template to reside in the module, not in the main theme so this is being done within the module that defines the field & formatter.

I'm guessing I've missed an obvious step, any ideas?

  • "the variables are not being passed to the template" <- what does this mean? – Jaypan Mar 3 '20 at 17:29
  • Within the template the variables are empty, for example {% for item in items %} <div{{ item.attributes.addClass('field__item') }}>{{ item.content }}</div> {% endfor %} Gives nothing, plain text within the template (as expected) gets output. So the field content is not getting to the my template, but does get to the default one when. Currently the template is a direct copy of the default field template. – Alex Monaghan Mar 4 '20 at 9:25
  • If I copy the new template into the core theme folder (just for testing purposes) the variables populate OK so I guess I'm missing something somewhere. – Alex Monaghan Mar 4 '20 at 10:19
  • Digging further, with the twig dump(), the "items", field_name/type, label variables are not populated when my template is loaded, all other variables appear to be OK. Is there something different to overriding a template that is within a module to overriding one within a theme? – Alex Monaghan Mar 4 '20 at 11:57
  • I think you need to implement hook_theme_suggestions_alter(). – Beau Mar 18 '20 at 15:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.