5

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?

3 Answers 3

1

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);

0

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,
]);
$groupContentEntity->save();
1
  • 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 ? Jun 15, 2017 at 13:14
0

Once installed the type of relationship in the group with the code feels should be sufficient for Drupal 8, 9, 10.

$node = Node::create([
  'type' => 'node_type',
  'title' => 'My title',
  'uid' => $uid,
]);
$node->save();

$group->addRelationship($node, 'group_node:company_access_request');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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