entity_type_access_conditions-1.0.1/src/EntityTypeAccessConditionsPluginManager.php
src/EntityTypeAccessConditionsPluginManager.php
<?php
declare(strict_types=1);
namespace Drupal\entity_type_access_conditions;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\Core\Plugin\Factory\ContainerFactory;
/**
* Defines a plugin manager to deal with entity_type_access_conditions.
*
* Modules can define entity_type_access_conditions in a
* MODULE_NAME.entity_type_access_conditions.yml file contained in the module's
* base directory. Each entity_type_access_conditions has the following
* structure:
*
* @code
* MACHINE_NAME:
* label: STRING
* altered_forms:
* - STRING
* restricted_operations:
* - STRING
* @endcode
*
* @see \Drupal\entity_type_access_conditions\EntityTypeAccessConditionsDefault
* @see \Drupal\entity_type_access_conditions\EntityTypeAccessConditionsInterface
*/
final class EntityTypeAccessConditionsPluginManager extends DefaultPluginManager {
/**
* {@inheritdoc}
*/
protected $defaults = [
// The entity_type_access_conditions id. Set by the plugin system based on
// the top-level YAML key.
'id' => '',
// The entity_type_access_conditions label.
'label' => '',
// The restricted operations.
'restricted_operations' => [],
// The forms to alter.
'altered_forms' => [],
// Default plugin class.
'class' => EntityTypeAccessConditionsDefault::class,
];
/**
* Constructs EntityTypeAccessConditionsPluginManager object.
*/
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->factory = new ContainerFactory($this);
$this->moduleHandler = $module_handler;
$this->alterInfo('entity_type_access_conditions_info');
$this->setCacheBackend($cache_backend, 'entity_type_access_conditions_plugins');
}
/**
* {@inheritdoc}
*/
protected function getDiscovery(): YamlDiscovery {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('entity_type_access_conditions', $this->moduleHandler->getModuleDirectories());
$this->discovery->addTranslatableProperty('label', 'label_context');
$this->discovery->addTranslatableProperty('description', 'description_context');
}
return $this->discovery;
}
/**
* Get all the forms that should be altered to add access conditions.
*/
public function getAlteredForms(): array {
$forms = [];
foreach ($this->getDefinitions() as $definition) {
$forms = array_merge($forms, $definition['altered_forms']);
}
return $forms;
}
}
