From 3505e80124a2fe365eab5510fd2585f9ead9e69d Mon Sep 17 00:00:00 2001 From: Paul Weidner Date: Thu, 10 Aug 2023 16:42:45 -0700 Subject: [PATCH] Refactor quick form route building to a single route --- modules/core/quick/farm_quick.links.task.yml | 11 ++- modules/core/quick/farm_quick.module | 9 +-- modules/core/quick/farm_quick.routing.yml | 15 +++- .../src/Controller/QuickFormController.php | 2 +- modules/core/quick/src/Form/QuickForm.php | 20 ++--- .../Plugin/Derivative/QuickFormMenuLink.php | 5 +- .../Plugin/Derivative/QuickFormTaskLink.php | 80 ------------------- .../quick/src/Routing/QuickFormRoutes.php | 73 ----------------- 8 files changed, 39 insertions(+), 176 deletions(-) delete mode 100644 modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php delete mode 100644 modules/core/quick/src/Routing/QuickFormRoutes.php diff --git a/modules/core/quick/farm_quick.links.task.yml b/modules/core/quick/farm_quick.links.task.yml index fc20fde76..f0391b10c 100644 --- a/modules/core/quick/farm_quick.links.task.yml +++ b/modules/core/quick/farm_quick.links.task.yml @@ -1,2 +1,9 @@ -farm.quick: - deriver: Drupal\farm_quick\Plugin\Derivative\QuickFormTaskLink +farm_quick.quick_form: + title: Quick Form + route_name: farm_quick.quick_form + base_route: farm_quick.quick_form + +farm_quick.configure: + title: Configure + route_name: farm_quick.configure + base_route: farm_quick.quick_form diff --git a/modules/core/quick/farm_quick.module b/modules/core/quick/farm_quick.module index 77fca33b5..31ce1c02a 100644 --- a/modules/core/quick/farm_quick.module +++ b/modules/core/quick/farm_quick.module @@ -20,12 +20,9 @@ function farm_quick_help($route_name, RouteMatchInterface $route_match) { } // Load help text for individual quick forms. - if (strpos($route_name, 'farm.quick.') === 0) { - $quick_form_id = $route_match->getParameter('id'); - if ($route_name == 'farm.quick.' . $quick_form_id) { - $quick_form = \Drupal::service('quick_form.instance_manager')->createInstance($quick_form_id); - $help_text = $quick_form->getHelpText(); - if (!empty($help_text)) { + if ($route_name == 'farm_quick.quick_form') { + if (($quick_form_id = $route_match->getParameter('quick_form')) && $quick_form = \Drupal::service('quick_form.instance_manager')->createInstance($quick_form_id)) { + if ($help_text = $quick_form->getHelpText()) { $output .= '

' . $help_text . '

