小编Mar*_*mri的帖子

Drupal表单模板建议

我正在尝试使用以下方法创建 drupal 表单建议:

function pfe_theme_suggestions_form_alter(array &$suggestions, array $variables) {
    $suggestions[] = 'form__'. $variables['element']['#id'];
}
Run Code Online (Sandbox Code Playgroud)

但在创建form--user-login-form.html.twig后,drupal 不会考虑此模板,但它始终使用form.html.twig

这是 html 注释:

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'form' -->
<!-- FILE NAME SUGGESTIONS:
   * form--user-login-form.html.twig
   x form.html.twig
-->
<!-- BEGIN OUTPUT from 'core/themes/stable/templates/form/form.html.twig' -->
Run Code Online (Sandbox Code Playgroud)

添加 HOOK_theme() 后:

function pfe_theme_suggestions_form_alter(array &$suggestions, array $variables) {
   $suggestions[] = 'form__'.$variables['element']['#id']; //form--user-login-form.html.twig
}

function pfe_theme($existing, $type, $theme, $path) {

  return [
    'form__user-login-form' => [
      '#theme' => 'form--user-login-form',
      'render element' => 'form',
    ], …
Run Code Online (Sandbox Code Playgroud)

drupal drupal-8

5
推荐指数
1
解决办法
5933
查看次数

标签 统计

drupal ×1

drupal-8 ×1