在我的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
}
答案 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();
只需添加一些检查,即可检查您的节点是否具有特定的内容类型和类似内容。