Hook into Group module I'm creating a module for Drupal 8 that will auto fill some content in a add content page. Now I need to have a way to group different contents in one group. I found the Group module that does the job, but I cannot find a way to hook into it. Can someone help me with this?

I want to have a few check boxes, for every group, in my add content page. When the user select a group and create the content it will be added to that group. How can I create the checkboxes for the groups where the users has access for. Also how can I auto add the content to the selected groups?
Sure it is  possible but you'll have to write quite a bunch of custom code to accomplish this.

First let's take a look at how a node belongs to a group. This is done through the GroupContent entity.



Before we can actually relate a node to a group, we'd have to install the entity to the group type. This can be done by going to:

Groups -> Group types -> Set available content (can be found via the Operations action button).



First we want to add the possibility to choose one or many groups the node being created can belong to. So we'll have to determine if the node we are trying to add is first installed and then get the groups.

Here is a very simplified example on how to do that by using altering the node and then finding out which group types the current node type is installed to, finally getting the groups and exposing them as checkboxes.

It does not currently care about the user actually having access to that group yet. 

/**
 * Implements hook_form_FORM_ID_alter().
 */
function custom_group_form_node_form_alter(&amp;$form, FormStateInterface $form_state) {
  $node = $form_state-&gt;getFormObject()-&gt;getEntity();

  $groupPluginManager = \Drupal::service('plugin.manager.group_content_enabler');

  // Get the group types this node type is installed to.
  $plugin_id = 'group_node:' . $node-&gt;getType();
  $groupTypes = $groupPluginManager-&gt;getGroupContentTypeIds($plugin_id);

  // This node is not installed to any group types.
  if (empty($groupTypes)) {
    return;
  }

  // Normalize group type ids to just the type name
  $groupTypes = array_map(function($groupType) {
    return explode('-', $groupType)[0];
  }, $groupTypes);

  // Get groups...
  // @TODO: Sort, group the groups to its group type?
  $query = \Drupal::entityQuery('group');
  $query-&gt;condition('type', $groupTypes, 'IN');
  $groups_ids = $query-&gt;execute();

  // ugh... no groups though?
  if (empty($groups_ids)) {
    return;
  }

  // Create a checkboxes with all groups without any grouping of type
  $groups = \Drupal\group\Entity\Group::loadMultiple($groups_ids);
  $options = [];

  foreach ($groups as $group) {
    $options[$group-&gt;id()] = $group-&gt;label();
  }

  $form['groups'] = [
    '#type' =&gt; 'checkboxes',
    '#title' =&gt; 'Attach to group',
    '#options' =&gt; $options,
  ];



 foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' &amp;&amp; isset($form['actions'][$action]['#type']) &amp;&amp; $form['actions'][$action]['#type'] === 'submit') {
      $form['actions'][$action]['#submit'][] = 'custom_group_submit_handler';
    }
  }

}


So we have a list of groups on the node form, how do we actually relate them to the checked groups? We added a submit handler custom_group_submit_handler Let's write that to, again very simplified:

function custom_group_submit_handler(&amp;$form, FormStateInterface $form_state) {

  $node = $form_state-&gt;getFormObject()-&gt;getEntity();
  $groups = $form_state-&gt;getValue('groups');

  // Iterate through each submitted group
  foreach ($groups as $group) {
    $groupEntity = Group::load($group);

    // Assemble the type.
    $type = $groupEntity-&gt;getGroupType()-&gt;id() . '-group_node-' . $node-&gt;getType();

    // Create the relation.
    $groupContent = GroupContent::create([
      'gid' =&gt; $group,
      'entity_id' =&gt; $node-&gt;id(),
      'type' =&gt; $type,
    ]);
    $groupContent-&gt;save();
  }
}


This took a bit longer to write than I expected, so therefore there are some details you will have to implement yourself, like:


Take group access and permission under consideration
You'd most likely want to clean up the code and move to a more object-oriented approach
You want to handle both add/edit and deletion for the node relation to a group.
You want to make sure the node is not already related to the group.


etc.

Hopefully this is an start to get you further on your objective!