3
votes

I'm trying to create a drupal form suggestion using :

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

but after creating the form--user-login-form.html.twig , drupal doesn't take this template in consideration but it always uses form.html.twig.

This is the html comment :

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'form' -->
<!-- FILE NAME SUGGESTIONS:
   * form--user-login-form.html.twig
   x form.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/form/form.html.twig' -->

After adding the HOOK_theme() :

function pfe_theme_suggestions_form_alter(array &$suggestions, array $variables) {
   $suggestions[] = 'form__'.$variables['element']['#id']; //form--user-login-form.html.twig
}

function pfe_theme($existing, $type, $theme, $path) {

  return [
    'form__user-login-form' => [
      '#theme' => 'form--user-login-form',
      'render element' => 'form',
    ],
  ];

}

I get this error :

Twig_Error_Loader: Template "themes/pfe/templates/form--user-login-form.html.twig" is not defined (Drupal\Core\Template\Loader\ThemeRegistryLoader: Unable to find template "themes/pfe/templates/form--user-login-form.html.twig" in the Drupal theme registry.). in Twig_Loader_Chain->getCacheKey() (line 43 of /home/marwen/workspace/pfe/themes/pfe/templates/block/block--userlogin.html.twig).

1
If it's still not working, where exactly do you place your template?leymannx

1 Answers

3
votes

Replace "-" with "_" using "str_replace", example:

$suggestions[] = 'form__' . str_replace('-','_', $variables['element']['#id']);