Blog

By toswebdev, 5 October, 2022

I will show you how to add Twig Template Suggestions for Form Elements in Drupal 9
 

/**
 * @param array $suggestions
 * @param array $variables
 * @param $hook
 */
function mytheme_theme_suggestions_form_element_alter(array &$suggestions, array $variables, $hook)
{
  if (isset($variables['element']['#id'])) {
    $id = str_replace("-", "_", $variables['element']['#id']);
    $suggestions[] = $hook . '__' . $id;
  }
}

The result is as follows
 

By toswebdev, 5 October, 2022

Edit services configuration for your Drupal site, for default location, should be:

/sites/default/services.yml

If the folder is correct, but you don't have a services.yml file, rename default.services.yml to services.yml.

Find twig.config section and change debug parameter to true:
 

Tags