如何在树枝模板中显示 drupal 8 块

How to display drupal 8 block in twig template

我已经通过创建一个页面覆盖了 drupal 首页--front.html.twig。 在那个 twig 文件中,我想 呈现 drupal 的用户登录块 。 我在网上搜索过,但没有找到解决该问题的明确方法。 我开始理解的是让这成为可能我有 2 个解决方案。

  1. 解决方案 1: 使用 hook_preprocess_HOOK()
  2. 解决方案 2: 使用像 Twig Tweak 这样的插件

我说得对吗?

我已经通过安装 twig tweak 尝试了第二种解决方案,并在我的 twig 模板文件中插入了以下行
{{ drupal_entity('block', 'block_id') }} 其中 block_id 表示可以在配置页面中找到的块的机器名称。
它什么都不显示,这是正常的,因为当我执行 {{ kint(drupal_entity('block', 'block_id')) }} 转储值时,我得到了一个 NULL 值。
我不明白为什么,因为我放置了用户登录表单块并且将其配置为仅在 <front> 页面上可见。

Twig Tweak

Twig Tweak module provides a Twig extension with some useful functions and filters that can improve developer experience.

例如:<dd>{{ drupal_view('centers', 'block_1') }}</dd>

希望对您有所帮助

谢谢