From 96b6002f5d256eae755df626b7667f92cb5bce09 Mon Sep 17 00:00:00 2001 From: Michael Stenta Date: Mon, 9 Oct 2023 08:44:57 -0400 Subject: [PATCH] Quick form actions cause RouteNotFoundException: Route farm.quick.[id] does not exist. #727 Revert "Refactor quick form route building to a single route" This reverts commit 3505e80124a2fe365eab5510fd2585f9ead9e69d. --- CHANGELOG.md | 1 + 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 | 72 +++++++++++++++++ 9 files changed, 176 insertions(+), 39 deletions(-) create mode 100644 modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php create mode 100644 modules/core/quick/src/Routing/QuickFormRoutes.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 978dd9463..ab52b713e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - [Fix asset_lookup and term_lookup exception messages #731](https://github.com/farmOS/farmOS/pull/731) - [Prevent saving invalid ID tag types #725](https://github.com/farmOS/farmOS/issues/725) +- [Quick form actions cause RouteNotFoundException: Route farm.quick.[id] does not exist. #727](https://github.com/farmOS/farmOS/issues/727) ## [2.2.0] 2023-10-06 diff --git a/modules/core/quick/farm_quick.links.task.yml b/modules/core/quick/farm_quick.links.task.yml index f0391b10c..fc20fde76 100644 --- a/modules/core/quick/farm_quick.links.task.yml +++ b/modules/core/quick/farm_quick.links.task.yml @@ -1,9 +1,2 @@ -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 +farm.quick: + deriver: Drupal\farm_quick\Plugin\Derivative\QuickFormTaskLink diff --git a/modules/core/quick/farm_quick.module b/modules/core/quick/farm_quick.module index 44b73018b..56024e9ab 100644 --- a/modules/core/quick/farm_quick.module +++ b/modules/core/quick/farm_quick.module @@ -20,9 +20,12 @@ function farm_quick_help($route_name, RouteMatchInterface $route_match) { } // Load help text for individual quick forms. - if ($route_name == 'farm_quick.quick_form') { - if (($quick_form_id = $route_match->getParameter('quick_form')) && $quick_form = \Drupal::service('quick_form.instance_manager')->getInstance($quick_form_id)) { - if ($help_text = $quick_form->getHelpText()) { + 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')->getInstance($quick_form_id); + $help_text = $quick_form->getHelpText(); + if (!empty($help_text)) { $output .= '

' . $help_text . '

'; } } diff --git a/modules/core/quick/farm_quick.routing.yml b/modules/core/quick/farm_quick.routing.yml index 542de546c..227697e6f 100644 --- a/modules/core/quick/farm_quick.routing.yml +++ b/modules/core/quick/farm_quick.routing.yml @@ -6,18 +6,6 @@ farm.quick: requirements: _permission: 'view quick_form' -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: @@ -29,3 +17,6 @@ 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 b75661fa6..753821b77 100644 --- a/modules/core/quick/src/Controller/QuickFormController.php +++ b/modules/core/quick/src/Controller/QuickFormController.php @@ -59,7 +59,7 @@ class QuickFormController extends ControllerBase { $quick_forms = $this->quickFormInstanceManager->getInstances(); $items = []; foreach ($quick_forms as $id => $quick_form) { - $url = Url::fromRoute('farm_quick.quick_form', ['quick_form' => $id]); + $url = Url::fromRoute('farm.quick.' . $id); if ($url->access()) { $cacheability->addCacheableDependency($quick_form); $items[] = [ diff --git a/modules/core/quick/src/Form/QuickForm.php b/modules/core/quick/src/Form/QuickForm.php index 1ea2c3960..2e2913000 100644 --- a/modules/core/quick/src/Form/QuickForm.php +++ b/modules/core/quick/src/Form/QuickForm.php @@ -62,7 +62,7 @@ class QuickForm extends FormBase implements BaseFormIdInterface { */ public function getFormId() { $form_id = $this->getBaseFormId(); - $id = $this->getRouteMatch()->getParameter('quick_form'); + $id = $this->getRouteMatch()->getParameter('id'); if (!is_null($id)) { $form_id .= '_' . $this->quickFormInstanceManager->getInstance($id)->getPlugin()->getFormId(); } @@ -72,14 +72,14 @@ class QuickForm extends FormBase implements BaseFormIdInterface { /** * Get the title of the quick form. * - * @param string $quick_form + * @param string $id * The quick form ID. * * @return string * Quick form title. */ - public function getTitle(string $quick_form) { - return $this->quickFormInstanceManager->getInstance($quick_form)->getLabel(); + public function getTitle(string $id) { + return $this->quickFormInstanceManager->getInstance($id)->getLabel(); } /** @@ -87,14 +87,14 @@ class QuickForm extends FormBase implements BaseFormIdInterface { * * @param \Drupal\Core\Session\AccountInterface $account * Run access checks for this account. - * @param string $quick_form + * @param string $id * The quick form ID. * * @return \Drupal\Core\Access\AccessResultInterface * The access result. */ - public function access(AccountInterface $account, string $quick_form) { - if ($quick_form = $this->quickFormInstanceManager->getInstance($quick_form)) { + public function access(AccountInterface $account, string $id) { + if ($quick_form = $this->quickFormInstanceManager->getInstance($id)) { return $quick_form->getPlugin()->access($account); } @@ -105,13 +105,13 @@ class QuickForm extends FormBase implements BaseFormIdInterface { /** * {@inheritdoc} */ - public function buildForm(array $form, FormStateInterface $form_state, $quick_form = NULL) { + public function buildForm(array $form, FormStateInterface $form_state, $id = NULL) { // Save the quick form ID. - $this->quickFormId = $quick_form; + $this->quickFormId = $id; // Load the quick form. - $form = $this->quickFormInstanceManager->getInstance($quick_form)->getPlugin()->buildForm($form, $form_state); + $form = $this->quickFormInstanceManager->getInstance($id)->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 ea73dff6b..7bfae6c65 100644 --- a/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php +++ b/modules/core/quick/src/Plugin/Derivative/QuickFormMenuLink.php @@ -63,10 +63,7 @@ class QuickFormMenuLink extends DeriverBase implements ContainerDeriverInterface $links[$route_id] = [ 'title' => $quick_form->getLabel(), 'parent' => 'farm.quick:farm.quick', - 'route_name' => 'farm_quick.quick_form', - 'route_parameters' => [ - 'quick_form' => $id, - ], + 'route_name' => $route_id, ] + $base_plugin_definition; } diff --git a/modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php b/modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php new file mode 100644 index 000000000..2665f8b09 --- /dev/null +++ b/modules/core/quick/src/Plugin/Derivative/QuickFormTaskLink.php @@ -0,0 +1,80 @@ +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 new file mode 100644 index 000000000..aba2759e5 --- /dev/null +++ b/modules/core/quick/src/Routing/QuickFormRoutes.php @@ -0,0 +1,72 @@ +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; + } + +}