You are here

class GroupMembership in Group 2.0.x

Same name in this branch
  1. 2.0.x src/GroupMembership.php \Drupal\group\GroupMembership
  2. 2.0.x src/Plugin/Group/Relation/GroupMembership.php \Drupal\group\Plugin\Group\Relation\GroupMembership

Provides a group relation plugin for users as members.

Plugin annotation


@GroupRelation(
  id = "group_membership",
  label = @Translation("Group membership"),
  description = @Translation("Adds users to groups as members."),
  entity_type_id = "user",
  pretty_path_key = "member",
  reference_label = @Translation("User"),
  reference_description = @Translation("The user you want to make a member"),
  enforced = TRUE,
  admin_permission = "administer members"
)

Hierarchy

Expanded class hierarchy of GroupMembership

File

src/Plugin/Group/Relation/GroupMembership.php, line 29

Namespace

Drupal\group\Plugin\Group\Relation
View source
class GroupMembership extends GroupRelationBase {

  /**
   * {@inheritdoc}
   */
  public function getGroupOperations(GroupInterface $group) {
    $account = \Drupal::currentUser();
    $operations = [];
    if ($group
      ->getMember($account)) {
      if ($group
        ->hasPermission('leave group', $account)) {
        $operations['group-leave'] = [
          'title' => $this
            ->t('Leave group'),
          'url' => new Url('entity.group.leave', [
            'group' => $group
              ->id(),
          ]),
          'weight' => 99,
        ];
      }
    }
    elseif ($group
      ->hasPermission('join group', $account)) {
      $operations['group-join'] = [
        'title' => $this
          ->t('Join group'),
        'url' => new Url('entity.group.join', [
          'group' => $group
            ->id(),
        ]),
        'weight' => 0,
      ];
    }
    return $operations;
  }

  /**
   * {@inheritdoc}
   */
  public function getGroupOperationsCacheableMetadata() {

    // We cannot use the user.is_group_member:%group_id cache context for the
    // join and leave operations, because they end up in the group operations
    // block, which is shown for most likely every group in the system. Instead,
    // we cache per user, meaning the block will be auto-placeholdered in most
    // set-ups.
    // @todo With the new VariationCache, we can use the above context.
    $cacheable_metadata = new CacheableMetadata();
    $cacheable_metadata
      ->setCacheContexts([
      'user',
    ]);
    return $cacheable_metadata;
  }

  /**
   * {@inheritdoc}
   */
  public function getEntityReferenceSettings() {
    $settings = parent::getEntityReferenceSettings();
    $settings['handler_settings']['include_anonymous'] = FALSE;
    return $settings;
  }

  /**
   * {@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 group relation 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;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property
DependencySerializationTrait::$_serviceIds protected property
DependencySerializationTrait::__sleep public function 2
DependencySerializationTrait::__wakeup public function 2
GroupMembership::buildConfigurationForm public function Form constructor. Overrides GroupRelationBase::buildConfigurationForm
GroupMembership::defaultConfiguration public function Gets default configuration for this plugin. Overrides GroupRelationBase::defaultConfiguration
GroupMembership::getEntityReferenceSettings public function Returns a list of entity reference field settings. Overrides GroupRelationBase::getEntityReferenceSettings
GroupMembership::getGroupOperations public function Provides a list of operations for a group. Overrides GroupRelationBase::getGroupOperations
GroupMembership::getGroupOperationsCacheableMetadata public function Provides the cacheable metadata for this plugin's group operations. Overrides GroupRelationBase::getGroupOperationsCacheableMetadata
GroupRelationBase::$groupTypeId protected property The ID of group type this plugin was instantiated for.
GroupRelationBase::$_permissions private property Backwards compatible permission array.
GroupRelationBase::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides DependentPluginInterface::calculateDependencies 1
GroupRelationBase::definesEntityAccess public function Returns whether this plugin defines entity access. Overrides GroupRelationInterface::definesEntityAccess
GroupRelationBase::getConfiguration public function Gets this plugin's configuration. Overrides ConfigurableInterface::getConfiguration
GroupRelationBase::getContentLabel public function Retrieves the label for a piece of group content. Overrides GroupRelationInterface::getContentLabel
GroupRelationBase::getContentTypeConfigId public function Returns a safe, unique configuration ID for a group content type. Overrides GroupRelationInterface::getContentTypeConfigId
GroupRelationBase::getContentTypeDescription public function Returns the administrative description for a group content type. Overrides GroupRelationInterface::getContentTypeDescription
GroupRelationBase::getContentTypeLabel public function Returns the administrative label for a group content type. Overrides GroupRelationInterface::getContentTypeLabel
GroupRelationBase::getDescription public function Returns the administrative description for the plugin. Overrides GroupRelationInterface::getDescription
GroupRelationBase::getEntityBundle public function Returns the entity bundle the plugin supports. Overrides GroupRelationInterface::getEntityBundle
GroupRelationBase::getEntityCardinality public function Returns the amount of times the same content can be added to a group. Overrides GroupRelationInterface::getEntityCardinality
GroupRelationBase::getEntityReferenceDescription public function Returns the description for the entity reference field. Overrides GroupRelationInterface::getEntityReferenceDescription
GroupRelationBase::getEntityReferenceLabel public function Returns the label for the entity reference field. Overrides GroupRelationInterface::getEntityReferenceLabel
GroupRelationBase::getEntityType protected function Returns the entity type definition the plugin supports.
GroupRelationBase::getEntityTypeId public function Returns the entity type ID the plugin supports. Overrides GroupRelationInterface::getEntityTypeId
GroupRelationBase::getGroupCardinality public function Returns the amount of groups the same content can be added to. Overrides GroupRelationInterface::getGroupCardinality
GroupRelationBase::getGroupType public function Returns the group type the plugin was instantiated for. Overrides GroupRelationInterface::getGroupType
GroupRelationBase::getGroupTypeId public function Returns the ID of the group type the plugin was instantiated for. Overrides GroupRelationInterface::getGroupTypeId
GroupRelationBase::getLabel public function Returns the administrative label for the plugin. Overrides GroupRelationInterface::getLabel
GroupRelationBase::getOperations public function Provides a list of operations for the group relation plugin. Overrides GroupRelationInterface::getOperations
GroupRelationBase::getPrettyPathKey public function Returns the pretty path key for use in path aliases. Overrides GroupRelationInterface::getPrettyPathKey
GroupRelationBase::getProvider public function Returns the plugin provider. Overrides GroupRelationInterface::getProvider
GroupRelationBase::isCodeOnly public function Returns whether this plugin can only be (un)installed through code. Overrides GroupRelationInterface::isCodeOnly
GroupRelationBase::isEnforced public function Returns whether this plugin is always on. Overrides GroupRelationInterface::isEnforced
GroupRelationBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
GroupRelationBase::submitConfigurationForm public function Only override this function if you need to do something specific to the submitted data before it is saved as configuration on the plugin. The data gets saved on the plugin in \Drupal\group\Entity\Form\GroupContentTypeForm. Overrides PluginFormInterface::submitConfigurationForm
GroupRelationBase::validateConfigurationForm public function Form validation handler. Overrides PluginFormInterface::validateConfigurationForm
GroupRelationBase::__construct public function Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase::__construct
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PluginBase::$configuration protected property Configuration information passed into the plugin. 1
PluginBase::$pluginDefinition protected property The plugin implementation definition. 1
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition 2
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.