90

Темизация элементов формы Drupal

Когда в Drupal 9 выводятся материалы через views, можно раскрыть фильтры, как здесь. Чтобы обернуть каждый элемент формы в свои классы, пишем в файле темы следующий код:

Чтобы были универсальные шаблоны элементов form-element.html.twig

function название_темы_theme_suggestions_form_element_alter(array &$suggestions, array $variables, $hook) {
   $label_for = $hook_name = $variables["element"]["#id"];
    $matches = array();
    if(preg_match('/^edit-(.*)/', $label_for, $matches)) {
       $hook_name = $matches[1];
    }
    $suggestions[] = 'form-element--'.$hook_name;
}

Что бы были универсальные ярлыки для элементов form-element-label.html.twig

function название_темы_theme_suggestions_form_element_label_alter(array &$suggestions, array $variables) {
   $label_for = $hook_name = $variables["element"]["#id"];
    $matches = array();
    if(preg_match('/^edit-(.*)/', $label_for, $matches)) {
       $hook_name = $matches[1];
    }
    $suggestions[] = 'form-element-label--'.$hook_name;
}

Чтобы правильно воспринимался нужный шаблон, также добавить это:

function название_темы_theme_suggestions_form_element_alter(array &$suggestions, array $variables, $hook) {
 if (isset($variables['element']['#name'])) {
   $suggestions[] = 'form_element__' . explode("[", $variables['element']['#name'])[0];
  }
}

После этого должен восприниматься шаблон:

<!-- FILE NAME SUGGESTIONS:
  x form-element-label--liter.html.twig
  * form-element-label.html.twig
-->

Оригинал вопроса и ответа на странице drupal.ru https://drupal.ru/node/132248

Ограниченный HTML

  • Допустимые HTML-теги: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Строки и абзацы переносятся автоматически.
  • Адреса веб-страниц и email-адреса преобразовываются в ссылки автоматически.
CAPTCHA