From ac1679841d7bb177650d534e1dbab337fc6d1ded Mon Sep 17 00:00:00 2001 From: paul121 Date: Mon, 25 Oct 2021 13:24:17 -0700 Subject: [PATCH] Provide a route for each quick form. --- modules/core/quick/farm_quick.routing.yml | 10 ++----- .../src/Controller/QuickFormController.php | 2 +- .../Plugin/Derivative/QuickFormMenuLink.php | 6 ++-- modules/core/quick/src/QuickFormManager.php | 28 +++++++++++++++++++ 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/modules/core/quick/farm_quick.routing.yml b/modules/core/quick/farm_quick.routing.yml index abcdcf3c..cfadef06 100644 --- a/modules/core/quick/farm_quick.routing.yml +++ b/modules/core/quick/farm_quick.routing.yml @@ -5,10 +5,6 @@ farm.quick: _title: 'Quick forms' requirements: _permission: 'view quick forms index' -farm.quick.form: - path: '/quick/{id}' - defaults: - _form: 'Drupal\farm_quick\Form\QuickForm' - _title_callback: 'Drupal\farm_quick\Form\QuickForm::getTitle' - requirements: - _custom_access: 'Drupal\farm_quick\Form\QuickForm::access' + +route_callbacks: + - 'plugin.manager.quick_form:getRoutes' diff --git a/modules/core/quick/src/Controller/QuickFormController.php b/modules/core/quick/src/Controller/QuickFormController.php index ce6bd0d7..98131c08 100644 --- a/modules/core/quick/src/Controller/QuickFormController.php +++ b/modules/core/quick/src/Controller/QuickFormController.php @@ -48,7 +48,7 @@ class QuickFormController extends ControllerBase { $quick_forms = $this->quickFormManager->getDefinitions(); $items = []; foreach ($quick_forms as $quick_form) { - $url = Url::fromRoute('farm.quick.form', ['id' => $quick_form['id']]); + $url = Url::fromRoute('farm.quick.' . $quick_form['id']); if ($url->access()) { $items[] = [ 'title' => $quick_form['label'], diff --git a/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php b/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php index 6ec2b6b2..c205d370 100644 --- a/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php +++ b/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php @@ -50,11 +50,11 @@ class QuickFormMenuLink extends DeriverBase implements ContainerDeriverInterface // Add links for quick form. $quick_forms = $this->quickFormManager->getDefinitions(); foreach ($quick_forms as $quick_form) { - $links['farm.quick.' . $quick_form['id']] = [ + $route_id = 'farm.quick.' . $quick_form['id']; + $links[$route_id] = [ 'title' => $quick_form['label'], 'parent' => 'farm.quick', - 'route_name' => 'farm.quick.form', - 'route_parameters' => ['id' => $quick_form['id']], + 'route_name' => $route_id, ] + $base_plugin_definition; } diff --git a/modules/core/quick/src/QuickFormManager.php b/modules/core/quick/src/QuickFormManager.php index ef7b8684..6f38b3fc 100644 --- a/modules/core/quick/src/QuickFormManager.php +++ b/modules/core/quick/src/QuickFormManager.php @@ -5,6 +5,9 @@ namespace Drupal\farm_quick; use Drupal\Core\Plugin\DefaultPluginManager; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\farm_quick\Form\QuickForm; +use Symfony\Component\Routing\Route; +use Symfony\Component\Routing\RouteCollection; /** * Quick form manager class. @@ -34,4 +37,29 @@ class QuickFormManager extends DefaultPluginManager { $this->setCacheBackend($cache_backend, 'quick_forms'); } + /** + * Provides routes for quick forms. + * + * @return \Symfony\Component\Routing\RouteCollection + * Returns a route collection. + */ + public function getRoutes(): RouteCollection { + $route_collection = new RouteCollection(); + foreach ($this->getDefinitions() as $id => $definition) { + $route = new Route( + "/quick/$id", + [ + '_form' => QuickForm::class, + '_title_callback' => QuickForm::class . '::getTitle', + 'id' => $id, + ], + [ + '_custom_access' => QuickForm::class . '::access', + ], + ); + $route_collection->add("farm.quick.$id", $route); + } + return $route_collection; + } + }