WorkflowGroupManager.php in State Machine 8
File
src/WorkflowGroupManager.php
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\DefaultPluginManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeDiscoveryDecorator;
use Drupal\Core\Plugin\Discovery\YamlDiscovery;
use Drupal\state_machine\Plugin\Workflow\Workflow;
use Drupal\state_machine\Plugin\WorkflowGroup\WorkflowGroup;
class WorkflowGroupManager extends DefaultPluginManager implements WorkflowGroupManagerInterface {
protected $defaults = [
'id' => '',
'label' => '',
'entity_type' => '',
'class' => WorkflowGroup::class,
'workflow_class' => Workflow::class,
];
public function __construct(ModuleHandlerInterface $module_handler, CacheBackendInterface $cache_backend) {
$this->moduleHandler = $module_handler;
$this
->setCacheBackend($cache_backend, 'workflow_group', [
'workflow_group',
]);
$this
->alterInfo('workflow_groups');
}
protected function getDiscovery() {
if (!isset($this->discovery)) {
$this->discovery = new YamlDiscovery('workflow_groups', $this->moduleHandler
->getModuleDirectories());
$this->discovery
->addTranslatableProperty('label', 'label_context');
$this->discovery = new ContainerDerivativeDiscoveryDecorator($this->discovery);
}
return $this->discovery;
}
public function processDefinition(&$definition, $plugin_id) {
parent::processDefinition($definition, $plugin_id);
if ($plugin_id == 'state_machine') {
throw new PluginException('The "state_machine" workflow_group ID is reserved and must not be used.');
}
$definition['id'] = $plugin_id;
foreach ([
'label',
'entity_type',
] as $required_property) {
if (empty($definition[$required_property])) {
throw new PluginException(sprintf('The workflow_group %s must define the %s property.', $plugin_id, $required_property));
}
}
}
public function getDefinitionsByEntityType($entity_type_id = NULL) {
$definitions = $this
->getDefinitions();
if ($entity_type_id) {
$definitions = array_filter($definitions, function ($definition) use ($entity_type_id) {
return $definition['entity_type'] == $entity_type_id;
});
}
return $definitions;
}
}