如何在整体树枝模板中显示当前页面的字段

时间:2020-11-03 08:42:07

标签: drupal drupal-8

在我的Basic page内容类型中,我有一个名为sidebar_image的图像字段。

我在使用自定义树枝模板的标题区域header_block上放置了一个块。我想知道如何在Basic page sidebar_image树枝模板中显示header_block

<h1>Some header</h1>
<img src="{{ sidebar_image }}" />

和预处理功能中的

THEME_preprocess_block__header_block {
  $variables['sidebar_image'] = // get current pages sidebar image URL
}

1 个答案:

答案 0 :(得分:1)

您应该能够像这样加载当前页面(节点):

$node = \Drupal::routeMatch()->getParameter('node');
if ($node instanceof \Drupal\node\NodeInterface) {
  // You can get nid and anything else you need from the node object.
  $nid = $node->id();
}

https://drupal.stackexchange.com/questions/145823/how-do-i-get-the-current-node-id

然后应该可以访问字段值,例如:

$node->get('field_sidebar_image')->getValue();

https://drupal.stackexchange.com/questions/144947/how-do-i-access-a-field-value-for-an-entity-e-g-node-object

只需添加一些检查,即可检查您的节点是否具有特定的内容类型和类似内容。