abstract class GroupContentEnablerBase in Group 8
Provides a base class for GroupContentEnabler plugins.
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\Core\Plugin\PluginBase uses DependencySerializationTrait, MessengerTrait, StringTranslationTrait
Expanded class hierarchy of GroupContentEnablerBase
See also
\Drupal\group\Annotation\GroupContentEnabler
\Drupal\group\GroupContentEnablerManager
\Drupal\group\Plugin\GroupContentEnablerInterface
6 files declare their use of GroupContentEnablerBase
- EntityTestAsContent.php in tests/
modules/ group_test_plugin/ src/ Plugin/ GroupContentEnabler/ EntityTestAsContent.php - GroupAsContent.php in tests/
modules/ group_test_plugin/ src/ Plugin/ GroupContentEnabler/ GroupAsContent.php - GroupMembership.php in src/
Plugin/ GroupContentEnabler/ GroupMembership.php - GroupNode.php in modules/
gnode/ src/ Plugin/ GroupContentEnabler/ GroupNode.php - NodeAsContent.php in tests/
modules/ group_test_plugin/ src/ Plugin/ GroupContentEnabler/ NodeAsContent.php
File
- src/
Plugin/ GroupContentEnablerBase.php, line 25
Namespace
Drupal\group\PluginView source
abstract class GroupContentEnablerBase extends PluginBase implements GroupContentEnablerInterface {
/**
* The ID of group type this plugin was instantiated for.
*
* @var string
*/
protected $groupTypeId;
/**
* Backwards compatible permission array.
*
* @var array
*/
private $_permissions;
/**
* {@inheritdoc}
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
// Only support setting the group type ID during construction.
if (!empty($configuration['group_type_id'])) {
$this->groupTypeId = $configuration['group_type_id'];
}
// Include the default configuration by calling ::setConfiguration().
$this
->setConfiguration($configuration);
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return $this->pluginDefinition['provider'];
}
/**
* {@inheritdoc}
*/
public function getLabel() {
return $this->pluginDefinition['label'];
}
/**
* {@inheritdoc}
*/
public function getDescription() {
return $this->pluginDefinition['description'];
}
/**
* {@inheritdoc}
*/
public function getEntityTypeId() {
return $this->pluginDefinition['entity_type_id'];
}
/**
* Returns the entity type definition the plugin supports.
*
* @return \Drupal\Core\Entity\EntityTypeInterface
* The entity type definition.
*/
protected function getEntityType() {
return \Drupal::entityTypeManager()
->getDefinition($this
->getEntityTypeId());
}
/**
* {@inheritdoc}
*/
public function getEntityBundle() {
return $this->pluginDefinition['entity_bundle'];
}
/**
* {@inheritdoc}
*/
public function getPrettyPathKey() {
return $this->pluginDefinition['pretty_path_key'];
}
/**
* {@inheritdoc}
*/
public function getGroupCardinality() {
return $this->configuration['group_cardinality'];
}
/**
* {@inheritdoc}
*/
public function getEntityCardinality() {
return $this->configuration['entity_cardinality'];
}
/**
* {@inheritdoc}
*/
public function getGroupType() {
if ($id = $this
->getGroupTypeId()) {
return GroupType::load($id);
}
}
/**
* {@inheritdoc}
*/
public function getGroupTypeId() {
return $this->groupTypeId;
}
/**
* {@inheritdoc}
*/
public function definesEntityAccess() {
return $this->pluginDefinition['entity_access'];
}
/**
* {@inheritdoc}
*/
public function isEnforced() {
return $this->pluginDefinition['enforced'];
}
/**
* {@inheritdoc}
*/
public function isCodeOnly() {
return $this->pluginDefinition['code_only'];
}
/**
* {@inheritdoc}
*/
public function getContentLabel(GroupContentInterface $group_content) {
return $group_content
->getEntity()
->label();
}
/**
* {@inheritdoc}
*/
public function getContentTypeConfigId() {
$preferred_id = $this
->getGroupTypeId() . '-' . str_replace(':', '-', $this
->getPluginId());
// Return a hashed ID if the readable ID would exceed the maximum length.
if (strlen($preferred_id) > EntityTypeInterface::BUNDLE_MAX_LENGTH) {
$hashed_id = 'group_content_type_' . md5($preferred_id);
$preferred_id = substr($hashed_id, 0, EntityTypeInterface::BUNDLE_MAX_LENGTH);
}
return $preferred_id;
}
/**
* {@inheritdoc}
*/
public function getContentTypeLabel() {
return $this
->getGroupType()
->label() . ': ' . $this
->getLabel();
}
/**
* {@inheritdoc}
*/
public function getContentTypeDescription() {
return $this
->getDescription();
}
/**
* {@inheritdoc}
*/
public function getGroupOperations(GroupInterface $group) {
return [];
}
/**
* {@inheritdoc}
*/
public function getGroupOperationsCacheableMetadata() {
return new CacheableMetadata();
}
/**
* {@inheritdoc}
*/
public function getOperations() {
return [];
}
/**
* Provides permissions for the group content entity; i.e. the relationship.
*
* @return array
* An array of group permissions, see ::getPermissions for more info.
*
* @see GroupContentEnablerInterface::getPermissions()
*
* @deprecated in Group 1.0, will be removed before Group 2.0.
*/
protected function getGroupContentPermissions() {
return $this->_permissions;
}
/**
* Provides permissions for the actual entity being added to the group.
*
* @return array
* An array of group permissions, see ::getPermissions for more info.
*
* @see GroupContentEnablerInterface::getPermissions()
*
* @deprecated in Group 1.0, will be removed before Group 2.0.
*/
protected function getTargetEntityPermissions() {
return $this->_permissions;
}
/**
* {@inheritdoc}
*/
public function getPermissions() {
/** @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface $manager */
$manager = \Drupal::service('plugin.manager.group_content_enabler');
// Backwards compatibility layer:
// - Use the declared permission provider if available.
if ($manager
->hasHandler($this->pluginId, 'permission_provider')) {
return $manager
->getPermissionProvider($this->pluginId)
->buildPermissions();
}
// Backwards compatibility layer:
// - Fall back to the default permission provider if none was found.
// - Still call the protected methods so old code can alter the permissions.
/** @var \Drupal\group\plugin\GroupContentPermissionProviderInterface $permission_provider */
$permission_provider = $manager
->createHandlerInstance('Drupal\\group\\Plugin\\GroupContentPermissionProvider', $this->pluginId, $this->pluginDefinition);
$this->_permissions = $permission_provider
->buildPermissions();
$this->_permissions = $this
->getGroupContentPermissions();
if ($this
->definesEntityAccess()) {
$this->_permissions = $this
->getTargetEntityPermissions();
}
return $this->_permissions;
}
/**
* {@inheritdoc}
*/
public function createEntityAccess(GroupInterface $group, AccountInterface $account) {
/** @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface $manager */
$manager = \Drupal::service('plugin.manager.group_content_enabler');
// Backwards compatibility layer:
// - Use the declared access control handler if available.
if ($manager
->hasHandler($this->pluginId, 'access')) {
return $manager
->getAccessControlHandler($this->pluginId)
->entityCreateAccess($group, $account, TRUE);
}
// Backwards compatibility layer:
// - Run the old code if there is no access control handler.
// You cannot create target entities if the plugin does not support it.
if (!$this
->definesEntityAccess()) {
return AccessResult::neutral();
}
$plugin_id = $this
->getPluginId();
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "create {$plugin_id} entity");
}
/**
* {@inheritdoc}
*/
public function createAccess(GroupInterface $group, AccountInterface $account) {
/** @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface $manager */
$manager = \Drupal::service('plugin.manager.group_content_enabler');
// Backwards compatibility layer:
// - Use the declared access control handler if available.
if ($manager
->hasHandler($this->pluginId, 'access')) {
return $manager
->getAccessControlHandler($this->pluginId)
->relationCreateAccess($group, $account, TRUE);
}
// Backwards compatibility layer:
// - Run the old code if there is no access control handler.
$plugin_id = $this
->getPluginId();
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "create {$plugin_id} content");
}
/**
* Performs access check for the view operation.
*
* This method is supposed to be overwritten by extending classes that
* do their own custom access checking.
*
* @param \Drupal\group\Entity\GroupContentInterface $group_content
* The group content for which to check access.
* @param \Drupal\Core\Session\AccountInterface $account
* The user for which to check access.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
protected function viewAccess(GroupContentInterface $group_content, AccountInterface $account) {
$group = $group_content
->getGroup();
$plugin_id = $this
->getPluginId();
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "view {$plugin_id} content");
}
/**
* Performs access check for the update operation.
*
* This method is supposed to be overwritten by extending classes that
* do their own custom access checking.
*
* @param \Drupal\group\Entity\GroupContentInterface $group_content
* The group content for which to check access.
* @param \Drupal\Core\Session\AccountInterface $account
* The user for which to check access.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
protected function updateAccess(GroupContentInterface $group_content, AccountInterface $account) {
$group = $group_content
->getGroup();
$plugin_id = $this
->getPluginId();
// Allow members to edit their own group content.
if ($group_content
->getOwnerId() == $account
->id()) {
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "update own {$plugin_id} content");
}
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "update any {$plugin_id} content");
}
/**
* Performs access check for the delete operation.
*
* This method is supposed to be overwritten by extending classes that
* do their own custom access checking.
*
* @param \Drupal\group\Entity\GroupContentInterface $group_content
* The group content for which to check access.
* @param \Drupal\Core\Session\AccountInterface $account
* The user for which to check access.
*
* @return \Drupal\Core\Access\AccessResultInterface
* The access result.
*/
protected function deleteAccess(GroupContentInterface $group_content, AccountInterface $account) {
$group = $group_content
->getGroup();
$plugin_id = $this
->getPluginId();
// Allow members to delete their own group content.
if ($group_content
->getOwnerId() == $account
->id()) {
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "delete own {$plugin_id} content");
}
return GroupAccessResult::allowedIfHasGroupPermission($group, $account, "delete any {$plugin_id} content");
}
/**
* {@inheritdoc}
*/
public function checkAccess(GroupContentInterface $group_content, $operation, AccountInterface $account) {
/** @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface $manager */
$manager = \Drupal::service('plugin.manager.group_content_enabler');
// Backwards compatibility layer:
// - Use the declared access control handler if available.
if ($manager
->hasHandler($this->pluginId, 'access')) {
return $manager
->getAccessControlHandler($this->pluginId)
->relationAccess($group_content, $operation, $account, TRUE);
}
// Backwards compatibility layer:
// - Run the old code if there is no access control handler.
switch ($operation) {
case 'view':
$result = $this
->viewAccess($group_content, $account);
break;
case 'update':
$result = $this
->updateAccess($group_content, $account);
break;
case 'delete':
$result = $this
->deleteAccess($group_content, $account);
break;
default:
$result = GroupAccessResult::neutral();
}
return $result;
}
/**
* {@inheritdoc}
*/
public function getEntityReferenceLabel() {
return isset($this->pluginDefinition['reference_label']) ? $this->pluginDefinition['reference_label'] : NULL;
}
/**
* {@inheritdoc}
*/
public function getEntityReferenceDescription() {
return isset($this->pluginDefinition['reference_description']) ? $this->pluginDefinition['reference_description'] : NULL;
}
/**
* {@inheritdoc}
*/
public function getEntityReferenceSettings() {
$settings['target_type'] = $this
->getEntityTypeId();
if ($bundle = $this
->getEntityBundle()) {
$settings['handler_settings']['target_bundles'] = [
$bundle,
];
}
return $settings;
}
/**
* {@inheritdoc}
*/
public function postInstall() {
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration) {
// Do not allow the changing of the group type ID after construction.
unset($configuration['group_type_id']);
// Merge in the default configuration.
$this->configuration = NestedArray::mergeDeep($this
->defaultConfiguration(), $configuration);
return $this;
}
/**
* {@inheritdoc}
*/
public function defaultConfiguration() {
// Warning: For every key defined here you need to have a matching config
// schema entry following the pattern group_content_enabler.config.MY_KEY!
// @see group.schema.yml
return [
'group_cardinality' => 0,
'entity_cardinality' => 0,
'use_creation_wizard' => 0,
];
}
/**
* {@inheritdoc}
*/
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
/** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
$entity_type_manager = \Drupal::service('entity_type.manager');
$replace = [
'%entity_type' => $entity_type_manager
->getDefinition($this
->getEntityTypeId())
->getLabel(),
'%group_type' => $this
->getGroupType()
->label(),
'%plugin' => $this
->getLabel(),
];
$form['group_cardinality'] = [
'#type' => 'number',
'#title' => $this
->t('Group cardinality'),
'#description' => $this
->t('The amount of %group_type groups a single %entity_type entity can be added to as a %plugin. Set to 0 for unlimited.', $replace),
'#default_value' => $this->configuration['group_cardinality'],
'#min' => 0,
'#required' => TRUE,
];
$form['entity_cardinality'] = [
'#type' => 'number',
'#title' => $this
->t('Entity cardinality'),
'#description' => $this
->t('The amount of times a single %entity_type entity can be added to the same %group_type group as a %plugin. Set to 0 for unlimited.', $replace),
'#default_value' => $this->configuration['entity_cardinality'],
'#min' => 0,
'#required' => TRUE,
];
if ($this
->definesEntityAccess()) {
$form['use_creation_wizard'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Use 2-step wizard when creating a new %entity_type entity within a %group_type group', $replace),
'#description' => $this
->t('This will first show you the form to create the actual entity and then a form to create the relationship between the entity and the group.<br />You can choose to disable this wizard if you did not or will not add any fields to the relationship (i.e. this plugin).<br /><strong>Warning:</strong> If you do have fields on the relationship and do not use the wizard, you may end up with required fields not being filled out.'),
'#default_value' => $this->configuration['use_creation_wizard'],
];
}
return $form;
}
/**
* {@inheritdoc}
*/
public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*
* 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.
*/
public function submitConfigurationForm(array &$form, FormStateInterface $form_state) {
}
/**
* {@inheritdoc}
*/
public function calculateDependencies() {
$dependencies['module'][] = $this
->getProvider();
$dependencies['module'][] = $this
->getEntityType()
->getProvider();
return $dependencies;
}
}
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 |
Form constructor. Overrides PluginFormInterface:: |
2 |
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 |
Gets default configuration for this plugin. Overrides ConfigurableInterface:: |
2 |
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:: |
public | function |
Returns a list of entity reference field settings. Overrides GroupContentEnablerInterface:: |
1 |
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 |
Provides a list of operations for a group. Overrides GroupContentEnablerInterface:: |
2 |
GroupContentEnablerBase:: |
public | function |
Provides the cacheable metadata for this plugin's group operations. Overrides GroupContentEnablerInterface:: |
1 |
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 |
Runs tasks after the group content type for this plugin has been created. Overrides GroupContentEnablerInterface:: |
1 |
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:: |
|
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. |