View source
<?php
namespace Drupal\wbm2cm;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Drupal\workbench_moderation\ModerationStateInterface;
use Drupal\workbench_moderation\ModerationStateTransitionInterface;
class WorkflowCollector {
use StringTranslationTrait;
protected $entityTypeManager;
protected $stateStorage;
protected $transitionStorage;
public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $translation = NULL) {
$this->entityTypeManager = $entity_type_manager;
$this->stateStorage = $entity_type_manager
->getStorage('moderation_state');
$this->transitionStorage = $entity_type_manager
->getStorage('moderation_state_transition');
if ($translation) {
$this
->setStringTranslation($translation);
}
}
public function getWorkflows() {
$workflows = [];
foreach ($this
->enabled() as $id => $bundle) {
$states = $bundle
->getThirdPartySetting('workbench_moderation', 'allowed_moderation_states', []);
sort($states);
$hash = sha1(implode('', $states));
if (empty($workflows[$hash])) {
$workflows[$hash] = [
'id' => substr($hash, 0, 8),
'type' => 'content_moderation',
'type_settings' => [
'states' => $this
->mapStates($states),
'transitions' => $this
->mapTransitions($states),
'entity_types' => [],
],
];
}
$bundle_of = $bundle
->getEntityType()
->getBundleOf();
$workflows[$hash]['type_settings']['entity_types'][$bundle_of][] = $id;
}
$i = 0;
foreach ($workflows as &$workflow) {
$workflow['label'] = $this
->t('Workflow @number', [
'@number' => ++$i,
]);
}
return $workflows;
}
protected function mapStates(array $states) {
$weight = 1;
$map = function (ModerationStateInterface $state) use (&$weight) {
return [
'label' => $state
->label(),
'published' => $state
->isPublishedState(),
'default_revision' => $state
->isDefaultRevisionState(),
'weight' => $weight++,
];
};
return array_map($map, $this->stateStorage
->loadMultiple($states));
}
protected function mapTransitions(array $states) {
$excluded_states = array_diff($this->stateStorage
->getQuery()
->execute(), $states);
$transitions = $this->transitionStorage
->getQuery()
->condition('stateFrom', $excluded_states, 'NOT IN')
->condition('stateTo', $excluded_states, 'NOT IN')
->execute();
$weight = 1;
$map = function (ModerationStateTransitionInterface $transition) use (&$weight) {
return [
'label' => $transition
->label(),
'from' => (array) $transition
->getFromState(),
'to' => $transition
->getToState(),
'weight' => $weight++,
];
};
return array_map($map, $this->transitionStorage
->loadMultiple($transitions));
}
protected function enabled() {
foreach ($this
->supported() as $id => $entity) {
if ($entity
->getThirdPartySetting('workbench_moderation', 'enabled', FALSE)) {
(yield $id => $entity);
}
}
}
public function supported() {
foreach ($this->entityTypeManager
->getDefinitions() as $id => $entity_type) {
if ($entity_type
->getBundleOf()) {
$storage = $this->entityTypeManager
->getStorage($id);
foreach ($storage
->getQuery()
->execute() as $entity_id) {
$entity = $storage
->load($entity_id);
if (in_array('workbench_moderation', $entity
->getThirdPartyProviders(), TRUE)) {
(yield $entity_id => $entity);
}
}
}
}
}
}