Puncte:1

How do I add a template suggestion for form input fields that have containers?

drapel in

I've been able to successfully add the parent form ID to all form elements, based on these two questions: How can we find the id of the parent form in preprocess_HOOK() or HOOK_alter() theme functions? and Add the Form name to the theme suggestions input field.

However, the two elements I really need template suggestions for are inputs that have containers... therefore the parent form ID is added to the containers, not the inputs, and when I add the template suggestions the variable is blank (obviously).

I see there are modules like Formdazzle! which makes this easy, but I feel like I'm close - however I'm struggling to 'reach' the children (the inputs) of the form elements (the containers).

This is my current code:

 * Implements hook_form_alter().
 */
function HOOK_form_alter(&$form, FormStateInterface $form_state, $form_id) {

  foreach (\Drupal\Core\Render\Element::children($form) as $key) {
    if ($form[$key]['#parents']) {
      $form[$key]['#attributes']['data-parent-form'] = $form['#id'];
    } else {
      // help!
    }
  }
}

/**
 * Implements hook_theme_suggestions_input_alter().
 */
function HOOK_theme_suggestions_input_alter(&$suggestions, array $variables) {
  $element = $variables['element'];
  $suggestions[] = 'input__' . $element['#type'] . '__' . $element['#attributes']['data-parent-form'];
}
leymannx avatar
drapel ne
Puteți preprocesa containerele pentru a verifica intrarea dvs. acolo și, dacă le găsiți, să atribuiți o variabilă intrării de acolo. Această variabilă poate fi apoi accesată în variabilele hook de sugestie mai târziu.
drapel in
@leymannx M-am uitat la asta și am ajuns să merg cu Formdazzle - încercam să reinventez roata și, în cele din urmă, trebuia să economisesc timp (și mă îngrijoam că o altă situație nu va fi acoperită în viitor). Totuși, aceasta este cu siguranță modalitatea de a proceda.
leymannx avatar
drapel ne
Da, [Formdazzle](https://www.drupal.org/project/formdazzle) întotdeauna plăcut pentru tematica formularului. Poate adăugați asta ca răspuns mai jos!

Postează un răspuns

Majoritatea oamenilor nu înțeleg că a pune multe întrebări deblochează învățarea și îmbunătățește legătura interpersonală. În studiile lui Alison, de exemplu, deși oamenii își puteau aminti cu exactitate câte întrebări au fost puse în conversațiile lor, ei nu au intuit legătura dintre întrebări și apreciere. În patru studii, în care participanții au fost implicați în conversații ei înșiși sau au citit transcrieri ale conversațiilor altora, oamenii au avut tendința să nu realizeze că întrebarea ar influența – sau ar fi influențat – nivelul de prietenie dintre conversatori.