class GroupMembership in Group 8
Same name in this branch
- 8 src/GroupMembership.php \Drupal\group\GroupMembership
- 8 src/Plugin/GroupContentEnabler/GroupMembership.php \Drupal\group\Plugin\GroupContentEnabler\GroupMembership
Provides a content enabler for users.
Plugin annotation
@GroupContentEnabler(
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,
handlers = {
"access" = "Drupal\group\Plugin\GroupContentAccessControlHandler",
"permission_provider" = "Drupal\group\Plugin\GroupMembershipPermissionProvider",
},
admin_permission = "administer members"
)
Hierarchy
- class \Drupal\Component\Plugin\PluginBase implements DerivativeInspectionInterface, PluginInspectionInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
- class \Drupal\group\Plugin\GroupContentEnablerBase implements GroupContentEnablerInterface
- class \Drupal\group\Plugin\GroupContentEnabler\GroupMembership
- class \Drupal\group\Plugin\GroupContentEnablerBase implements GroupContentEnablerInterface
- class \Drupal\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of GroupMembership
File
- src/
Plugin/ GroupContentEnabler/ GroupMembership.php, line 37
Namespace
Drupal\group\Plugin\GroupContentEnablerView source
class GroupMembership extends GroupContentEnablerBase {
/**
* {@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 postInstall() {
// Only create config objects while config import is not in progress.
if (!\Drupal::isConfigSyncing()) {
$group_content_type_id = $this
->getContentTypeConfigId();
// Add the group_roles field to the newly added group content type. The
// field storage for this is defined in the config/install folder. The
// default handler for 'group_role' target entities in the 'group_type'
// handler group is GroupTypeRoleSelection.
FieldConfig::create([
'field_storage' => FieldStorageConfig::loadByName('group_content', 'group_roles'),
'bundle' => $group_content_type_id,
'label' => $this
->t('Roles'),
'settings' => [
'handler' => 'group_type:group_role',
'handler_settings' => [
'group_type_id' => $this
->getGroupTypeId(),
],
],
])
->save();
// Build the 'default' display ID for both the entity form and view mode.
$default_display_id = "group_content.{$group_content_type_id}.default";
// Build or retrieve the 'default' form mode.
if (!($form_display = EntityFormDisplay::load($default_display_id))) {
$form_display = EntityFormDisplay::create([
'targetEntityType' => 'group_content',
'bundle' => $group_content_type_id,
'mode' => 'default',
'status' => TRUE,
]);
}
// Build or retrieve the 'default' view mode.
if (!($view_display = EntityViewDisplay::load($default_display_id))) {
$view_display = EntityViewDisplay::create([
'targetEntityType' => 'group_content',
'bundle' => $group_content_type_id,
'mode' => 'default',
'status' => TRUE,
]);
}
// Assign widget settings for the 'default' form mode.
$form_display
->setComponent('group_roles', [
'type' => 'options_buttons',
])
->save();
// Assign display settings for the 'default' view mode.
$view_display
->setComponent('group_roles', [
'label' => 'above',
'type' => 'entity_reference_label',
'settings' => [
'link' => 0,
],
])
->save();
}
}
/**
* {@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;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | An array of entity type IDs keyed by the property name of their storages. | |
DependencySerializationTrait:: |
protected | property | An array of service IDs keyed by property name used for serialization. | |
DependencySerializationTrait:: |
public | function | 1 | |
DependencySerializationTrait:: |
public | function | 2 | |
GroupContentEnablerBase:: |
protected | property | The ID of group type this plugin was instantiated for. | |
GroupContentEnablerBase:: |
private | property | Backwards compatible permission array. | |
GroupContentEnablerBase:: |
public | function |
Calculates dependencies for the configured plugin. Overrides DependentPluginInterface:: |
1 |
GroupContentEnablerBase:: |
public | function |
Checks access to an operation on a given group content entity. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Performs access check for the create operation. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Performs access check for the create target entity operation. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns whether this plugin defines entity access. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
protected | function | Performs access check for the delete operation. | |
GroupContentEnablerBase:: |
public | function |
Gets this plugin's configuration. Overrides ConfigurableInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Retrieves the label for a piece of group content. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns a safe, unique configuration ID for a group content type. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the administrative description for a group content type. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the administrative label for a group content type. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the administrative description for the plugin. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the entity bundle the plugin supports. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the amount of times the same content can be added to a group. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the description for the entity reference field. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the label for the entity reference field. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
protected | function | Returns the entity type definition the plugin supports. | |
GroupContentEnablerBase:: |
public | function |
Returns the entity type ID the plugin supports. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the amount of groups the same content can be added to. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
protected | function | Provides permissions for the group content entity; i.e. the relationship. | |
GroupContentEnablerBase:: |
public | function |
Returns the group type the plugin was instantiated for. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the ID of the group type the plugin was instantiated for. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the administrative label for the plugin. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Provides a list of operations for the content enabler plugin. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Provides a list of group permissions the plugin exposes. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the pretty path key for use in path aliases. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns the plugin provider. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
protected | function | Provides permissions for the actual entity being added to the group. | |
GroupContentEnablerBase:: |
public | function |
Returns whether this plugin can only be (un)installed through code. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Returns whether this plugin is always on. Overrides GroupContentEnablerInterface:: |
|
GroupContentEnablerBase:: |
public | function |
Sets the configuration for this plugin instance. Overrides ConfigurableInterface:: |
|
GroupContentEnablerBase:: |
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:: |
|
GroupContentEnablerBase:: |
protected | function | Performs access check for the update operation. | |
GroupContentEnablerBase:: |
public | function |
Form validation handler. Overrides PluginFormInterface:: |
|
GroupContentEnablerBase:: |
protected | function | Performs access check for the view operation. | |
GroupContentEnablerBase:: |
public | function |
Constructs a \Drupal\Component\Plugin\PluginBase object. Overrides PluginBase:: |
|
GroupMembership:: |
public | function |
Form constructor. Overrides GroupContentEnablerBase:: |
|
GroupMembership:: |
public | function |
Gets default configuration for this plugin. Overrides GroupContentEnablerBase:: |
|
GroupMembership:: |
public | function |
Returns a list of entity reference field settings. Overrides GroupContentEnablerBase:: |
|
GroupMembership:: |
public | function |
Provides a list of operations for a group. Overrides GroupContentEnablerBase:: |
|
GroupMembership:: |
public | function |
Provides the cacheable metadata for this plugin's group operations. Overrides GroupContentEnablerBase:: |
|
GroupMembership:: |
public | function |
Runs tasks after the group content type for this plugin has been created. Overrides GroupContentEnablerBase:: |
|
MessengerTrait:: |
protected | property | The messenger. | 29 |
MessengerTrait:: |
public | function | Gets the messenger. | 29 |
MessengerTrait:: |
public | function | Sets the messenger. | |
PluginBase:: |
protected | property | Configuration information passed into the plugin. | 1 |
PluginBase:: |
protected | property | The plugin implementation definition. | 1 |
PluginBase:: |
protected | property | The plugin_id. | |
PluginBase:: |
constant | A string which is used to separate base plugin IDs from the derivative ID. | ||
PluginBase:: |
public | function |
Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface:: |
|
PluginBase:: |
public | function |
Gets the definition of the plugin implementation. Overrides PluginInspectionInterface:: |
3 |
PluginBase:: |
public | function |
Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface:: |
|
PluginBase:: |
public | function | Determines if the plugin is configurable. | |
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. |