View source
<?php
namespace Drupal\state_machine;
use Drupal\Component\Plugin\Exception\PluginException;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
class WorkflowManager extends DefaultPluginManager implements WorkflowManagerInterface {
protected $groupManager;
protected $plugins;
protected $defaults = [
'id' => '',
'label' => '',
'group' => '',
'states' => [],
'transitions' => [],
];
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend, WorkflowGroupManagerInterface $group_manager) {
$this->moduleHandler = $module_handler;
$this
->setCacheBackend($cache_backend, 'workflow', [
'workflow',
]);
$this->groupManager = $group_manager;
$this
->alterInfo('workflows');
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('workflows', $this->moduleHandler
->getModuleDirectories());
$this->discovery
->addTranslatableProperty('label', 'label_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
}
return $this->discovery;
}
public function createInstance($plugin_id, array $configuration = []) {
if (empty($this->plugins[$plugin_id])) {
$plugin_definition = $this
->getDefinition($plugin_id);
if (empty($plugin_definition['group'])) {
throw new PluginException(sprintf('The workflow %s must define the group property.', $plugin_id));
}
$group_definition = $this->groupManager
->getDefinition($plugin_definition['group']);
$plugin_class = $group_definition['workflow_class'];
if (is_subclass_of($plugin_class, ContainerFactoryPluginInterface::class)) {
$this->plugins[$plugin_id] = $plugin_class::create(\Drupal::getContainer(), $configuration, $plugin_id, $plugin_definition);
}
else {
$this->plugins[$plugin_id] = new $plugin_class($configuration, $plugin_id, $plugin_definition);
}
}
return $this->plugins[$plugin_id];
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
$definition['id'] = $plugin_id;
foreach ([
'label',
'group',
'states',
'transitions',
] as $required_property) {
if (empty($definition[$required_property])) {
throw new PluginException(sprintf('The workflow %s must define the %s property.', $plugin_id, $required_property));
}
}
foreach ($definition['states'] as $state_id => $state_definition) {
if (empty($state_definition['label'])) {
throw new PluginException(sprintf('The workflow state %s must define the label property.', $state_id));
}
}
foreach ($definition['transitions'] as $transition_id => $transition_definition) {
foreach ([
'label',
'from',
'to',
] as $required_property) {
if (empty($transition_definition[$required_property])) {
throw new PluginException(sprintf('The workflow transition %s must define the %s property.', $transition_id, $required_property));
}
}
foreach ($transition_definition['from'] as $from_state) {
if (!isset($definition['states'][$from_state])) {
throw new PluginException(sprintf('The workflow transition %s specified an invalid "from" property: %s.', $transition_id, $from_state));
}
}
$to_state = $transition_definition['to'];
if (!isset($definition['states'][$to_state])) {
throw new PluginException(sprintf('The workflow transition %s specified an invalid "to" property.', $transition_id));
}
foreach ($transition_definition['from'] as $from_state) {
if ($from_state == $transition_definition['to']) {
throw new PluginException(sprintf('Invalid workflow transition %s: the "from" and "to" properties cannot overlap.', $transition_id));
}
}
}
}
public function getGroupedLabels($entity_type_id = NULL) {
$definitions = $this
->getSortedDefinitions();
$group_labels = $this
->getGroupLabels($entity_type_id);
$grouped_definitions = [];
foreach ($definitions as $id => $definition) {
$group_id = $definition['group'];
if (!isset($group_labels[$group_id])) {
continue;
}
$group_label = $group_labels[$group_id];
$grouped_definitions[$group_label][$id] = $definition['label'];
}
return $grouped_definitions;
}
protected function getSortedDefinitions() {
$definitions = $this
->getDefinitions();
uasort($definitions, function ($a, $b) {
if ($a['group'] != $b['group']) {
return strnatcasecmp($a['group'], $b['group']);
}
return strnatcasecmp($a['label'], $b['label']);
});
return $definitions;
}
protected function getGroupLabels($entity_type_id = NULL) {
$group_definitions = $this->groupManager
->getDefinitionsByEntityType($entity_type_id);
$group_labels = array_map(function ($group_definition) {
return (string) $group_definition['label'];
}, $group_definitions);
natcasesort($group_labels);
return $group_labels;
}
}