Reputation: 14833
In my Basic page
content type I have an image field called sidebar_image
.
I have placed a block to my header region called header_block
which is using a custom twig template. I'm wondering how I can get the Basic page sidebar_image
to be displayed in my header_block
twig template:
<h1>Some header</h1>
<img src="{{ sidebar_image }}" />
and in the preprocess function:
THEME_preprocess_block__header_block {
$variables['sidebar_image'] = // get current pages sidebar image URL
}
Upvotes: 0
Views: 1992
Reputation: 7124
You should be able to load current page (node) like this:
$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
And then should be possible to access field value like:
$node->get('field_sidebar_image')->getValue();
Just add some checking if your node is in specific content type and similar.
Upvotes: 1