0

Simple question, I just need to know how to attach a library to Drupal 8's contact forms. I recently made a similar post for attaching a library to a view, that was accomplished by adding {{ attach_library('mytheme/mylib') }} to views-view--myview.html.twig, but doing the same to contact forms is less obvious. If the preferred way to attach a library is through twig templates can someone please advise me on what the naming convention is for contact-form twig templates?

1 Answer 1

1

Enable twig debug in your develop environment, then you can see through HTML comments all theme suggestions, then create a twig file with based in what you want template.

Original post here.

Debugging Twig templates

The Twig templating engine offers a debug tool.

The Drupal 8 implementation also adds an additional tool that allows you to locate the template that outputs the markup.

Enable debugging

You enable Twig Debugging in sites/default/services.yml. (If services.yml does not yet exist; copy default.services.yml and rename it to services.yml.)

Set the debug variable to true.

parameters:
  twig.config:
    debug: true 

Just pay attention, if you not using site's default folder, use that you created.

After turn on debug, clear the cache, then you will see something like this in your HTML

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'form' -->
<!-- FILE NAME SUGGESTIONS:
   x form--feedback.html.twig
   * form.html.twig
-->

If you need create a new name suggestion for a element, use THEME_theme_suggestions_HOOK_alter in your .theme file, and add your suggestion.

Example:

function THEME_theme_suggestions_form_alter(array &$suggestions, array $variables) {
  $suggestions[] = 'form__' . $variables['element']['#form_id'];
}

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.