You are here

class DynamicLocalTasks in Workbench Moderation 8

Same name and namespace in other branches
  1. 8.2 src/Plugin/Derivative/DynamicLocalTasks.php \Drupal\workbench_moderation\Plugin\Derivative\DynamicLocalTasks

Generates moderation-related local tasks.

Hierarchy

Expanded class hierarchy of DynamicLocalTasks

1 string reference to 'DynamicLocalTasks'
workbench_moderation.links.task.yml in ./workbench_moderation.links.task.yml
workbench_moderation.links.task.yml

File

src/Plugin/Derivative/DynamicLocalTasks.php, line 18

Namespace

Drupal\workbench_moderation\Plugin\Derivative
View source
class DynamicLocalTasks extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

  /**
   * The base plugin ID.
   *
   * @var string
   */
  protected $basePluginId;

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

  /**
   * Creates an FieldUiLocalTask object.
   *
   * @param string $base_plugin_id
   *   The base plugin ID.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The translation manager.
   */
  public function __construct($base_plugin_id, EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
    $this->basePluginId = $base_plugin_id;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $base_plugin_id) {
    return new static($base_plugin_id, $container
      ->get('entity_type.manager'), $container
      ->get('string_translation'));
  }

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    $this->derivatives = [];
    foreach ($this
      ->moderatableEntityTypeDefinitions() as $entity_type_id => $entity_type) {
      $this->derivatives["{$entity_type_id}.moderation_tab"] = [
        'route_name' => "entity.{$entity_type_id}.moderation",
        'title' => $this
          ->t('Manage moderation'),
        // @todo - are we sure they all have an edit_form?
        'base_route' => "entity.{$entity_type_id}.edit_form",
        'weight' => 30,
      ] + $base_plugin_definition;
    }
    $latest_version_entities = array_filter($this
      ->moderatableEntityDefinitions(), function (EntityTypeInterface $type) {
      return $type
        ->hasLinkTemplate('latest-version');
    });
    foreach ($latest_version_entities as $entity_type_id => $entity_type) {
      $this->derivatives["{$entity_type_id}.latest_version_tab"] = [
        'route_name' => "entity.{$entity_type_id}.latest_version",
        'title' => $this
          ->t('Latest version'),
        'base_route' => "entity.{$entity_type_id}.canonical",
        'weight' => 1,
      ] + $base_plugin_definition;
    }
    return $this->derivatives;
  }

  /**
   * Returns an array of content entities that are potentially moderateable.
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface[]
   *   An array of just those entities we care about.
   */
  protected function moderatableEntityDefinitions() {
    return array_filter($this->entityTypeManager
      ->getDefinitions(), function (EntityTypeInterface $type) {
      return $type instanceof ContentEntityTypeInterface && $type
        ->getBundleEntityType() && $type
        ->isRevisionable();
    });
  }

  /**
   * Returns an iterable of config entities representing moderatable content.
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface[]
   *   An array of just those entity types we care about.
   */
  protected function moderatableEntityTypeDefinitions() {
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    return array_filter($entity_types, function (EntityTypeInterface $type) use ($entity_types) {
      return $type instanceof ConfigEntityTypeInterface && ($bundle_of = $type
        ->get('bundle_of')) && $entity_types[$bundle_of]
        ->isRevisionable();
    });
  }

}

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
DynamicLocalTasks::$basePluginId protected property The base plugin ID.
DynamicLocalTasks::$entityTypeManager protected property The entity type manager.
DynamicLocalTasks::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
DynamicLocalTasks::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions
DynamicLocalTasks::moderatableEntityDefinitions protected function Returns an array of content entities that are potentially moderateable.
DynamicLocalTasks::moderatableEntityTypeDefinitions protected function Returns an iterable of config entities representing moderatable content.
DynamicLocalTasks::__construct public function Creates an FieldUiLocalTask object.
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.