5

I'd like to add my custom content entity to my group types, just like the module's "group node" plugin does already with nodes bundles. Has anyone done this? There does not seem to be much progress other than the documentation request about Document making custom entity types available as content for group types.

1

Here's how I've solved it in the end. Although in my example I'm adding user entities, you can add whatever custom-entity you want, the approach is the same. What I needed was to add users to a group not as members (as already provided by the group module), but as pure user entities (group content). My answer is a basic example. See /group/src/Plugin/GroupContentEnablerInterface.php for a detailed description what methods etc. are available to refine the whole thing. You basically need 2 files

module_name\src\Plugin\GroupContentEnabler\UserEntity.php

<?php

namespace Drupal\module_name\Plugin\GroupContentEnabler;

use Drupal\group\Plugin\GroupContentEnablerBase;
use Drupal\Core\Form\FormStateInterface;

/**
 * Provides a content enabler for user entities.
 * 
 * @GroupContentEnabler(
 *   id = "group_entity_user",
 *   label = @Translation("User"),
 *   description = @Translation("Adds users to groups as pure entities and not as members."),
 *   entity_type_id = "user",
 *   entity_access = TRUE,
 *   pretty_path_key = "user",
 *   reference_label = @Translation("Username"),
 *   reference_description = @Translation("The name of the user (entity) to add to the group (not as a member)"),
 *   deriver = "Drupal\module_name\Plugin\GroupContentEnabler\UserEntityDeriver"
 * )
 */
class UserEntity extends GroupContentEnablerBase {

/**
 * {@inheritdoc}
 * 
 */
 public function defaultConfiguration() {
   $config = parent::defaultConfiguration();
   $config['entity_cardinality'] = 1;
   return $config;
 }

 /**
  * {@inheritdoc}
  */
 public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
   $form = parent::buildConfigurationForm($form, $form_state);
   // Disable the entity cardinality field as the functionality of this module
   // relies on a cardinality of 1. We don't just hide it, though, to keep a UI
   // that's consistent with other content enabler plugins.
   $info = $this->t("This field has been disabled by the plugin to guarantee the functionality that's expected of it.");
   $form['entity_cardinality']['#disabled'] = TRUE;
   $form['entity_cardinality']['#description'] .= '<br /><em>' . $info . '</em>';

   return $form;
 }

 /**
  * {@inheritdoc}
  */
  public function calculateDependencies() {
   return ['module' => ['user']];
  }

}

and the second file module_name\src\Plugin\GroupContentEnabler\UserEntityDeriver.php

<?php

namespace Drupal\module_name\Plugin\GroupContentEnabler;

use Drupal\Component\Plugin\Derivative\DeriverBase;

class UserEntityDeriver extends DeriverBase {

  /**
   * {@inheritdoc}.
   */
   public function getDerivativeDefinitions($base_plugin_definition) {
     // I used to set 'entity_bundle' to 'user' and the autocomplete for the user name never worked.
     // Checking a user with devel, the bundle is  always empty.
     // So leave it empty and specify the entity_type_id = "user" in the docblock
     // of UserEntity.php
     $this->derivatives['user'] = [
       'entity_bundle' => '',
       'label' => t('Group entity (user)'),
       'description' => t('Adds users to groups as pure entities and not as members.'),
     ] + $base_plugin_definition;
     return $this->derivatives;
    }
}

This can now be installed just like all the other group-content types. The plugin_id of this user-entity group content type is group_entity_user:user. Again, see the group module's source code, as the documentation is good and lot's of additional methods can be used to refine this approach.

As a warning, be very careful when creating these custom plugins as it can cause a lot of trouble when not propery configured/tested. (see my previous question)

  • This approach works great. Thanks @theuni – sgoelz Aug 26 at 11:17

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.