Guidance on including co-authors in Twig

Created on 22 March 2024, 9 months ago

Dear all,

Any guidance how to display co-authors using a Twig template?

Thanks in advance

Mithun A. Sridharan

💬 Support request
Status

Active

Version

1.1

Component

Documentation

Created by

🇩🇪Germany mithun-a-sridharan Heidelberg

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

Comments & Activities

  • Issue created by @mithun-a-sridharan
  • 🇧🇪Belgium dieterholvoet Brussels

    If you have access to the entity (the node variable in node templates), you can get the co-authors with node.co_authors.referencedEntities. For example, you can get the display name of the first co-author with node.co_authors.referencedEntities|first.displayName or node.co_authors.entity.displayName.

    You can also show the co-authors in the view display and access the rendered output with content.co_authors in the node template.

  • 🇩🇪Germany mithun-a-sridharan Heidelberg

    Hi Dieter,

    Thanks for your responsiveness. Much appreciated!

    Is there a mechanism to get all co-authors in the Twig file? The idea here is to create an authors' biography that goes after the content in the blog content type. The biography will include the co-authors' picture and a biography.

    Following your inputs, I tried the following:

    node.co_authors.referencedEntities and node.co_authors.referencedEntities|first.displayName

    This resulted in the following error:

    Twig\Sandbox\SecurityError: Calling "referencedEntities" method on a "Drupal\Core\Field\EntityReferenceFieldItemList" object is not allowed. in Drupal\Core\Template\TwigSandboxPolicy->checkMethodAllowed() (line 174 of themes/custom/rhythm/templates/node/node--nd-blog.html.twig).

    This worked:

    node.co_authors.entity.displayName

    I'm new to Drupal, coming from the WordPress ecosystem.

    Is there an alternative approach you'd recommend?

    Thanks again!

    Mithun A. Sridharan

  • 🇧🇪Belgium dieterholvoet Brussels

    You can fix the SecurityError by adding the following to your settings.php:

    $settings['twig_sandbox_allowed_methods'] = [
        'id',
        'label',
        'bundle',
        'get',
        '__toString',
        'toString',
        'referencedEntities',
    ];
    
  • Status changed to Closed: works as designed 7 months ago
  • 🇩🇪Germany mithun-a-sridharan Heidelberg

    @dieterholvoet,

    It's been a while since I approached you with the issues.

    The suggested fixes resolve the issues I was facing.

    Thank you very much for your support.

    Much appreciated!

    Mithun A. Sridharan

Production build 0.71.5 2024