Added Entity and Entity Reference Revisions which got dropped somewhere along the...
[yaffs-website] / web / modules / contrib / entity / src / Menu / EntityCollectionLocalActionProvider.php
diff --git a/web/modules/contrib/entity/src/Menu/EntityCollectionLocalActionProvider.php b/web/modules/contrib/entity/src/Menu/EntityCollectionLocalActionProvider.php
new file mode 100644 (file)
index 0000000..4ddd554
--- /dev/null
@@ -0,0 +1,47 @@
+<?php
+
+namespace Drupal\entity\Menu;
+
+use Drupal\Core\Entity\EntityTypeInterface;
+
+/**
+ * Provides a action link to the add page or add form on the collection.
+ */
+class EntityCollectionLocalActionProvider implements EntityLocalActionProviderInterface {
+
+  /**
+   * {@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] = [
+          // The title is translated at runtime by EntityAddLocalAction.
+          /* @see \Drupal\entity\Menu\EntityAddLocalAction::getTitle() */
+          'title' => 'Add ' . $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"],
+          'class' => EntityAddLocalAction::class,
+        ];
+      }
+    }
+    return $actions;
+  }
+
+}