You are here

class WorkflowCollector in Workbench Moderation to Content Moderation 8.2

Hierarchy

Expanded class hierarchy of WorkflowCollector

1 string reference to 'WorkflowCollector'
wbm2cm.services.yml in ./wbm2cm.services.yml
wbm2cm.services.yml
1 service uses WorkflowCollector
wbm2cm.workflow_collector in ./wbm2cm.services.yml
\Drupal\wbm2cm\WorkflowCollector

File

src/WorkflowCollector.php, line 11

Namespace

Drupal\wbm2cm
View source
class WorkflowCollector {
  use StringTranslationTrait;

  /**
   * The entity type manager service.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The moderation state entity storage handler.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $stateStorage;

  /**
   * The moderation state transition entity storage handler.
   *
   * @var \Drupal\Core\Entity\EntityStorageInterface
   */
  protected $transitionStorage;

  /**
   * WorkflowCollector constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager service.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $translation
   *   (optional) The string translation service.
   */
  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);
    }
  }

  /**
   * Returns all unique content type workflows.
   *
   * @return array
   *   An array of arrays, each of which is a set of values representing a
   *   workflow config entity.
   */
  public function getWorkflows() {
    $workflows = [];

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $bundle */
    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;
  }

  /**
   * Generates Content Moderation-compatible moderation state definitions.
   *
   * @param string[] $states
   *   The moderation state entity IDs.
   *
   * @return array
   *   The Content Moderation-compatible moderation state definitions.
   */
  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));
  }

  /**
   * Generates Content Moderation-compatible state transition definitions.
   *
   * @param string[] $states
   *   The moderation state entity IDs for which transition definitions should
   *   be generated.
   *
   * @return array
   *   The Content Moderation-compatible state transition definitions.
   */
  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() {

    /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
    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) {

          /** @var \Drupal\Core\Config\Entity\ConfigEntityInterface $entity */
          $entity = $storage
            ->load($entity_id);
          if (in_array('workbench_moderation', $entity
            ->getThirdPartyProviders(), TRUE)) {
            (yield $entity_id => $entity);
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkflowCollector::$entityTypeManager protected property The entity type manager service.
WorkflowCollector::$stateStorage protected property The moderation state entity storage handler.
WorkflowCollector::$transitionStorage protected property The moderation state transition entity storage handler.
WorkflowCollector::enabled protected function
WorkflowCollector::getWorkflows public function Returns all unique content type workflows.
WorkflowCollector::mapStates protected function Generates Content Moderation-compatible moderation state definitions.
WorkflowCollector::mapTransitions protected function Generates Content Moderation-compatible state transition definitions.
WorkflowCollector::supported public function
WorkflowCollector::__construct public function WorkflowCollector constructor.