6

my content type contains several fields; body, field_meta_tags, field_image, field_date, etc.

In node.html.twig, using the without filter, I can replace the standard rendering of {{ content }} with {{ content|without('field_image') }} and all of the content, less the image, will display.

How can I expose this variable to another region and render the image through a different template, perhaps region--hero-image.html.twig?

Thanks!

3 Answers 3

6

Easiest way to do this is create a View with a block display, and set the node ID as the contextual filter. The output of the View should be "Content" and a view mode - this view mode can be set with its own twig template in the theme. You can add a view mode at Admin > Structure > Display Modes > View Modes, then go to whatever content type you need it for and configure the display. On the view mode, you can pick and chose what fields should display and what formatters they use.

Here is a good write up detailing it, and a YouTube video walking through view modes and what they are.

Once the View is created, you can place the block in the block admin, and set its visibility settings to only show on your content type(s) that you configured.

4
  • Thank you, but this can't be a View. I think I need to preprocess the field_image variable, but not quite certain how to accomplish that. Commented Jun 27, 2017 at 2:26
  • 1
    "Can't be a view" why can't it be a view? You could knock this out in 2 minutes with a View and a view mode. The View would render the node showing only the image field. You aren't going to be able to inject node fields into various regions by code easily, especially if you are new to Drupal 8. Views is the easiest/best/fastest way to getting that output. Since the View renders a view mode, it has the same twig suggestions (turn on twig debugger), and will trigger the same preprocess hooks if you need them.
    – Kevin
    Commented Jun 27, 2017 at 2:28
  • It can't be a View because I've been writing Views for over 10 years and I wanted to programmatically render the field. It can't be that hard. We're developers and we're smart people who know in the long run, writing this type of code takes less than two minutes and remains much easier to maintain over time. Still working on this... Commented Jun 27, 2017 at 3:58
  • 1
    ... probably takes more than 2 minutes and the code from your answer is not ready for production, there shouldn't be code that breaks the site when you change configuration. The View @Kevin suggested is only a tool to display view modes in a block. Key point is to have these view modes, which you can customize in preprocess and templates. If you don't want a View, then still use view modes to render whole entities. No php necessary, you can use Twig Tweak to render the current node {{ drupal_entity('node', null, 'custom_view_mode') }}. Then the php is maintained by the module.
    – 4uk4
    Commented Jun 27, 2017 at 7:18
1

I found some code here: What's the best practice to show a node's fields in different regions? Placed this in my [theme_name].theme file:

function THEME_preprocess_region(&$variables) {
  //TODO: change for you region name
  if ($variables['region'] == 'sidebar_right') {
    if ($node = \Drupal::routeMatch()->getParameter('node')) {
      //TODO: change for you node type
      if ($node->getType() == 'article') {
        //If you need a flag for this type
        $variables['is_article'] = TRUE;
        //Here is your field
        $variables['node_field'] = $node->get('field_descricao')->view();
      }
    }
  }
}

With

{% if node_field %} 
  {{ node_field }}
{% endif %}

Placed in region--footer.html.twig (or the equivalent for whatever region you declared above). Obviously, field_descricao is the field you are rendering and node_field is whatever you wish your new variable to be.

This returns a link to my content (which is no longer an image but a node reference). I'm still looking for a way to render the full content, but this is closer than I was.

Any additional insight greatly appreciated!

1

Check out the experimental "Chaos Tools blocks" module, part of Drupal 8.3+. There is no documentation for it at all right now, but it's pretty easy.

  • Enable Chaos Tools blocks module
  • Create a display mode as discussed above. Set it to show only the field you want.
  • In Block Layout -> Place Block, choose Entity View and configure as desired.
  • I had to add a little custom css to keep the title from showing up. Something like .node--view-mode-display_mode_name { header {display: none;" }
1
  • Entity view blocks seem to be in core. I was able to add an entity view block without enabling the chaos tools blocks module. Does anyone know anything about this? The Entity View Blocks are so cool, they give so much control over where you can display content on the page and what the layout is. Amazing! Thanks @joecasey for your answer - really helpful!
    – Ben
    Commented Sep 9, 2021 at 7:57

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.