class UserProtectionPluginCollection in User protect 8
A collection of protection rules.
Hierarchy
- class \Drupal\Component\Plugin\LazyPluginCollection implements \Drupal\Component\Plugin\IteratorAggregate, \Drupal\Component\Plugin\Countable
- class \Drupal\Core\Plugin\DefaultLazyPluginCollection uses DependencySerializationTrait
- class \Drupal\userprotect\Plugin\UserProtection\UserProtectionPluginCollection
- class \Drupal\Core\Plugin\DefaultLazyPluginCollection uses DependencySerializationTrait
Expanded class hierarchy of UserProtectionPluginCollection
2 files declare their use of UserProtectionPluginCollection
- ProtectionRule.php in src/
Entity/ ProtectionRule.php - ProtectionRuleUnitTest.php in tests/
src/ Kernel/ Entity/ ProtectionRuleUnitTest.php
File
- src/
Plugin/ UserProtection/ UserProtectionPluginCollection.php, line 11
Namespace
Drupal\userprotect\Plugin\UserProtectionView source
class UserProtectionPluginCollection extends DefaultLazyPluginCollection {
/**
* All possible user protection plugin IDs.
*
* @var array
*/
protected $definitions;
/**
* Retrieves all user protection plugin instances.
*
* @return array
* An array of user protection plugin instances.
*/
public function getAll() {
// Retrieve all available user protection plugin definitions.
if (!$this->definitions) {
$this->definitions = $this->manager
->getDefinitions();
}
// Ensure that there is an instance of all available plugins.
foreach ($this->definitions as $plugin_id => $definition) {
if (!isset($this->pluginInstances[$plugin_id])) {
$this
->initializePlugin($plugin_id);
}
}
// Sort plugins.
uasort($this->pluginInstances, [
$this,
'pluginInstancesSort',
]);
return $this->pluginInstances;
}
/**
* Retrieves enabled user protection plugin instances.
*
* @return array
* An array of active user protection plugin instances.
*/
public function getEnabledPlugins() {
$instances = $this
->getAll();
$enabled = [];
foreach ($this->configurations as $instance_id => $configuration) {
if ($configuration['status']) {
$enabled[] = $instances[$instance_id];
}
}
// Sort plugins.
uasort($enabled, [
$this,
'pluginInstancesSort',
]);
return $enabled;
}
/**
* {@inheritdoc}
*/
protected function initializePlugin($instance_id) {
$configuration = $this->manager
->getDefinition($instance_id);
// Merge the actual configuration into the default configuration.
if (isset($this->configurations[$instance_id])) {
$configuration = NestedArray::mergeDeep($configuration, $this->configurations[$instance_id]);
}
$this->configurations[$instance_id] = $configuration;
parent::initializePlugin($instance_id);
}
/**
* Sorts plugin instances based on weight, label, provider or id.
*
* @param \Drupal\userprotect\Plugin\UserProtection\UserProtectionInterface $a
* The first plugin in the comparison.
* @param \Drupal\userprotect\Plugin\UserProtection\UserProtectionInterface $b
* The second plugin in the comparison.
*
* @return int
* -1 if $a should go first.
* 1 if $b should go first.
* 0 if it's unknown which should go first.
*/
public function pluginInstancesSort(UserProtectionInterface $a, UserProtectionInterface $b) {
if ($a
->getWeight() != $b
->getWeight()) {
return $a
->getWeight() < $b
->getWeight() ? -1 : 1;
}
if ($a
->label() != $b
->label()) {
return strnatcasecmp($a
->label(), $b
->label());
}
if ($a->provider != $b->provider) {
return strnatcasecmp($a->provider, $b->provider);
}
return strnatcasecmp($a
->getPluginId(), $b
->getPluginId());
}
/**
* {@inheritdoc}
*/
public function getConfiguration() {
$configuration = parent::getConfiguration();
// Remove disabled protections.
foreach ($configuration as $instance_id => $instance_config) {
if (empty($instance_config['status'])) {
unset($configuration[$instance_id]);
}
}
return $configuration;
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DefaultLazyPluginCollection:: |
protected | property | The initial configuration for each plugin in the collection. | |
DefaultLazyPluginCollection:: |
protected | property | The manager used to instantiate the plugins. | |
DefaultLazyPluginCollection:: |
protected | property | The original order of the instances. | |
DefaultLazyPluginCollection:: |
protected | property | The key within the plugin configuration that contains the plugin ID. | 3 |
DefaultLazyPluginCollection:: |
public | function |
Adds an instance ID to the available instance IDs. Overrides LazyPluginCollection:: |
|
DefaultLazyPluginCollection:: |
public | function |
Removes an instance ID. Overrides LazyPluginCollection:: |
|
DefaultLazyPluginCollection:: |
public | function |
Sets the configuration for all plugins in this collection. Overrides LazyPluginCollection:: |
|
DefaultLazyPluginCollection:: |
public | function | Updates the configuration for a plugin instance. | |
DefaultLazyPluginCollection:: |
public | function | Sorts all plugin instances in this collection. | 1 |
DefaultLazyPluginCollection:: |
public | function | Provides uasort() callback to sort plugins. | 2 |
DefaultLazyPluginCollection:: |
public | function | Constructs a new DefaultLazyPluginCollection object. | 1 |
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 | |
LazyPluginCollection:: |
protected | property | Stores the IDs of all potential plugin instances. | |
LazyPluginCollection:: |
protected | property | Stores all instantiated plugins. | |
LazyPluginCollection:: |
public | function | Clears all instantiated plugins. | |
LazyPluginCollection:: |
public | function | ||
LazyPluginCollection:: |
public | function | Gets a plugin instance, initializing it if necessary. | 4 |
LazyPluginCollection:: |
public | function | Gets all instance IDs. | |
LazyPluginCollection:: |
public | function | ||
LazyPluginCollection:: |
public | function | Determines if a plugin instance exists. | |
LazyPluginCollection:: |
public | function | Removes an initialized plugin. | |
LazyPluginCollection:: |
public | function | Stores an initialized plugin. | |
UserProtectionPluginCollection:: |
protected | property | All possible user protection plugin IDs. | |
UserProtectionPluginCollection:: |
public | function | Retrieves all user protection plugin instances. | |
UserProtectionPluginCollection:: |
public | function |
Gets the current configuration of all plugins in this collection. Overrides DefaultLazyPluginCollection:: |
|
UserProtectionPluginCollection:: |
public | function | Retrieves enabled user protection plugin instances. | |
UserProtectionPluginCollection:: |
protected | function |
Initializes and stores a plugin. Overrides DefaultLazyPluginCollection:: |
|
UserProtectionPluginCollection:: |
public | function | Sorts plugin instances based on weight, label, provider or id. |