Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / Menu / EntityCollectionLocalActionProvider.php
1 <?php
2
3 namespace Drupal\entity\Menu;
4
5 use Drupal\Core\Entity\EntityTypeInterface;
6
7 /**
8  * Provides a action link to the add page or add form on the collection.
9  */
10 class EntityCollectionLocalActionProvider implements EntityLocalActionProviderInterface {
11
12   /**
13    * {@inheritdoc}
14    */
15   public function buildLocalActions(EntityTypeInterface $entity_type) {
16     $actions = [];
17     if ($entity_type->hasLinkTemplate('collection')) {
18       $entity_type_id = $entity_type->id();
19
20       if ($entity_type->hasLinkTemplate('add-page')) {
21         $route_name = "entity.$entity_type_id.add_page";
22       }
23       elseif ($entity_type->hasLinkTemplate('add-form')) {
24         $route_name = "entity.$entity_type_id.add_form";
25       }
26
27       if (isset($route_name)) {
28         $actions[$route_name] = [
29           // The title is translated at runtime by EntityAddLocalAction.
30           /* @see \Drupal\entity\Menu\EntityAddLocalAction::getTitle() */
31           'title' => 'Add ' . $entity_type->getSingularLabel(),
32           'route_name' => $route_name,
33           'options' => [
34             // Redirect back to the collection after form submission.
35             'query' => [
36               'destination' => $entity_type->getLinkTemplate('collection'),
37             ],
38           ],
39           'appears_on' => ["entity.$entity_type_id.collection"],
40           'class' => EntityAddLocalAction::class,
41         ];
42       }
43     }
44     return $actions;
45   }
46
47 }