You are here

class EntityCollectionLocalActionProvider in Entity API 8

Provides a action link to the add page or add form on the collection.

Hierarchy

Expanded class hierarchy of EntityCollectionLocalActionProvider

File

src/Menu/EntityCollectionLocalActionProvider.php, line 14

Namespace

Drupal\entity\Menu
View source
class EntityCollectionLocalActionProvider implements EntityLocalActionProviderInterface, EntityHandlerInterface {
  use StringTranslationTrait;

  /**
   * Constructs a new EntityCollectionLocalActionProvider object.
   *
   * @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
   *   The entity type.
   * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
   *   The string translation.
   */
  public function __construct(EntityTypeInterface $entity_type, TranslationInterface $string_translation) {
    $this
      ->setStringTranslation($string_translation);
  }

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

  /**
   * {@inheritdoc}
   */
  public function buildLocalActions(EntityTypeInterface $entity_type) {
    $actions = [];
    if ($entity_type
      ->hasLinkTemplate('collection')) {
      $entity_type_id = $entity_type
        ->id();
      if ($entity_type
        ->hasLinkTemplate('add-page')) {
        $route_name = "entity.{$entity_type_id}.add_page";
      }
      elseif ($entity_type
        ->hasLinkTemplate('add-form')) {
        $route_name = "entity.{$entity_type_id}.add_form";
      }
      if (isset($route_name)) {
        $actions[$route_name] = [
          'title' => $this
            ->t('Add @entity', [
            '@entity' => $entity_type
              ->getSingularLabel(),
          ]),
          'route_name' => $route_name,
          'options' => [
            // Redirect back to the collection after form submission.
            'query' => [
              'destination' => $entity_type
                ->getLinkTemplate('collection'),
            ],
          ],
          'appears_on' => [
            "entity.{$entity_type_id}.collection",
          ],
        ];
      }
    }
    return $actions;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityCollectionLocalActionProvider::buildLocalActions public function Builds local actions for the given entity type. Overrides EntityLocalActionProviderInterface::buildLocalActions
EntityCollectionLocalActionProvider::createInstance public static function Instantiates a new instance of this entity handler. Overrides EntityHandlerInterface::createInstance
EntityCollectionLocalActionProvider::__construct public function Constructs a new EntityCollectionLocalActionProvider 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.