1

An admin who wants to relate a node to a group can go to the group and then create a node from there (/group/{id}/node/create) . The node is then assigned to the group. Or she can assign a already created node (/group/{id}/content/add)

What I need is a way to relate a node to one or multiple groups directly when creating it. Is there a module to do this or some documentation to get me started in developing it?

1 Answer 1

2

There is no module that offers this out of the box as far as I know. You'd need some custom programming. I don't know what exactly you need, but here's something that might help to get you started.

Let's assume you want to programmatically relate a node/content to one/multiple groups as soon as the node is created. You could use a hook hook_ENTITY_TYPE_insert() (hook replaced by your custom module's name and ENTITY_TYPE replaced by the entity type (e.g. node).

Here's an example for article nodes:

use Drupal\node\Entity\Node; 
use Drupal\group\Entity\Group;

/**
* Implements hook_ENTITY_TYPE_insert().
*/
function modulename_node_insert(Node $node) {
  if ($node->bundle() == 'article') {
    $pluginId = 'group_node:article';
    // Here create some logic to load/chose the group id's you want to 
    // add the content to
    // ... 
    foreach ($group_ids as $gid) {
      $group = Group::load($gid);
      $group->addContent($node, $pluginId);
    }
  }
}

This would automatically add the (article) nodes that are created to one or multiple groups. This is just a basic example of how to code the differnt parts. You can however make it as complex as you need. Hope this helps

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.