2

In Drupal 8 I am trying to add in a suggestion via hook_theme_suggestions_alter() like so:

function ua_sc_theme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  print '<!-- hook: ' . $hook . '-->';
  if (in_array($hook, ['input', 'form_element']) && $variables['element']['#attributes']['id'] == 'edit-keys') {
    $suggestions[] = $hook . '__custom';
  }
}

...and then I give my template a name of form-element__custom.html.twig and put it in my theme it doesn't get used an instead the default form-element.html.twig template is used. Any thoughts on what is going on?

2 Answers 2

2

Ok, looks like I was too tired. The problem was that the template should be called form-element--custom.html.twig.

Notice the '--' (double dash) instead of the __ (double underscore).

I also ended up using the more specific hook alter:

hook_theme_suggestions_HOOK_alter

...where HOOK for me was form_element.

1

In your form add '#theme' => 'input__custom' to the input element. The 'input' is the theme implementation and '__custom' is the suggestion.

This hook is for global suggestions.

2
  • I am trying to theme an exposed view filter form element - so I would need to do that in an alter. Is there no other way?
    – tzm
    Commented Aug 7, 2015 at 1:24
  • There is nothing wrong with using hook_form_alter or hook_form_FORM_ID_alter in this case.
    – user21641
    Commented Aug 7, 2015 at 8:37

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.