I have a module that periodically creates new nodes and saves them programmatically. I want to assign new nodes to different groups depending on an ID that I choose, but after searching I couldn't find how to do that! These nodes are of a content type that I have already marked as group content for my group type.

Comments

RawanMoukalled created an issue. See original summary.

kristiaanvandeneynde’s picture

Status: Active » Fixed

I just added a new function for this: Group::addContent(ContentEntityInterface $entity, $plugin_id, $values = [])

Where $plugin_id is more than likely 'group_node:YOUR_NODE_TYPE_HERE' and $values can be empty unless you want to populate fields on the node-to-group relation entity (GroupContent entity).

kristiaanvandeneynde’s picture

P.S.: This will be in 8.x-1.0-beta1, you can try it out using the dev release.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Anonymous’s picture

I'm trying to add an already existing node to a group and i'm getting sql error with using this addContent() method.

Drupal\Core\Entity\EntityStorageException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '8644-2-gnode:article-fi' for key 'PRIMARY': INSERT INTO {node_access} (nid, langcode, fallback, realm, gid, grant_view, grant_update, grant_delete) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2, :db_insert_placeholder_3, :db_insert_placeholder_4, :db_insert_placeholder_5, :db_insert_placeholder_6, :db_insert_placeholder_7), (:db_insert_placeholder_8, :db_insert_placeholder_9, :db_insert_placeholder_10, :db_insert_placeholder_11, :db_insert_placeholder_12, :db_insert_placeholder_13, :db_insert_placeholder_14, :db_insert_placeholder_15), (:db_insert_placeholder_16, :db_insert_placeholder_17, :db_insert_placeholder_18, :db_insert_placeholder_19, :db_insert_placeholder_20, :db_insert_placeholder_21, :db_insert_placeholder_22, :db_insert_placeholder_23), (:db_insert_placeholder_24, :db_insert_placeholder_25, :db_insert_placeholder_26, :db_insert_placeholder_27, :db_insert_placeholder_28, :db_insert_placeholder_29, :db_insert_placeholder_30, :db_insert_placeholder_31), (:db_insert_placeholder_32, :db_insert_placeholder_33, :db_insert_placeholder_34, :db_insert_placeholder_35, :db_insert_placeholder_36, :db_insert_placeholder_37, :db_insert_placeholder_38, :db_insert_placeholder_39); Array ( [:db_insert_placeholder_0] => 8644 [:db_insert_placeholder_1] => fi [:db_insert_placeholder_2] => 1 [:db_insert_placeholder_3] => gnode:article [:db_insert_placeholder_4] => 2 [:db_insert_placeholder_5] => 1 [:db_insert_placeholder_6] => 1 [:db_insert_placeholder_7] => 1 [:db_insert_placeholder_8] => 8644 [:db_insert_placeholder_9] => fi [:db_insert_placeholder_10] => 1 [:db_insert_placeholder_11] => gnode_author:1:article [:db_insert_placeholder_12] => 2 [:db_insert_placeholder_13] => 1 [:db_insert_placeholder_14] => 1 [:db_insert_placeholder_15] => 1 [:db_insert_placeholder_16] => 8644 [:db_insert_placeholder_17] => fi [:db_insert_placeholder_18] => 1 [:db_insert_placeholder_19] => gnode:article [:db_insert_placeholder_20] => 2 [:db_insert_placeholder_21] => 1 [:db_insert_placeholder_22] => 1 [:db_insert_placeholder_23] => 1 [:db_insert_placeholder_24] => 8644 [:db_insert_placeholder_25] => fi [:db_insert_placeholder_26] => 1 [:db_insert_placeholder_27] => gnode_author:1:article [:db_insert_placeholder_28] => 2 [:db_insert_placeholder_29] => 1 [:db_insert_placeholder_30] => 1 [:db_insert_placeholder_31] => 1 [:db_insert_placeholder_32] => 8644 [:db_insert_placeholder_33] => fi [:db_insert_placeholder_34] => 1 [:db_insert_placeholder_35] => gnode_bypass [:db_insert_placeholder_36] => 1986 [:db_insert_placeholder_37] => 1 [:db_insert_placeholder_38] => 1 [:db_insert_placeholder_39] => 1 ) in Drupal\Core\Entity\Sql\SqlContentEntityStorage->save() (line 777 of core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php).

The code i'm using is quite simple so there might be something in that:

    // Load whole entity
    $entity = \Drupal::entityTypeManager()->getStorage("node")->load($node->id());

    // Load group and add $entity to group
    $group = \Drupal::entityTypeManager()->getStorage('group')->load($groupid);
    $group->addContent($entity, 'group_node:article');

Maybe someone could help? I've tried all kinds of stuff but nothing works.

nit3ch’s picture

I am having the same problem when i am trying to use addContent() method in hook_ENTITY_TYPE_insert, Otherwise it is working fine.

docans’s picture

The Solution in #5 only works if you have one 1 node you want to add to just the "article content type".

how will you modify this to work for multiple content type. for example how will you modify this code to work for both "article" and "basic_page" node content types

thanks

taylormade415’s picture

StatusFileSize
new396.58 KB

my plugin is accessible via /admin/group/content/manage/groups-group_node-playlists and fully configured.

yet when attempting any of these

$type = $group->getGroupType()->id() . '-group_node-' . $node->getType();
$type = 'group_node-' . $node->getType();
$group->addContent($node, $type);

i get

Drupal\Component\Plugin\Exception\PluginNotFoundException: Plugin ID 'groups-group_node-playlists' was not found. in Drupal\Core\Plugin\DefaultLazyPluginCollection->initializePlugin() (line 79 of /Users/macbookpro/../drupal8/core/lib/Drupal/Core/Plugin/DefaultLazyPluginCollection.php).

breakpoints lead to $plugin = $group->getGroupType()->getContentPlugin($plugin_id); in modules/group/src/Entity/Storage/GroupContentStorage.php::createForEntityInGroup

taylormade415’s picture

my issue was the separator should be a colon. only this format worked:

$type = 'group_node:' . $node->getType();

afsch’s picture

To check if the content was already added to a group you can try:

    $pluginId = 'group_node:' . $node->getType();
    $group = Group::load($gid);
    $relation = $group->getContentByEntityId($pluginId, $node->id());
    if (!$relation) {
      $group->addContent($node, $pluginId);
    }
chems’s picture

Hello,
I create a custom entity content without bundle entity type id is 'project' , with project i create a plugin group with id 'group_project'.
When i try to addContent to group using
addContent(ContentEntityInterface $entity, $plugin_id, $values = [])
addContent($entity, 'group_project') the error was plugin_id not found,
i test with addContent($entity, 'group_project:project') the error was entity not support bundle

The Derivate definition :

        $this->derivatives['project'] = [
                'entity_bundle' => '', //no bundle
                'label' => t('Group entity (project)'),
                'description' => t('Adds users to groups as pure entities.'),
            ] + $base_plugin_definition;
        return $this->derivatives;

what should i do ?? thx for help !

chems’s picture

Version: 8.x-1.0-alpha7 » 8.x-1.0-rc2
dangur’s picture

@chems you need a content enabler plugin for your entity type à la https://drupal.stackexchange.com/questions/259674/how-to-add-custom-enti...