Score:1

Drupal 8 Theme suggestions for form children elements

ve flag

What is the best way to define template name suggestions for children elements of form in drupal 8?

In myTheme.theme I can define hook like this:

function myTheme_theme_suggestions_alter(array &$suggestions, array $variables, $hook) {
  if ($hook == 'form' & !empty($variables['element']['#id'])) {
   $suggestions[] = 'form__' . str_replace('-', '_', $variables['element']  ['#id']);
  }
}

Which gives suggestions:

form--form-id.html.twig
form.html.twig

I want to define template names for all elements inside my form with id of the form as part of name suggestion. For example:

form-element--form-id.html.twig
form-element.html.twig

Edit: I understand there is a plugin for that. But I would preffer programmatic solution for better customization in future and also to not be completely dependend on external plugin functionality.

jp flag
I would say this is a duplicate of: https://drupal.stackexchange.com/questions/302423/how-to-add-template-suggestions-for-form-elements-of-a-certain-form-id I can't vote to close this issue as a duplicate of that one because that one doesn't have an accepted answer unfortunately.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.