5

I am using Drupal Lightning and a bootstrap subtheme. I have a requirement to place a section of the node page template in another block region. So I need to access the nodes fields in that block, fields like the taxonomy, block, breadcrumbs. Example my node--page.html.twig template prints these fields:

{{ node.title.value }}
{{ content.field_text }}
{{ drupal_view('mysection', 'block_1') }}
{{ drupal_block('mytheme_breadcrumbs') }}

It also has an includes template

{% include '@mytheme/includes/section.html.twig' %}

I can print these fields fine in a node template

node--page.html.twig

But I need to print these fields outside of the node template and into a block. I tried to create a View block and set a contextual filter "Content ID from URL" Then placed the View block in a region and then in the region template I tried to access the node fields from my region template but could not. I can access the title and body fields. This is the template.

region--myregion.html.twig

I then tried Entity blocks. I created a new block of type Entity view (Content). I tried to access the node fields from my region template but could not.

block--entityviewcontent.html.twig

1 Answer 1

10

Enable debug

by going in sites/default/ and copying the file default.services.yml to services.yml and modifying the following lines

cache: false
debug: true

Turn browser comments on

make sure your browser has Comments enabled (some browsers have them disabled by default for example in chrome in inspector on the 3 dot menu click settings and preferences find Show HTML comments

Find out which twig template is used

after that inspect the code in near the block that you want to override and you should see something similar to this

<!-- THEME DEBUG -->
<!-- THEME HOOK: 'block' -->
<!-- FILE NAME SUGGESTIONS:
   * block--block-name.html.twig
   * block--block-content--b436f9c6-34ae-4879-8b55-068015a999d3.html.twig
   * block--block-content--bundle-name.html.twig
   * block--block-content.html.twig
   * block--block-content.html.twig
   x block.html.twig
-->
<!-- BEGIN OUTPUT from 'themes/bootstrap/templates/block/block--system.html.twig' -->   

you can get the twig block template from the location in comment BEGIN OUTPUT

Use and modify template

Copy this twig file into your theme templates folder and rename to any name above. Modify the code and add something similar to the twig block template you just created

{% if nid %}
    nid:{{nid}} <br/>
    title:{{node_title}} <br/>
    body:{{body}}<br/>
    text:{{field_text}} <br/>    
{% endif %}

Expose node variable to block templates

Add into your THEMENAME.theme the following code or similar

function THEMENAME_preprocess_block(&$vars){
    $node = \Drupal::routeMatch()->getParameter('node');
    if ($node instanceof \Drupal\node\NodeInterface) {
      // You can get nid and anything else you need from the node object.
      $vars["nid"] = $node->id();
      $vars["node_title"] = $node->getTitle();
      $vars["body"] = $node->body->value;
      $vars["field_text"] = $node->field_text->value;

      //following did not work ?!
      //$vars["field_text"] = $node->get('field_text')->getValue();
      //$vars["field_text"] = $node->field_text->getValue();
    }    
}

Clear cache

with the above you are making the node fields available to block templates

2
  • Will doing it in this method give me the same access to a node like I have when using the node template: node--page.html.twig. I am going to try to print a partial included template, taxonomy field and a block {{ drupal_block('my theme_breadcrumbs') }} {{ drupal_view('section', 'block_1') }} Commented Dec 15, 2017 at 17:28
  • Not sure how much time I can devote on help or tutoring but you can always shoot me a message and will see ... about the 2nd question I think you have to insert what you will need in the block template inside the _preprocess_block function
    – GiorgosK
    Commented Dec 15, 2017 at 18:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.