Print node field in twig individually with custom div structure

Created on 6 September 2023, over 1 year ago
Updated 22 June 2024, 6 months ago

Hello everyone,

I want render my node field with custom div structure. For this I created twig with entity-print--node--content_type.html.twig. When render field like this {{ content.field_machine_name }} but in this case nothing showing. if I add defautl code

<html>
<head>
  <meta charset="utf-8">
  <title>{{ title }}</title>
  {{ entity_print_css }}
</head>
<body>
<div class="page">
    {{ content }}
</div>
</body>
</html>

In this case it's showing everything.

One more issue if we have image in field it's not showing in both case.

Feature request
Status

Needs work

Version

2.13

Component

Documentation

Created by

🇮🇳India pankajlogical Jaipur

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @pankajlogical
  • Assigned to viren18febS
  • 🇮🇳India viren18febS

    Checking the issue!

  • Status changed to Needs review over 1 year ago
  • 🇮🇳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 6 months ago
  • 🇬🇧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!

Production build 0.71.5 2024