You are here

class StateChangeDeriver in Workbench Moderation Actions 8

Derives which moderation states are available.

Hierarchy

Expanded class hierarchy of StateChangeDeriver

1 file declares its use of StateChangeDeriver
workbench_moderation_actions.install in ./workbench_moderation_actions.install
Contains workbench_moderation.install.

File

src/Plugin/Deriver/StateChangeDeriver.php, line 16

Namespace

Drupal\workbench_moderation_actions\Plugin\Deriver
View 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

Namesort descending Modifiers Type Description Overrides
DeriverBase::$derivatives protected property List of derivative definitions. 1
DeriverBase::getDerivativeDefinition public function Gets the definition of a derivative plugin. Overrides DeriverInterface::getDerivativeDefinition
StateChangeDeriver::$entityTypeManager protected property The EntityTypeManagerInterface.
StateChangeDeriver::$moderationInformation protected property The ModerationInformationInterface.
StateChangeDeriver::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
StateChangeDeriver::getAvailableStates protected function Gets the available moderation states.
StateChangeDeriver::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
StateChangeDeriver::getModeratedEntityTypeLabels protected function Gets an array of moderated entity type labels.
StateChangeDeriver::__construct public function Creates a new StateChangeDeriver instance.
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.