14

So I have a textarea with summary and I want to display the summary before the content. So if I just use {{ content.body }} the text is displayed without the summary.

How can I get the summary ?

{{ content.body.summary }} doesn't seem to do the trick

5 Answers 5

5

In your THEME.theme file you can just define it too:

function THEME_preprocess_node(&$variables) {
$variables['node_teaser'] = ['#markup' => $variables['node']->body->summary];
}

And then display it with:

{{ node_teaser }}
27

The only way I managed to solve this is:

if you only want to target the value of the summary, you can target the entity you're on. If you're on a node then you can do this :

node.body.summary

In a paragraph

paragraph.field_body.summary

You can also get the summary front content like this :

content.body['#object'].body.summary

Not great but will do for now.

1
  • 4
    That IS the only way to use the node object, because content.body is the render array of the body field how you configured in the view mode and if you didn't change it, it will render the full view.
    – 4uk4
    Feb 4, 2016 at 11:58
3

To build on what 4k4 wrote (and updated for 2018):

You first choose the way you'd like the field formatted, then you print it.

1) Go to Structure > Content Types > YOUR CONTENT TYPE > Manage Display > YOUR VIEW MODE.

2) Under the "Formatter" column, choose "Summary or Trimmed."

3) Save.

4) In your twig file you should be able to just print {{ body }} (this is what works for me, but correct me if I'm wrong and I'll update).

This works for me using the node in a View block, and should work on the node as well.

1
  • 1
    +1 This the ideal way to trim the content. In this, the site administrator can configure the trim limit as well no need of a developer help. Apr 6, 2020 at 14:58
2

I got it to work by simply using

{{ content.field_description['#items'][0].summary }}

wherein my description field is field_description

1
  • Perfect! it works. {{ content.body['#items'][0].summary }} May 18, 2022 at 7:14
0

It depend on how you have configure the body field. By default, Drupal has content.body; however, many Drupal developers avoid using it "as it comes" because its particular process may cause issues. If you go to Structure > Content Types > YOUR CONTENT TYPE, you can see the name of your field. On Structure > Content Types > YOUR CONTENT TYPE > Manage Display > YOUR VIEW MODE, you can change how your field is rendered and add different view modes to use for specific use cases. Finally, if you haven't been able to find a solution for your project, you could use devel or kint debug to see your project configuration and find the correct answer.

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.