From e065469af6306bc7f28846cda84553678a48f4b5 Mon Sep 17 00:00:00 2001 From: Mingsong Hu <mingsonghu@Mingsongs-MBP.home> Date: Mon, 16 Sep 2019 14:57:06 +1000 Subject: [PATCH] Use Url::fromRoute() instead of hardcode URL string --- src/Controller/HmMenuController.php | 3 --- src/Controller/HmTaxonomyController.php | 9 ++++++++- src/Form/HmMenuForm.php | 16 +++++----------- src/Form/HmOverviewTerms.php | 16 +++++++++------- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/src/Controller/HmMenuController.php b/src/Controller/HmMenuController.php index d101c2e..d76abfc 100644 --- a/src/Controller/HmMenuController.php +++ b/src/Controller/HmMenuController.php @@ -114,9 +114,6 @@ class HmMenuController extends ControllerBase { $parent = ''; } - if (empty($destination)) { - $destination = ''; - } // We indicate that a menu administrator is running the menu access check. $request->attributes->set('_menu_admin', TRUE); diff --git a/src/Controller/HmTaxonomyController.php b/src/Controller/HmTaxonomyController.php index 536b1f4..eb70be9 100644 --- a/src/Controller/HmTaxonomyController.php +++ b/src/Controller/HmTaxonomyController.php @@ -79,6 +79,7 @@ class HmTaxonomyController extends ControllerBase { } $parent = $request->get('parent') ?: 0; $depth = $request->get('depth'); + $destination = $request->get('destination'); if(!empty($depth)) { $depth = intval($depth); @@ -95,12 +96,18 @@ class HmTaxonomyController extends ControllerBase { if ($term instanceof Term) { // User can only access the terms that they can update. if ($access_control_handler->access($term, 'update')) { + if (empty($destination)) { + $url = $term->toUrl('edit-form')->toString(); + } + else { + $url = $term->toUrl('edit-form', ['query' => ['destination' => $destination]])->toString(); + } $term_array[] = $this->hmPluginTypeManager->buildHierarchyItem( $term->id(), $term->label(), $term->parents[0], - $term->toUrl('edit-form')->toString()); + $url); } } } diff --git a/src/Form/HmMenuForm.php b/src/Form/HmMenuForm.php index 0476df8..1295c7e 100644 --- a/src/Form/HmMenuForm.php +++ b/src/Form/HmMenuForm.php @@ -2,6 +2,7 @@ namespace Drupal\hierarchy_manager\Form; +use Drupal\Core\Url; use Drupal\Core\Form\FormStateInterface; use Drupal\menu_ui\MenuForm; @@ -80,24 +81,17 @@ class HmMenuForm extends MenuForm { $mid = $this->entity->id(); // CSRF token. $token = \Drupal::csrfToken()->get($mid); - // Get current language. - $language = \Drupal::languageManager()->getCurrentLanguage(); // Destination for edit link. $destination = $this->getDestinationArray(); if (isset($destination['destination'])) { $destination = $destination['destination']; } else { - $destination = ''; - } - if ($language->isDefault()) { - $source_url = $base_path . 'admin/hierarchy_manager/menu/json/' . $mid . '?token=' . $token . '&destination=' . $destination; - $update_url = $base_path . 'admin/hierarchy_manager/menu/update/' . $mid . '?token=' . $token; - } - else { - $source_url = $base_path . $language->getId() . '/admin/hierarchy_manager/menu/json/' . $mid . '?token=' . $token . '&destination=' . $destination; - $update_url = $base_path . $language->getId() . '/admin/hierarchy_manager/menu/update/' . $mid . '?token=' . $token; + $destination = '/'; } + // Urls + $source_url = Url::fromRoute('hierarchy_manager.menu.tree.json', ['mid' => $mid], ['query' => ['token' => $token, 'destination' => $destination]])->toString(); + $update_url = Url::fromRoute('hierarchy_manager.menu.tree.update', ['mid' => $mid], ['query' => ['token' => $token]])->toString(); return $display_plugin_instance->getForm($source_url, $update_url, $form, $form_state); } } diff --git a/src/Form/HmOverviewTerms.php b/src/Form/HmOverviewTerms.php index f4f632c..c6772a0 100644 --- a/src/Form/HmOverviewTerms.php +++ b/src/Form/HmOverviewTerms.php @@ -2,6 +2,7 @@ namespace Drupal\hierarchy_manager\Form; +use Drupal\Core\Url; use Drupal\Core\Form\FormStateInterface; use Drupal\taxonomy\VocabularyInterface; use Drupal\taxonomy\Form\OverviewTerms; @@ -51,16 +52,17 @@ class HmOverviewTerms extends OverviewTerms { $vid = $taxonomy_vocabulary->id(); // CSRF token. $token = \Drupal::csrfToken()->get($vid); - // Get current language. - $language = \Drupal::languageManager()->getCurrentLanguage(); - if ($language->isDefault()) { - $source_url = $base_path . 'admin/hierarchy_manager/taxonomy/json/' . $vid . '?token=' . $token; - $update_url = $base_path . 'admin/hierarchy_manager/taxonomy/update/' . $vid . '?token=' . $token; + // Destination for edit link. + $destination = $this->getDestinationArray(); + if (isset($destination['destination'])) { + $destination = $destination['destination']; } else { - $source_url = $base_path . $language->getId() . '/admin/hierarchy_manager/taxonomy/json/' . $vid . '?token=' . $token; - $update_url = $base_path . $language->getId() . '/admin/hierarchy_manager/taxonomy/update/' . $vid . '?token=' . $token; + $destination = '/'; } + // Urls. + $source_url = Url::fromRoute('hierarchy_manager.taxonomy.tree.json', ['vid' => $vid], ['query' => ['token' => $token, 'destination' => $destination]])->toString(); + $update_url = Url::fromRoute('hierarchy_manager.taxonomy.tree.update', ['vid' => $vid], ['query' => ['token' => $token]])->toString(); return $instance->getForm($source_url, $update_url, $form, $form_state); } } -- GitLab