Theme colour switcher

Print entity group field value in node.html.twig and configure display modes

Anyone who has read this blog before knows that I love the Drupal Group module. It has a companion module, Entity Group Field (not yet stable), which makes it even more useful. This adds a field to entities which allows you to add them to groups on their edit form, rather than having to go separately to the group to add them.

Here's how to print the rendered value of that field in node.html.twig (something similar should work for other entities added to groups using this module, you'll just change the base variable from "content" to whatever it is for that entity, e.g. user for user.html.twig):

{{ content.entitygroupfield }}

You can change the options for how the field is displayed in the entity's "manage display" settings. The options are: rendered entity, parent group rendered entity, parent group ID, and parent group label. For rendered entity and parent group rendered entity, you have the option to choose what display mode you use.

This might be a bit complicated and not so intuitive, but it's not once you understand how the Group module works.

Rendered entity is the relationship between the entity and the Group(s) it is in, also known as "Group content". This relationship is its own entity in the world of Drupal and can have its own fields, permissions, templates, etc.

Parent group rendered entity is the Group the content is in, itself.

To create and configure display modes other than "default" for either of these options, you will have to first add new view modes. To do that, go to /admin/structure/display-modes/view, and select "Add view mode". To get a new display mode for "Parent group rendered entity", you will select "Group" as the type of view mode you want to add (/admin/structure/display-modes/view/add/group).

To add a new display mode for "Rendered entity", you will add a new "Group content" view mode (/admin/structure/display-modes/view/add/group_content).

After adding your view modes, you will need to activate them for the Group or Group content. To do this for the Group, go to /admin/group/types/manage/[group-type-name]/display. For Group content, go to /admin/group/types/manage/[group-type-name]/content, and for the group content you're targeting, select "Manage display". For both, you will need to go to the bottom of the form under "Custom display settings: Use custom display settings for the following view modes" and select the view mode(s) you want to use. You can then customize them, adding and reordering fields and choosing other display options.


Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.