- Issue created by @pankajlogical
- Assigned to viren18febS
- Status changed to Needs review
about 1 year ago 10:03am 13 September 2023 - 🇮🇳India viren18febS
Hi @pankajlogical
You can use this code for fields values in twig.
{{ content.field_name.value }} - 🇮🇳India Madhu Kumar M E
To render a field's default value in twig file, you would typically use a Twig variable as in
{{ node.field_some_name.value }}
The twig_tweak module greatly simplifies such tasks and can give easier twig statements for field values. we can try this module and play around it.
- 🇬🇧United Kingdom matt b
I have the same issue, I want to style the fields included in the PDF view separately to the entity body. The only thing I can do is manipulate {{ content }}
<div class="content"> <div>Created: {{ content.created.value }}</div> <div>Tags: {{ content.field_tags.value }}</div> <div>Author: {{ content.field_author.value }}</div> <div>Body: {{ content.body.value }}</div> <div>Created: {{ node.created.value }}</div> <div>Tags: {{ node.field_tags.value }}</div> <div>Author: {{ node.field_author.value }}</div> <div>Body: {{ node.body.value }}</div> <div>{{ content }}</div> </div>
Everything returns empty except {{ content }}
I've tried handling this in preprocessing, but it's further complicated by this being on a multilingual site. I should just be able to get to the individual field values.
- 🇬🇧United Kingdom matt b
Would an approach be to subscribe to the print events and add content there somehow?
- Status changed to Needs work
5 months ago 8:56am 22 June 2024 - 🇬🇧United Kingdom matt b
So Madhu Kumar M E in #4 was on the right lines for a work around. I installed the twig_tweak module. I added this to my theme preprocess function:
// pull out nid to use in template for twig tweaks $variables['nid']= \Drupal::routeMatch()->getParameter('entity_id');
and then this works in the template file:
<div>Tags: {{ drupal_field('field_tags', 'node', nid) }}</div> <div>Author: {{ drupal_field('field_author', 'node', nid) }}</div> <div>Body: {{ drupal_field('body', 'node', nid) }}</div> <div>Created: {{ drupal_field('created', 'node', nid) }}</div>
However, this is a workaround. The module should expose the fields in the PDF view mode to us in the same way we can in the node template file - as per this comment extracted from node.html.twig:
* - content: All node items. Use {{ content }} to print them all, * or print a subset such as {{ content.field_example }}. Use * {{ content|without('field_example') }} to temporarily suppress the printing * of a given child element.
I believe this is a feature request, not a support request. Please update the metadata on this issue if you disagree!