“Fallen for” Drupal 8 Group Module's principle of using entities and entities relations (as opposed to node “knots”) for handling groups? For the entire concept behind it: sectioning your Drupal site into multiple “micro-sites”, where users having different sets of permissions gain access to certain “micro-sites” only?

A "seductive" entity API-powered idea indeed!

And here you are now: pulling your hair out to find a way to link your user groups with their corresponding content (nodes). To list related group content!

How you do that?

How do you filter content and relate it to the group that it belongs to? Keep reading...
 

But First: A Few Words About The Drupal 8 Group Module

As you must already know, the Group module was born as a solution to all the limitations that the Organic Groups module, its older “rival”, presented. A major drawback being, for instance, the fact that it provided no API.

No API to rely on for configuring a site's role and permission system and content types across its ecosystem of user groups.

And before we point out its most significant strong points, allow us to list and to briefly detail a few key concepts to fully understand how this module works:
 

  • group type: a fieldable entity (common pitfall: do not take user-defined groups for nodes!)

    group (user) membership: a “group type” fieldable entity specific to a given group, incorporated through a plugin; in other words: each “Group type” can feature multiple fields that describe their user memberships 
     
  • the Group Node module: provides “Group type”-specific fieldable entities which describe to what level a group is connected to the nodes of a given content
     

Now that we've shed some light onto these key notions, let us highlight to you the Drupal 8 Group module's key advantages over Organic Groups:
 

  1. self-contained admin, great UI: everything you need for configuring your groups' functionalities is right there, in your admin menu, no need to delve into a dozen of pages for adding your groups, your group types etc.; simply look for the Group section (neighboring the “People” one)
     
  2. improved data model: Group relies on group dedicated entities, opening up a whole new era for handling groups of users, groups of content...
     
  3. “pluggable” architecture: take the “Group Node Module, for instance, that the Drupal 8 Group module ships with; it enables you to integrate nodes into a given group and to share access to private content among that group's members only
     

Before We Dig Into It: How Do You Programmatically Add New Content To Your Group?

Put yourself into this Drupal developer's shoes:

He/she has just created an Article node and now he's facing the challenge of programmatically adding it to a pre-configured group on his website."

How does he solve the “problem”? By using the addContent() method, which leverages the following function added to the Group module: Group::addContent(ContentEntityInterface $entity, $plugin_id, $values = []) 

Note:

  • $plugin_id: it stands for 'group_node:YOUR_NODE_TYPE_HERE'
  • $values: add them only if you want to fill in the fields on the node-to-group relation entity
     

Now What If You Need to Bulk Add Content To Your Group?

That's right? What if you had to add multiple nodes to one of the groups on your Drupal site relying exclusively on the Drupal 8 Group module's capabilites?

Basically your key objective then would be adding a group content entity to each node connected to your Group.

Here are the steps to take:
 

  1. Make certain that you have a GroupType
     
  2. Check that this GroupType has all the proper plugins enabled (GroupType::installContentPlugin(); read deprecation notice)
     
  3. Put together a group of said GroupType
     
  4. Bulk add those nodes using: $group->addContent($node, 'group_node:NODE_TYPE_HERE');

          
Tip: do rely on the API at hand, GroupInterface::addContent(), for this!

Or, if you prefer a shorter version, one that doesn't “constrain” you to use the API,  here's a three-step migration process to get inspired by:
 

  1. Ensure that a group does exist, first things first
     
  2. (Bulk) Migrate your nodes
     
  3. Manually put together your GroupContent entities
     

And Finally: This Is How You Show Related Group Content

Or, better said “how you create a view for showing that content” using the Drupal 8 Group module.

Before we dig into the step-by-step guide, we'd like to highlight the fact that:
 

  1. you'll be using “node id” as a contextual filter
     
  2. the process requires 2 types of Views relationships: Contextual Node > Parent Group and Parent Group > Nodes in group
     

And here are the promised steps:
 

  1. Create your content view (it should have at least one block)
     
  2. Under your View's relationships choose “Content: Parent group” and then click the “Require this relationship box”; you'll now see new relationships listed in the dropdown menu
     
  3. Next, from the same dropdown menu select the following relationship: "Group: Node entities", then hit the “Require this relationship” box
     
  4. Then it's time to define your contextual filter: select “Content NID” from the dropdown menu, hit the “Provide default value” box, then pick “Content ID from URL” from the next dropdown unfolding there
     
  5. Now it's time you defined the “Group Node” relationship (under Format more precisely) for every single rendered entity, content and/or field (according to your own selections)
     
  6. Navigate to /admin/structure/block and select the region of the page that you want this block to be displayed on
     

In short: all there's left for you to do now is simply display nodes from the second relationship that you've defined and (if you want to) filter them by creation date, content type, whatever criteria you'd like.

And this is how you do it! How you show related group content using the Drupal 8 Group module's functionalities! Do let us know if you encounter any (other) roadblocks as you're putting this advice into practice!

Development

We do Drupal development

Go to our Drupal page!

Visit page!

Browse cities

Recommended Stories

Drupal Commerce: Building Powerful E-commerce Solutions
E-commerce sales are projected to reach $6.3 trillion globally by the end of 2024. With online shopping… (Read more)
10 Minutes /
Choosing the Right Drupal Distribution: A Guide for Businesses
Choosing the perfect Drupal distribution can feel like navigating a maze—especially for businesses and government… (Read more)
10 minutes /
How OPTASY Creates Client-Centric Drupal Experiences
Agile methodology has revolutionized the way web development projects are executed, particularly for complex… (Read more)
7 minutes /