I'm using the Group module. If I have a group type such as Project, and I have a content type such as Article, and I programmatically created a new article node using Node::create(), how can I in the same code add this article as content to a project that I choose?


Given you have the group entity and node entity, you can simply create a GroupContent entity.

$groupEntity = ... The group entity.
$node = ... The node entity.

$type = $type = $groupEntity->getGroupType()->id() . '-group_node-' . $node->getType();

$groupContentEntity = \Drupal\group\Entity\GroupContent::create([
    'gid' => $groupEntity->id(),
    'entity_id' => $node->id(),
    'type' => $type,
  • Could you clarify, is it necessary to create a plugin to get permission for it like here (drupal.org/node/2723911) ? Or I can have a node entity, a group entity and simply add the node to the group without writing the plugin ? And in this case will all users of the group be able to edit content of the node ? – Yakimkin Roman Jun 15 '17 at 13:14

If the problem is just to add an article to a group, the group module directly offers the following function for this. See GroupInterface.php

public function addContent(ContentEntityInterface $entity, $plugin_id, $values = []);

In an example $group->addContent($node, $pluginId);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.