You are here

abstract class EntityActionDeriverBase in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Action/Plugin/Action/Derivative/EntityActionDeriverBase.php \Drupal\Core\Action\Plugin\Action\Derivative\EntityActionDeriverBase

Provides a base action for each entity type with specific interfaces.

Hierarchy

Expanded class hierarchy of EntityActionDeriverBase

File

core/lib/Drupal/Core/Action/Plugin/Action/Derivative/EntityActionDeriverBase.php, line 16

Namespace

Drupal\Core\Action\Plugin\Action\Derivative
View source
abstract class EntityActionDeriverBase extends DeriverBase implements ContainerDeriverInterface {
  use StringTranslationTrait;

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

  /**
   * Constructs a new EntityActionDeriverBase object.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, TranslationInterface $string_translation) {
    $this->entityTypeManager = $entity_type_manager;
    $this->stringTranslation = $string_translation;
  }

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

  /**
   * Indicates whether the deriver can be used for the provided entity type.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   *
   * @return bool
   *   TRUE if the entity type can be used, FALSE otherwise.
   */
  protected abstract function isApplicable(EntityTypeInterface $entity_type);

  /**
   * {@inheritdoc}
   */
  public function getDerivativeDefinitions($base_plugin_definition) {
    if (empty($this->derivatives)) {
      $definitions = [];
      foreach ($this
        ->getApplicableEntityTypes() as $entity_type_id => $entity_type) {
        $definition = $base_plugin_definition;
        $definition['type'] = $entity_type_id;
        $definition['label'] = sprintf('%s %s', $base_plugin_definition['action_label'], $entity_type
          ->getSingularLabel());
        $definitions[$entity_type_id] = $definition;
      }
      $this->derivatives = $definitions;
    }
    return parent::getDerivativeDefinitions($base_plugin_definition);
  }

  /**
   * Gets a list of applicable entity types.
   *
   * The list consists of all entity types which match the conditions for the
   * given deriver.
   * For example, if the action applies to entities that are publishable,
   * this method will find all entity types that are publishable.
   *
   * @return \Drupal\Core\Entity\EntityTypeInterface[]
   *   The applicable entity types, keyed by entity type ID.
   */
  protected function getApplicableEntityTypes() {
    $entity_types = $this->entityTypeManager
      ->getDefinitions();
    $entity_types = array_filter($entity_types, function (EntityTypeInterface $entity_type) {
      return $this
        ->isApplicable($entity_type);
    });
    return $entity_types;
  }

}

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
EntityActionDeriverBase::$entityTypeManager protected property The entity type manager.
EntityActionDeriverBase::create public static function Creates a new class instance. Overrides ContainerDeriverInterface::create
EntityActionDeriverBase::getApplicableEntityTypes protected function Gets a list of applicable entity types.
EntityActionDeriverBase::getDerivativeDefinitions public function Gets the definition of all derivatives of a base plugin. Overrides DeriverBase::getDerivativeDefinitions 1
EntityActionDeriverBase::isApplicable abstract protected function Indicates whether the deriver can be used for the provided entity type. 3
EntityActionDeriverBase::__construct public function Constructs a new EntityActionDeriverBase object.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
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.