3

In a Drupal 8 site design there is expected a view block and a static block in the middle of node content.

I've been trying to figure out how to render node content around the block region (in page.html.twig level), render the block region in the middle of the node content (in node.html.twig level) or any other work around, for ages.

I imagine one way would be to render part of the node content in a view block with contextual filter and other node content fields in the node.html.twig, but I don't know how resource heavy would it be for the system?

If there are any suggestions/solutions anyone here could think of for solving this as efficiently as possible, I'd be very happy to hear about them.

Best,

Alari

  • Have you seen the related questions drupal.stackexchange.com/questions/188122/…, drupal.stackexchange.com/questions/179426/… and others? – Berdir Mar 7 '16 at 19:18
  • @Berdir: I hadn't but thank you for the referral, the first one seems rather helpful and neither showed up for my search queries, as I wasn't really sure what to search for and mainly searched related to: "drupal 8 render node fields(/content) page level(+twig)" – Alari Truuts Mar 7 '16 at 19:24
  • Also added a more detailed answer now. The first one actually is listed for me in the sidebar under Related and i'd expect that would also show up as suggestions when you create the question, not sure why it wouldn't for you? – Berdir Mar 7 '16 at 19:33
  • @Berdir: It didn't for me, as this current title is the title edited by Vagner. – Alari Truuts Mar 7 '16 at 21:35
3

So one thing is directly accessing the node object that you have available. E.g. {{ node.some_field.value }} (which prints the raw value of e.g. a text field, not what you want in most cases) or {{ node.some_field.view() }} (which views it using the default formatter configuration, you can also provide a view mode or formatter configuration to it).

See also the two related questions that I already added as a comment: Render a node field inside page.html.twig AND Render image field from node in page.html.twig.

As you found out yourself, you can also use blocks for this. However, using views for that is a bit backwards. Views executes a query. So what it does is get the node from the already loaded ID, then does a query to get the node ID and then gets the node again for that Node ID ;)

As an alternative, blocks in D8 support context. So they can get things from the site injected and use it directly. See https://drupal.stackexchange.com/a/188629/31 for some explanation on this. You can implement a custom block yourself and print out what you want, or you can use the block that ctools provides that allows you to view a node with a specific view mode.

1

I believe twig doesn't let us use php. So I guess the only way is to use a module.

Display Suite

Display Suite allows you to take full control over how your content is displayed using a drag and drop interface. Arrange your nodes, views, comments, user data etc. the way you want without having to work your way through dozens of template files. A predefined list of layouts (D7 only) is available for even more drag and drop fun!

Display suite lets you add blocks as fields.

Panels

The Panels module allows a site administrator to create customized layouts for multiple uses. At its core it is a drag and drop content manager that lets you visually design a layout and place content within that layout. Integration with other systems allows you to create nodes that use this, landing pages that use this, and even override system pages such as taxonomy and the node page so that you can customize the layout of your site with very fine grained permissions.

Basically an alternative to Display Suite. Both of these modules are great. Panels has an easier create a layout system than Display Suite.


With either of these modules, you could do something like this

Field A

Block or Block View

Field B

  • Wouldn't using an extra dedicated module for this cause more overhead than rendering part of the node in a view block? (I'm not saying it does, I really don't know and would like to know :) ) – Alari Truuts Mar 7 '16 at 13:39
  • @AlariTruuts yes it will add some overhead, but I don't think it's a big deal considering that Drupal 8 caches everything. – No Sssweat Mar 7 '16 at 13:43
  • @AlariTruuts yes, rendering part of the node in a view block would be less overhead. Which I did not think about using that as a solution. – No Sssweat Mar 7 '16 at 13:46
  • Ok, for now I'll be going with the view block thing, thanks! – Alari Truuts Mar 7 '16 at 14:12
1

Also, if a field has multiple values, the syntax for it in page.html.twig is node.some_field[x].value. X is for the position in field value array.

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.