drupal 8 доступ к типу ввода из шаблона метки

#drupal #drupal-8

#drupal #drupal-8

Вопрос:

Как вы ссылаетесь на тип ввода соответствующей формы из шаблона метки элемента формы (form-element-label.html.twig)?

Ответ №1:

Тип элемента недоступен в этом шаблоне TWIG. Вам нужно будет подключиться к элементу формы и передать переменную в привязку метки элемента.

 /**
 * The contents of $variable['label'] will be passed to the preprocess
 * hook of the element's label.
 */
function theme_preprocess_form_element(array amp;$variables) {
  $variables['label']['#attributes']["data-element-type"] = $variables['element']['#type'];

}

/** 
 * Now we need to merge the array element we created in the previous hook
 * with the label's attributes.
 */
function theme_preprocess_form_element_label(array amp;$variables) {
  $variables['attributes'] = array_merge($variables['attributes'], $variables['element']['#attributes']);
}
 

Метка будет иметь следующий вывод (я тестирую тип элемента электронной почты).

 <label for="edit-email-addresses" data-element-type="email" class="js-form-required form-required">Email Addresses</label>
 

Не забудьте очистить кеши 😉