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, ]); $groupContentEntity->save();
If the problem is just to add an article to a group, the group module directly offers the following function for this. See
public function addContent(ContentEntityInterface $entity, $plugin_id, $values = );
In an example