2

I have a custom content type, view and block all setup and working properly. When I attempt to use kint to access node or anything else from my block template file: views-view--testimonials--block_1.html.twig i keep getting NULL values returned.

Prior to attempting to customize my block template, in my content-types folder, i had a file to directly access my content type: node--testimonials.html.twig which could access the values like so: {{ node.field_testimonial_author.value }}

My question is, how can I get access to node.field_testimonial_author from my block template file? (which is views-view--testimonials--block_1.html.twig)

I may be approaching this entirely wrong. If so, please feel free to offer any educational resources that will help me out. Coming from Drupal 7 the learning curve here for me is essentially a brick wall.

Thanks in advance.

3

Blocks don't have the same variables as other entity types do. They will vary in their theme hook implementations. That is why there is no {{ node.field }} concept for blocks, the information does not exist. The same principle applies to Views, Media, Paragraphs and other entities. For example, to reference fields on the paragraph in its template, you wouldn't call {{ node.field_foo }} you would call {{ paragraph.field_foo }}.

Just because a view block is rendering on a node detail page doesn't mean that the same twig template automatically sees a node object.

If the View is building this block display, I would say add the field to the View that you want to output (Author).

If you want to blow your own mind, if the view you have is returning fields on a node, change the display to use the rendered content mode, and choose a display mode (start with Teaser). This approach will allow you to use node--node-type twig templates, as Views is rendering the entity instead of printing fields into the View. The View output is then driven by the display mode on the node type itself and is a much more manageable result in most cases.

  • This is ultimately what I ended up doing, I then added a Contextual Filter for my block types to check against a taxonomy term...MUCH simpler! Thank you! – just_trying_to_learn Feb 15 '17 at 21:25
  • You can add unlimited display modes and View configurations with this approach and theming is much more straightforward. You can also do node--node-type--display-mode for granular overrides too. – Kevin Feb 15 '17 at 21:26
  • 1
    @Kevin, you might want to embed that last comment as part of your answer. It is pretty valuable :) – Neograph734 Feb 15 '17 at 23:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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