class GroupRelationManager in Group 2.0.x
Manages GroupRelation plugin implementations.
Hierarchy
- class \Drupal\Component\Plugin\PluginManagerBase implements PluginManagerInterface uses DiscoveryTrait
- class \Drupal\Core\Plugin\DefaultPluginManager implements CachedDiscoveryInterface, PluginManagerInterface, CacheableDependencyInterface uses DiscoveryCachedTrait, UseCacheBackendTrait
- class \Drupal\group\Plugin\Group\Relation\GroupRelationManager implements \Symfony\Component\DependencyInjection\ContainerAwareInterface, GroupRelationManagerInterface uses \Symfony\Component\DependencyInjection\ContainerAwareTrait
- class \Drupal\Core\Plugin\DefaultPluginManager implements CachedDiscoveryInterface, PluginManagerInterface, CacheableDependencyInterface uses DiscoveryCachedTrait, UseCacheBackendTrait
Expanded class hierarchy of GroupRelationManager
See also
hook_group_content_info_alter()
\Drupal\group\Annotation\GroupRelation
\Drupal\group\Plugin\Group\Relation\GroupRelationInterface
\Drupal\group\Plugin\Group\Relation\GroupRelationBase
1 file declares its use of GroupRelationManager
- GroupRelationManagerTest.php in tests/
src/ Unit/ GroupRelationManagerTest.php
1 string reference to 'GroupRelationManager'
1 service uses GroupRelationManager
File
- src/
Plugin/ Group/ Relation/ GroupRelationManager.php, line 24
Namespace
Drupal\group\Plugin\Group\RelationView source
class GroupRelationManager extends DefaultPluginManager implements GroupRelationManagerInterface, ContainerAwareInterface {
use ContainerAwareTrait;
/**
* Contains instantiated handlers keyed by handler type and plugin ID.
*
* @var array
*/
protected $handlers = [];
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* The group type storage handler.
*
* @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
*/
protected $groupTypeStorage;
/**
* A group content type storage handler.
*
* @var \Drupal\group\Entity\Storage\GroupContentTypeStorageInterface
*/
protected $groupContentTypeStorage;
/**
* A collection of vanilla instances of all group relation plugins.
*
* @var \Drupal\group\Plugin\Group\Relation\GroupRelationCollection
*/
protected $allPlugins;
/**
* An list each group type's installed plugins as plugin collections.
*
* @var \Drupal\group\Plugin\Group\Relation\GroupRelationCollection[]
*/
protected $groupTypeInstalled = [];
/**
* An static cache of group content type IDs per plugin ID.
*
* @var array[]
*/
protected $pluginGroupContentTypeMap;
/**
* The cache key for the group content type IDs per plugin ID map.
*
* @var string
*/
protected $pluginGroupContentTypeMapCacheKey;
/**
* An static cache of plugin IDs per group type ID.
*
* @var array[]
*/
protected $groupTypePluginMap;
/**
* The cache key for the plugin IDs per group type ID map.
*
* @var string
*/
protected $groupTypePluginMapCacheKey;
/**
* Constructs a GroupRelationManager object.
*
* @param \Traversable $namespaces
* An object that implements \Traversable which contains the root paths
* keyed by the corresponding namespace to look for plugin implementations.
* @param \Drupal\Core\Cache\CacheBackendInterface $cache_backend
* Cache backend instance to use.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler to invoke the alter hook with.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(\Traversable $namespaces, CacheBackendInterface $cache_backend, ModuleHandlerInterface $module_handler, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct('Plugin/Group/Relation', $namespaces, $module_handler, 'Drupal\\group\\Plugin\\Group\\Relation\\GroupRelationInterface', 'Drupal\\group\\Annotation\\GroupRelation');
$this
->alterInfo('group_relation_info');
$this
->setCacheBackend($cache_backend, 'group_relations');
$this->entityTypeManager = $entity_type_manager;
$this->pluginGroupContentTypeMapCacheKey = $this->cacheKey . '_GCT_map';
$this->groupTypePluginMapCacheKey = $this->cacheKey . '_GT_map';
}
/**
* {@inheritdoc}
*/
public function getHandler($plugin_id, $handler_type) {
if (!isset($this->handlers[$handler_type][$plugin_id])) {
$this->handlers[$handler_type][$plugin_id] = $this
->createHandlerInstance($plugin_id, $handler_type);
}
return $this->handlers[$handler_type][$plugin_id];
}
/**
* {@inheritdoc}
*/
public function createHandlerInstance($plugin_id, $handler_type) {
$definition = $this
->getDefinition($plugin_id);
$service_name = "group.relation_handler.{$handler_type}.{$definition['id']}";
if (!$this->container
->has($service_name)) {
throw new InvalidPluginDefinitionException($plugin_id, sprintf('The "%s" plugin did not specify a %s handler service (%s).', $plugin_id, $handler_type, $service_name));
}
$handler = $this->container
->get($service_name);
if (!is_subclass_of($handler, 'Drupal\\group\\Plugin\\Group\\RelationHandler\\RelationHandlerInterface')) {
throw new InvalidPluginDefinitionException($plugin_id, 'Trying to instantiate a handler that does not implement \\Drupal\\group\\Plugin\\Group\\RelationHandler\\RelationHandlerInterface.');
}
$handler
->init($plugin_id, $definition);
return $handler;
}
/**
* {@inheritdoc}
*/
public function getAccessControlHandler($plugin_id) {
return $this
->getHandler($plugin_id, 'access_control');
}
/**
* {@inheritdoc}
*/
public function getPermissionProvider($plugin_id) {
return $this
->getHandler($plugin_id, 'permission_provider');
}
/**
* {@inheritdoc}
*/
public function getPostInstallHandler($plugin_id) {
return $this
->getHandler($plugin_id, 'post_install');
}
/**
* Returns the group type storage handler.
*
* @return \Drupal\Core\Config\Entity\ConfigEntityStorageInterface
* The group type storage handler.
*/
protected function getGroupTypeStorage() {
if (!isset($this->groupTypeStorage)) {
$this->groupTypeStorage = $this->entityTypeManager
->getStorage('group_type');
}
return $this->groupTypeStorage;
}
/**
* Returns the group content type storage handler.
*
* @return \Drupal\group\Entity\Storage\GroupContentTypeStorageInterface
* The group content type storage handler.
*/
protected function getGroupContentTypeStorage() {
if (!isset($this->groupContentTypeStorage)) {
$this->groupContentTypeStorage = $this->entityTypeManager
->getStorage('group_content_type');
}
return $this->groupContentTypeStorage;
}
/**
* {@inheritdoc}
*/
public function getAll() {
if (!isset($this->allPlugins)) {
$collection = new GroupRelationCollection($this, []);
// Add every known plugin to the collection with a vanilla configuration.
foreach ($this
->getDefinitions() as $plugin_id => $plugin_info) {
$collection
->setInstanceConfiguration($plugin_id, [
'id' => $plugin_id,
]);
}
// Sort and set the plugin collection.
$this->allPlugins = $collection
->sort();
}
return $this->allPlugins;
}
/**
* {@inheritdoc}
*/
public function getInstalled(GroupTypeInterface $group_type = NULL) {
return !isset($group_type) ? $this
->getVanillaInstalled() : $this
->getGroupTypeInstalled($group_type);
}
/**
* Retrieves a vanilla instance of every installed plugin.
*
* @return \Drupal\group\Plugin\Group\Relation\GroupRelationCollection
* A plugin collection with a vanilla instance of every installed plugin.
*/
protected function getVanillaInstalled() {
// Retrieve a vanilla instance of all known group relation plugins.
$plugins = clone $this
->getAll();
// Retrieve all installed group relation plugin IDs.
$installed = $this
->getInstalledIds();
// Remove uninstalled plugins from the collection.
/** @var \Drupal\group\Plugin\Group\Relation\GroupRelationCollection $plugins */
foreach ($plugins as $plugin_id => $plugin) {
if (!in_array($plugin_id, $installed)) {
$plugins
->removeInstanceId($plugin_id);
}
}
return $plugins;
}
/**
* Retrieves fully instantiated plugins for a group type.
*
* @param \Drupal\group\Entity\GroupTypeInterface $group_type
* The group type to instantiate the installed plugins for.
*
* @return \Drupal\group\Plugin\Group\Relation\GroupRelationCollection
* A plugin collection with fully instantiated plugins for the group type.
*/
protected function getGroupTypeInstalled(GroupTypeInterface $group_type) {
if (!isset($this->groupTypeInstalled[$group_type
->id()])) {
$configurations = [];
$group_content_types = $this
->getGroupContentTypeStorage()
->loadByGroupType($group_type);
// Get the plugin config from every group content type for the group type.
foreach ($group_content_types as $group_content_type) {
$plugin_id = $group_content_type
->getRelationPluginId();
// Grab the plugin config from every group content type and amend it
// with the group type ID so the plugin knows what group type to use. We
// also specify the 'id' key because DefaultLazyPluginCollection throws
// an exception if it is not present.
$configuration = $group_content_type
->get('plugin_config');
$configuration['group_type_id'] = $group_type
->id();
$configuration['id'] = $plugin_id;
$configurations[$plugin_id] = $configuration;
}
$plugins = new GroupRelationCollection($this, $configurations);
$plugins
->sort();
$this->groupTypeInstalled[$group_type
->id()] = $plugins;
}
return $this->groupTypeInstalled[$group_type
->id()];
}
/**
* {@inheritdoc}
*/
public function getInstalledIds(GroupTypeInterface $group_type = NULL) {
// If no group type was provided, we can find all installed plugin IDs by
// grabbing the keys from the group content type IDs per plugin ID map.
if (!isset($group_type)) {
return array_keys($this
->getPluginGroupContentTypeMap());
}
// Otherwise, we can find the entry in the plugin IDs per group type ID map.
$map = $this
->getGroupTypePluginMap();
return isset($map[$group_type
->id()]) ? $map[$group_type
->id()] : [];
}
/**
* {@inheritdoc}
*/
public function getPluginIdsByEntityTypeAccess($entity_type_id) {
$plugin_ids = [];
foreach ($this
->getDefinitions() as $plugin_id => $plugin_info) {
if (!empty($plugin_info['entity_access']) && $plugin_info['entity_type_id'] == $entity_type_id) {
$plugin_ids[] = $plugin_id;
}
}
return $plugin_ids;
}
/**
* {@inheritdoc}
*/
public function installEnforced(GroupTypeInterface $group_type = NULL) {
$enforced = [];
// Gather the ID of all plugins that are marked as enforced.
foreach ($this
->getDefinitions() as $plugin_id => $plugin_info) {
if ($plugin_info['enforced']) {
$enforced[] = $plugin_id;
}
}
// If no group type was specified, we check all of them.
/** @var \Drupal\group\Entity\GroupTypeInterface[] $group_types */
$group_types = empty($group_type) ? $this
->getGroupTypeStorage()
->loadMultiple() : [
$group_type,
];
// Search through all of the enforced plugins and install new ones.
foreach ($group_types as $group_type) {
$installed = $this
->getInstalledIds($group_type);
foreach ($enforced as $plugin_id) {
if (!in_array($plugin_id, $installed)) {
$this
->getGroupContentTypeStorage()
->createFromPlugin($group_type, $plugin_id)
->save();
}
}
}
}
/**
* {@inheritdoc}
*/
public function getGroupContentTypeIds($plugin_id) {
$map = $this
->getPluginGroupContentTypeMap();
return isset($map[$plugin_id]) ? $map[$plugin_id] : [];
}
/**
* {@inheritdoc}
*/
public function getPluginGroupContentTypeMap() {
$map = $this
->getCachedPluginGroupContentTypeMap();
if (!isset($map)) {
$map = [];
/** @var \Drupal\group\Entity\GroupContentTypeInterface[] $group_content_types */
$group_content_types = $this
->getGroupContentTypeStorage()
->loadMultiple();
foreach ($group_content_types as $group_content_type) {
$map[$group_content_type
->getRelationPluginId()][] = $group_content_type
->id();
}
$this
->setCachedPluginGroupContentTypeMap($map);
}
return $map;
}
/**
* Returns the cached group content type ID map.
*
* @return array|null
* On success this will return the group content ID map (array). On failure
* this should return NULL, indicating to other methods that this has not
* yet been defined. Success with no values should return as an empty array.
*/
protected function getCachedPluginGroupContentTypeMap() {
if (!isset($this->pluginGroupContentTypeMap) && ($cache = $this
->cacheGet($this->pluginGroupContentTypeMapCacheKey))) {
$this->pluginGroupContentTypeMap = $cache->data;
}
return $this->pluginGroupContentTypeMap;
}
/**
* Sets a cache of the group content type ID map.
*
* @param array $map
* The group content type ID map to store in cache.
*/
protected function setCachedPluginGroupContentTypeMap(array $map) {
$this
->cacheSet($this->pluginGroupContentTypeMapCacheKey, $map, Cache::PERMANENT);
$this->pluginGroupContentTypeMap = $map;
}
/**
* {@inheritdoc}
*/
public function getGroupTypePluginMap() {
$map = $this
->getCachedGroupTypePluginMap();
if (!isset($map)) {
$map = [];
/** @var \Drupal\group\Entity\GroupContentTypeInterface[] $group_content_types */
$group_content_types = $this
->getGroupContentTypeStorage()
->loadMultiple();
foreach ($group_content_types as $group_content_type) {
$map[$group_content_type
->getGroupTypeId()][] = $group_content_type
->getRelationPluginId();
}
$this
->setCachedGroupTypePluginMap($map);
}
return $map;
}
/**
* Returns the cached group type plugin map.
*
* @return array|null
* On success this will return the group type plugin map (array). On failure
* this should return NULL, indicating to other methods that this has not
* yet been defined. Success with no values should return as an empty array.
*/
protected function getCachedGroupTypePluginMap() {
if (!isset($this->groupTypePluginMap) && ($cache = $this
->cacheGet($this->groupTypePluginMapCacheKey))) {
$this->groupTypePluginMap = $cache->data;
}
return $this->groupTypePluginMap;
}
/**
* Sets a cache of the group type plugin map.
*
* @param array $map
* The group type plugin map to store in cache.
*/
protected function setCachedGroupTypePluginMap(array $map) {
$this
->cacheSet($this->groupTypePluginMapCacheKey, $map, Cache::PERMANENT);
$this->groupTypePluginMap = $map;
}
/**
* {@inheritdoc}
*/
public function clearCachedGroupTypeCollections(GroupTypeInterface $group_type = NULL) {
if (!isset($group_type)) {
$this->groupTypeInstalled = [];
}
else {
$this->groupTypeInstalled[$group_type
->id()] = NULL;
}
}
/**
* {@inheritdoc}
*/
public function clearCachedPluginMaps() {
if ($this->cacheBackend) {
$this->cacheBackend
->delete($this->pluginGroupContentTypeMapCacheKey);
$this->cacheBackend
->delete($this->groupTypePluginMapCacheKey);
}
$this->pluginGroupContentTypeMap = NULL;
$this->groupTypePluginMap = NULL;
// Also clear the array of per group type plugin collections as it shares
// its cache clearing requirements with the group type plugin map.
$this->groupTypeInstalled = [];
}
/**
* {@inheritdoc}
*/
public function clearCachedDefinitions() {
parent::clearCachedDefinitions();
// The collection of all plugins should only change if the plugin
// definitions change, so we can safely reset that here.
$this->allPlugins = NULL;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultPluginManager:: |
protected | property | Additional namespaces the annotation discovery mechanism should scan for annotation definitions. | |
DefaultPluginManager:: |
protected | property | Name of the alter hook if one should be invoked. | |
DefaultPluginManager:: |
protected | property | The cache key. | |
DefaultPluginManager:: |
protected | property | An array of cache tags to use for the cached definitions. | |
DefaultPluginManager:: |
protected | property | A set of defaults to be referenced by $this->processDefinition() if additional processing of plugins is necessary or helpful for development purposes. | 9 |
DefaultPluginManager:: |
protected | property | The module handler to invoke the alter hook. | 1 |
DefaultPluginManager:: |
protected | property | An object that implements \Traversable which contains the root paths keyed by the corresponding namespace to look for plugin implementations. | |
DefaultPluginManager:: |
protected | property | The name of the annotation that contains the plugin definition. | |
DefaultPluginManager:: |
protected | property | The interface each plugin should implement. | 1 |
DefaultPluginManager:: |
protected | property | The subdirectory within a namespace to look for plugins, or FALSE if the plugins are in the top level of the namespace. | |
DefaultPluginManager:: |
protected | function | Invokes the hook to alter the definitions if the alter hook is set. | 1 |
DefaultPluginManager:: |
protected | function | Sets the alter hook name. | |
DefaultPluginManager:: |
protected | function | Extracts the provider from a plugin definition. | |
DefaultPluginManager:: |
protected | function | Finds plugin definitions. | 7 |
DefaultPluginManager:: |
public | function |
The cache contexts associated with this object. Overrides CacheableDependencyInterface:: |
|
DefaultPluginManager:: |
protected | function | Returns the cached plugin definitions of the decorated discovery class. | |
DefaultPluginManager:: |
public | function |
The maximum age for which this object may be cached. Overrides CacheableDependencyInterface:: |
|
DefaultPluginManager:: |
public | function |
The cache tags associated with this object. Overrides CacheableDependencyInterface:: |
|
DefaultPluginManager:: |
public | function |
Gets the definition of all plugins for this type. Overrides DiscoveryTrait:: |
2 |
DefaultPluginManager:: |
protected | function |
Gets the plugin discovery. Overrides PluginManagerBase:: |
12 |
DefaultPluginManager:: |
protected | function |
Gets the plugin factory. Overrides PluginManagerBase:: |
|
DefaultPluginManager:: |
public | function | Performs extra processing on plugin definitions. | 13 |
DefaultPluginManager:: |
protected | function | Determines if the provider of a definition exists. | 3 |
DefaultPluginManager:: |
public | function | Initialize the cache backend. | |
DefaultPluginManager:: |
protected | function | Sets a cache of plugin definitions for the decorated discovery class. | |
DefaultPluginManager:: |
public | function |
Disable the use of caches. Overrides CachedDiscoveryInterface:: |
1 |
DiscoveryCachedTrait:: |
protected | property | Cached definitions array. | 1 |
DiscoveryCachedTrait:: |
public | function |
Overrides DiscoveryTrait:: |
3 |
DiscoveryTrait:: |
protected | function | Gets a specific plugin definition. | |
DiscoveryTrait:: |
public | function | ||
GroupRelationManager:: |
protected | property | A collection of vanilla instances of all group relation plugins. | |
GroupRelationManager:: |
protected | property | The entity type manager. | |
GroupRelationManager:: |
protected | property | A group content type storage handler. | |
GroupRelationManager:: |
protected | property | An list each group type's installed plugins as plugin collections. | |
GroupRelationManager:: |
protected | property | An static cache of plugin IDs per group type ID. | |
GroupRelationManager:: |
protected | property | The cache key for the plugin IDs per group type ID map. | |
GroupRelationManager:: |
protected | property | The group type storage handler. | |
GroupRelationManager:: |
protected | property | Contains instantiated handlers keyed by handler type and plugin ID. | |
GroupRelationManager:: |
protected | property | An static cache of group content type IDs per plugin ID. | |
GroupRelationManager:: |
protected | property | The cache key for the group content type IDs per plugin ID map. | |
GroupRelationManager:: |
public | function |
Clears static and persistent plugin definition caches. Overrides DefaultPluginManager:: |
|
GroupRelationManager:: |
public | function |
Clears the static per group type plugin collection cache. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Clears static and persistent plugin ID map caches. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Creates a new handler instance. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Creates a new access control handler instance. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Returns a plugin collection of all available group relations. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
protected | function | Returns the cached group type plugin map. | |
GroupRelationManager:: |
protected | function | Returns the cached group content type ID map. | |
GroupRelationManager:: |
public | function |
Retrieves all of the group content type IDs for a content plugin. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
protected | function | Returns the group content type storage handler. | |
GroupRelationManager:: |
protected | function | Retrieves fully instantiated plugins for a group type. | |
GroupRelationManager:: |
public | function |
Retrieves a list of plugin IDs per group type ID. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
protected | function | Returns the group type storage handler. | |
GroupRelationManager:: |
public | function |
Returns a handler instance for the given plugin and handler. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Returns a plugin collection of all installed group relations. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Returns the plugin ID of all group relations in use. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Creates a new permission provider instance. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Retrieves a list of group content type IDs per plugin ID. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Returns the ID of all plugins that define access for a given entity type. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
public | function |
Creates a new post install handler instance. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
protected | function | Retrieves a vanilla instance of every installed plugin. | |
GroupRelationManager:: |
public | function |
Installs all plugins which are marked as enforced. Overrides GroupRelationManagerInterface:: |
|
GroupRelationManager:: |
protected | function | Sets a cache of the group type plugin map. | |
GroupRelationManager:: |
protected | function | Sets a cache of the group content type ID map. | |
GroupRelationManager:: |
public | function |
Constructs a GroupRelationManager object. Overrides DefaultPluginManager:: |
|
PluginManagerBase:: |
protected | property | The object that discovers plugins managed by this manager. | |
PluginManagerBase:: |
protected | property | The object that instantiates plugins managed by this manager. | |
PluginManagerBase:: |
protected | property | The object that returns the preconfigured plugin instance appropriate for a particular runtime condition. | |
PluginManagerBase:: |
public | function |
Creates a pre-configured instance of a plugin. Overrides FactoryInterface:: |
12 |
PluginManagerBase:: |
public | function |
Gets a preconfigured instance of a plugin. Overrides MapperInterface:: |
6 |
PluginManagerBase:: |
protected | function | Allows plugin managers to specify custom behavior if a plugin is not found. | 1 |
UseCacheBackendTrait:: |
protected | property | Cache backend instance. | |
UseCacheBackendTrait:: |
protected | property | Flag whether caches should be used or skipped. | |
UseCacheBackendTrait:: |
protected | function | Fetches from the cache backend, respecting the use caches flag. | |
UseCacheBackendTrait:: |
protected | function | Stores data in the persistent cache, respecting the use caches flag. |