【问题标题】:How to render specific form elements Drupal 8如何渲染特定的表单元素 Drupal 8
【发布时间】:2018-02-26 21:36:43
【问题描述】:

我正在使用 Drupal 8,并希望自定义表单元素的显示方式。具体来说,我不喜欢不可编辑的填充文本字段如何显示为纯文本。我会将它显示为可编辑的文本字段(或者让文本看起来像是在不可编辑的文本字段中)。我查看了各种钩子函数来尝试实现这一点,但似乎没有任何效果。

我认为解决此问题的最佳方法是,如果我可以自己单独渲染表单字段,然后创建一个 twig 文件来显示我希望显示的各个字段。这是我希望树枝字段的样子:

<div class="from">
  {{ form.mail }}
</div>

<div class="message">
  {{ form.message }}
</div>

<div class="actions">
  {{ form.actions }}
</div>

【问题讨论】:

    标签: drupal twig drupal-8 drupal-theming


    【解决方案1】:

    在你的 .module 文件中

    function your_module_theme($existing, $type, $theme, $path) {
        return [
            'custom_theme' => [
                'variables' => [
                    'form' => NULL
                ],
                'render element' => 'form',
            ]
        ];
    }
    

    在您的 CustomForm.php 文件中

    public function buildForm(array $form, FormStateInterface $form_state) {
        $form = parent::buildForm($form, $form_state);
        [...your fields ...]
        return $form
    }
    

    在您的自定义模块模板目录中

    自定义主题.html.twig

    {{ form.your_field }}
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 2022-11-01
      • 2023-01-16
      相关资源
      最近更新 更多