1

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?

4

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.

enter image description here

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

enter image description here

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(&$form, FormStateInterface $form_state) {
  $node = $form_state->getFormObject()->getEntity();

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

  // Get the group types this node type is installed to.
  $plugin_id = 'group_node:' . $node->getType();
  $groupTypes = $groupPluginManager->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->condition('type', $groupTypes, 'IN');
  $groups_ids = $query->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->id()] = $group->label();
  }

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



 foreach (array_keys($form['actions']) as $action) {
    if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $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(&$form, FormStateInterface $form_state) {

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

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

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

    // Create the relation.
    $groupContent = GroupContent::create([
      'gid' => $group,
      'entity_id' => $node->id(),
      'type' => $type,
    ]);
    $groupContent->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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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