0

I have a page--front.html.twig file. I can't figure out how to show a specific node title and body summary on the frontpage with only using twig. I know I can create a view of something like that and show it with: {{ drupal_view('viewmachinename', 'blockname') }}, but in my opinion it's a little bit overkill for just showing the title and body of specific node.

Normally I show the current node title and body summary with {{ node.label }} and {{node.body.summary}}, but I don't know how to show the page title and body summary with an other nid.

I try to show the page title and body summary with nid 2 on the frontpage.

2
  • Are you doing this just to learn twig? If not then you can configure which node is shown on the front page in Configuration > System > Basic site settings. Enter /node/2 in the Front page field.
    – prkos
    Jan 23, 2021 at 12:34
  • Yes I know, but page node 2 is not my front page. It has content which should be displayed at the frontpage. Jan 23, 2021 at 12:38

1 Answer 1

1

If you must do this in twig only, you can use the drupal_field() function provided by the Twig Tweak module.

For example, to print the title of node 2:

{{ drupal_field('title', 'node', 2) }}

Or to print the body field for node 2 using the field's configuration from the teaser view mode:

{{ drupal_field('body', 'node', 2, 'teaser') }}

Or to print an image field with custom formatter settings:

{{ drupal_field('field_image', 'node', 2, {type: 'image_url', settings: {image_style: 'large'}}) }}

I'd probably aim to create a custom view mode here and then print the whole view mode as one chunk with the drupal_entity() function. For example:

{{ drupal_entity('node', 2, 'my_custom_view_mode') }}
1

Your Answer

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

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