class StateChangeDeriver in Workbench Moderation Actions 8
Derives which moderation states are available.
Hierarchy
- class \Drupal\Component\Plugin\Derivative\DeriverBase implements DeriverInterface
- class \Drupal\workbench_moderation_actions\Plugin\Deriver\StateChangeDeriver implements ContainerDeriverInterface uses StringTranslationTrait
Expanded class hierarchy of StateChangeDeriver
1 file declares its use of StateChangeDeriver
File
- src/
Plugin/ Deriver/ StateChangeDeriver.php, line 16
Namespace
Drupal\workbench_moderation_actions\Plugin\DeriverView source
class StateChangeDeriver extends DeriverBase implements ContainerDeriverInterface {
use StringTranslationTrait;
/**
* The ModerationInformationInterface.
*
* @var \Drupal\workbench_moderation\ModerationInformationInterface
*/
protected $moderationInformation;
/**
* The EntityTypeManagerInterface.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Creates a new StateChangeDeriver instance.
*
* @param \Drupal\workbench_moderation\ModerationInformationInterface $moderationInformation
* The ModerationInformationInterface.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The EntityTypeManagerInterface.
*/
public function __construct(ModerationInformationInterface $moderationInformation, EntityTypeManagerInterface $entityTypeManager) {
$this->moderationInformation = $moderationInformation;
$this->entityTypeManager = $entityTypeManager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static($container
->get('workbench_moderation.moderation_information'), $container
->get('entity_type.manager'));
}
/**
* Gets an array of moderated entity type labels.
*/
protected function getModeratedEntityTypeLabels() {
$entity_types = $this->moderationInformation
->selectRevisionableEntities($this->entityTypeManager
->getDefinitions());
return array_map(function (EntityTypeInterface $entityType) {
return $entityType
->getLabel();
}, $entity_types);
}
/**
* Gets the available moderation states.
*
* @return \Drupal\Core\Entity\EntityInterface[]
* An array of entity objects indexed by their IDs.
*
* Returns an empty array if no matching entities are found.
*/
protected function getAvailableStates() {
return $this->entityTypeManager
->getStorage('moderation_state')
->loadMultiple();
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
if (empty($this->derivatives)) {
$plugin = $base_plugin_definition;
$states = $this
->getAvailableStates();
$entity_type_labels = $this
->getModeratedEntityTypeLabels();
foreach ($entity_type_labels as $entity_type_id => $entity_label) {
$plugin['type'] = $entity_type_id;
foreach ($states as $state_id => $state) {
$plugin['state'] = $state_id;
$plugin['label'] = $this
->t('Set @entity_type_label as @state_label', [
'@entity_type_label' => $entity_label,
'@state_label' => $state
->label(),
]);
$this->derivatives[$entity_type_id . '__' . $state_id] = $plugin;
}
}
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DeriverBase:: |
protected | property | List of derivative definitions. | 1 |
DeriverBase:: |
public | function |
Gets the definition of a derivative plugin. Overrides DeriverInterface:: |
|
StateChangeDeriver:: |
protected | property | The EntityTypeManagerInterface. | |
StateChangeDeriver:: |
protected | property | The ModerationInformationInterface. | |
StateChangeDeriver:: |
public static | function |
Creates a new class instance. Overrides ContainerDeriverInterface:: |
|
StateChangeDeriver:: |
protected | function | Gets the available moderation states. | |
StateChangeDeriver:: |
public | function |
Gets the definition of all derivatives of a base plugin. Overrides DeriverBase:: |
|
StateChangeDeriver:: |
protected | function | Gets an array of moderated entity type labels. | |
StateChangeDeriver:: |
public | function | Creates a new StateChangeDeriver instance. | |
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. |