'; } } diff --git a/modules/core/quick/farm_quick.routing.yml b/modules/core/quick/farm_quick.routing.yml index 6f4e2f8bc..a8db8257a 100644 --- a/modules/core/quick/farm_quick.routing.yml +++ b/modules/core/quick/farm_quick.routing.yml @@ -6,6 +6,18 @@ farm.quick: requirements: _permission: 'view quick forms index' +farm_quick.quick_form: + path: /quick/{quick_form} + defaults: + _form: \Drupal\farm_quick\Form\QuickForm + _title_callback: \Drupal\farm_quick\Form\QuickForm::getTitle + requirements: + _custom_access: \Drupal\farm_quick\Form\QuickForm::access + options: + parameters: + quick_form: + type: string + farm_quick.configure: path: /quick/{quick_form}/configure defaults: @@ -17,6 +29,3 @@ farm_quick.configure: parameters: quick_form: type: string - -route_callbacks: - - '\Drupal\farm_quick\Routing\QuickFormRoutes::routes' diff --git a/modules/core/quick/src/Controller/QuickFormController.php b/modules/core/quick/src/Controller/QuickFormController.php index 4fe9f3cf5..0baa0b9be 100644 --- a/modules/core/quick/src/Controller/QuickFormController.php +++ b/modules/core/quick/src/Controller/QuickFormController.php @@ -52,7 +52,7 @@ class QuickFormController extends ControllerBase { $quick_forms = $this->quickFormInstanceManager->getInstances(); $items = []; foreach ($quick_forms as $id => $quick_form) { - $url = Url::fromRoute('farm.quick.' . $id); + $url = Url::fromRoute('farm_quick.quick_form', ['quick_form' => $id]); if ($url->access()) { $items[] = [ 'title' => $quick_form->getLabel(), diff --git a/modules/core/quick/src/Form/QuickForm.php b/modules/core/quick/src/Form/QuickForm.php index 00c3ea150..4fd80caf5 100644 --- a/modules/core/quick/src/Form/QuickForm.php +++ b/modules/core/quick/src/Form/QuickForm.php @@ -61,7 +61,7 @@ class QuickForm extends FormBase implements BaseFormIdInterface { */ public function getFormId() { $form_id = $this->getBaseFormId(); - $id = $this->getRouteMatch()->getParameter('id'); + $id = $this->getRouteMatch()->getParameter('quick_form'); if (!is_null($id)) { $form_id .= '_' . $this->quickFormInstanceManager->createInstance($id)->getPlugin()->getFormId(); } @@ -71,14 +71,14 @@ class QuickForm extends FormBase implements BaseFormIdInterface { /** * Get the title of the quick form. * - * @param string $id + * @param string $quick_form * The quick form ID. * * @return string * Quick form title. */ - public function getTitle(string $id) { - return $this->quickFormInstanceManager->createInstance($id)->getLabel(); + public function getTitle(string $quick_form) { + return $this->quickFormInstanceManager->createInstance($quick_form)->getLabel(); } /** @@ -86,26 +86,26 @@ class QuickForm extends FormBase implements BaseFormIdInterface { * * @param \Drupal\Core\Session\AccountInterface $account * Run access checks for this account. - * @param string $id + * @param string $quick_form * The quick form ID. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ - public function access(AccountInterface $account, string $id) { - return $this->quickFormInstanceManager->createInstance($id)->getPlugin()->access($account); + public function access(AccountInterface $account, string $quick_form) { + return $this->quickFormInstanceManager->createInstance($quick_form)->getPlugin()->access($account); } /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) { + public function buildForm(array $form, FormStateInterface $form_state, $quick_form = NULL) { // Save the quick form ID. - $this->quickFormId = $id; + $this->quickFormId = $quick_form; // Load the quick form. - $form = $this->quickFormInstanceManager->createInstance($id)->getPlugin()->buildForm($form, $form_state); + $form = $this->quickFormInstanceManager->createInstance($quick_form)->getPlugin()->buildForm($form, $form_state); // Add a submit button, if one wasn't provided. if (empty($form['actions']['submit'])) { diff --git a/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php b/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php index 7bfae6c65..ea73dff6b 100644 --- a/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php +++ b/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php @@ -63,7 +63,10 @@ class QuickFormMenuLink extends DeriverBase implements ContainerDeriverInterface $links[$route_id] = [ 'title' => $quick_form->getLabel(), 'parent' => 'farm.quick:farm.quick', - 'route_name' => $route_id, + 'route_name' => 'farm_quick.quick_form', + 'route_parameters' => [ + 'quick_form' => $id, + ], ] + $base_plugin_definition; } diff --git a/modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php b/modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php deleted file mode 100644 index 2665f8b09..000000000 --- a/modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php +++ /dev/null @@ -1,80 +0,0 @@ -quickFormInstanceManager = $quick_form_instance_manager; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container, $base_plugin_id) { - return new static( - $container->get('quick_form.instance_manager') - ); - } - - /** - * {@inheritdoc} - */ - public function getDerivativeDefinitions($base_plugin_definition) { - $links = []; - - // Load quick forms. - $quick_forms = $this->quickFormInstanceManager->getInstances(); - - // Add links for each quick form. - foreach ($quick_forms as $id => $quick_form) { - $route_name = 'farm.quick.' . $id; - $links[$route_name] = [ - 'title' => $this->t('Quick form'), - 'route_name' => $route_name, - 'base_route' => $route_name, - 'weight' => 0, - ] + $base_plugin_definition; - - // If the quick form is configurable, add a link to the config form. - if ($quick_form->getPlugin()->isConfigurable()) { - $links["farm.quick.$id.configure"] = [ - 'title' => $this->t('Configure'), - 'route_name' => 'farm_quick.configure', - 'route_parameters' => [ - 'quick_form' => $id, - ], - 'base_route' => $route_name, - 'weight' => 100, - ] + $base_plugin_definition; - } - } - - return $links; - } - -} diff --git a/modules/core/quick/src/Routing/QuickFormRoutes.php b/modules/core/quick/src/Routing/QuickFormRoutes.php deleted file mode 100644 index 014ffbed4..000000000 --- a/modules/core/quick/src/Routing/QuickFormRoutes.php +++ /dev/null @@ -1,73 +0,0 @@ -quickFormInstanceManager = $quick_form_instance_manager; - } - - /** - * {@inheritdoc} - */ - public static function create(ContainerInterface $container) { - return new static( - $container->get('quick_form.instance_manager'), - ); - } - - /** - * Provides routes for quick forms. - * - * @return \Symfony\Component\Routing\RouteCollection - * Returns a route collection. - */ - public function routes(): RouteCollection { - $route_collection = new RouteCollection(); - /** @var \Drupal\farm_quick\Entity\QuickFormInstanceInterface[] $quick_forms */ - $quick_forms = $this->quickFormInstanceManager->getInstances(); - foreach ($quick_forms as $id => $quick_form) { - - // Build a route for the quick form. - $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; - } - -}