You are here

public function EntityCollectionLocalActionProvider::buildLocalActions in Entity API 8

Builds local actions for the given entity type.

Parameters

\Drupal\Core\Entity\EntityTypeInterface $entity_type: The entity type.

Return value

array[] An array of local action definitions.

Overrides EntityLocalActionProviderInterface::buildLocalActions

File

src/Menu/EntityCollectionLocalActionProvider.php, line 40

Class

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

Namespace

Drupal\entity\Menu

Code

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;
}