From 62581af1896adf088a30510e19a8958d7d4d3761 Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Fri, 7 Mar 2025 15:23:07 +0530 Subject: [PATCH 01/10] =?UTF-8?q?Issue=20#3281676:=20Update=20Views?= =?UTF-8?q?=E2=80=99=20paths=20dynamically=20based=20on=20admin=20path=20s?= =?UTF-8?q?etting?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Form/RenameAdminPathsSettingsForm.php | 64 +++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/src/Form/RenameAdminPathsSettingsForm.php b/src/Form/RenameAdminPathsSettingsForm.php index e43beca..defdb7f 100644 --- a/src/Form/RenameAdminPathsSettingsForm.php +++ b/src/Form/RenameAdminPathsSettingsForm.php @@ -13,6 +13,7 @@ use Drupal\Core\StringTranslation\StringTranslationTrait; use Drupal\rename_admin_paths\Config; use Drupal\rename_admin_paths\EventSubscriber\RenameAdminPathsEventSubscriber; use Symfony\Component\DependencyInjection\ContainerInterface; +use Drupal\views\Entity\View; /** * Settings form for the Rename Admin Paths module. @@ -157,10 +158,21 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { #[\Override] public function submitForm(array &$form, FormStateInterface $form_state): void { $this->saveConfiguration($form_state); + $dynamic_prefix = $form_state->getValue('admin_path_value'); // At this stage we rebuild all routes to use the new renamed paths. $this->routeBuilder->rebuild(); + // Check if the admin path has been disabled (unchecked). + if (!$form_state->getValue('admin_path')) { + // If the path is disabled, revert the views' paths to the original ones. + $this->updateOrRevertViewPaths(TRUE, $dynamic_prefix); + } + else { + // If enabled, update the views' paths to reflect the new value. + $this->updateOrRevertViewPaths(FALSE, $dynamic_prefix); + } + // Add confirmation message. parent::submitForm($form, $form_state); @@ -188,4 +200,56 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { $this->config->save(); } + /** + * Updates or reverts the view paths based on the given flag. + * + * @param bool $revert + * If TRUE, reverts paths from the dynamic prefix to 'admin'. + * If FALSE, updates paths from 'admin' to the dynamic prefix. + * @param string $dynamic_prefix + * The dynamic prefix (e.g., 'backend') to use in the path. + */ + private function updateOrRevertViewPaths(bool $revert = FALSE, string $dynamic_prefix): void { + // Load all views. + $views = View::loadMultiple(); + + // Iterate through all the views. + foreach ($views as $view) { + // Get the executable view object. + $executable = $view->getExecutable(); + + // Loop through all displays in the view. + foreach ($view->get('display') as $display_id => $display) { + // Check if the display has a path. + if (isset($display['display_options']['path'])) { + $current_path = $display['display_options']['path']; + + // Modify the path based on whether we're updating or reverting. + if ($revert && strpos($current_path, $dynamic_prefix . '/') === 0) { + // Revert the dynamic prefix to 'admin'. + $new_path = str_replace($dynamic_prefix, 'admin', $current_path); + } + elseif (!$revert && strpos($current_path, 'admin/') === 0) { + // Update 'admin' to the dynamic prefix. + $new_path = str_replace('admin', $dynamic_prefix, $current_path); + } + else { + // Skip if no change is needed. + continue; + } + + // Set the display and update the path. + $executable->setDisplay($display_id); + $executable->display_handler->setOption('path', $new_path); + } + } + + // Save the updated view. + $view->save(); + } + + // Optionally clear the cache to ensure the changes are reflected. + \Drupal::cache()->deleteAll(); + } + } -- GitLab From 7080fe2c6a0fa4172557519a476e86587119aca6 Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Fri, 7 Mar 2025 16:45:02 +0530 Subject: [PATCH 02/10] Issue #3281676: Fix PHPCS and PHPstan issues. --- src/Form/RenameAdminPathsSettingsForm.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Form/RenameAdminPathsSettingsForm.php b/src/Form/RenameAdminPathsSettingsForm.php index defdb7f..8be9502 100644 --- a/src/Form/RenameAdminPathsSettingsForm.php +++ b/src/Form/RenameAdminPathsSettingsForm.php @@ -14,6 +14,7 @@ use Drupal\rename_admin_paths\Config; use Drupal\rename_admin_paths\EventSubscriber\RenameAdminPathsEventSubscriber; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\views\Entity\View; +use Drupal\Core\Cache\CacheBackendInterface; /** * Settings form for the Rename Admin Paths module. @@ -27,6 +28,7 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { TypedConfigManagerInterface $typed_config_manager, private readonly Config $config, private readonly RouteBuilderInterface $routeBuilder, + private readonly CacheBackendInterface $cache, ) { parent::__construct($config_factory, $typed_config_manager); } @@ -41,6 +43,7 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { $container->get('config.typed'), $container->get(Config::class), $container->get('router.builder'), + $container->get('cache.default'), ); } @@ -127,7 +130,6 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { * The form state. */ public function validate(array &$element, FormStateInterface $formState): void { - // @phpstan-ignore-next-line Permit this use of empty. if (empty($element['#value'])) { $formState->setError( $element, @@ -203,13 +205,13 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { /** * Updates or reverts the view paths based on the given flag. * + * @param string $dynamic_prefix + * The dynamic prefix (e.g., 'backend') to use in the path. * @param bool $revert * If TRUE, reverts paths from the dynamic prefix to 'admin'. * If FALSE, updates paths from 'admin' to the dynamic prefix. - * @param string $dynamic_prefix - * The dynamic prefix (e.g., 'backend') to use in the path. */ - private function updateOrRevertViewPaths(bool $revert = FALSE, string $dynamic_prefix): void { + private function updateOrRevertViewPaths(string $dynamic_prefix, bool $revert = FALSE): void { // Load all views. $views = View::loadMultiple(); @@ -249,7 +251,7 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { } // Optionally clear the cache to ensure the changes are reflected. - \Drupal::cache()->deleteAll(); + $this->cache->deleteAll(); } } -- GitLab From 2a1aa0e92d41d896e10ed691c4202c5f838cd17e Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Fri, 7 Mar 2025 16:55:03 +0530 Subject: [PATCH 03/10] Issue #3281676: Fix PHPCS and PHPstan issues. --- src/Form/RenameAdminPathsSettingsForm.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Form/RenameAdminPathsSettingsForm.php b/src/Form/RenameAdminPathsSettingsForm.php index 8be9502..e9e0950 100644 --- a/src/Form/RenameAdminPathsSettingsForm.php +++ b/src/Form/RenameAdminPathsSettingsForm.php @@ -130,7 +130,7 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { * The form state. */ public function validate(array &$element, FormStateInterface $formState): void { - if (empty($element['#value'])) { + if ($element['#value'] === '' || $element['#value'] === NULL) { $formState->setError( $element, $this->t('Path replacement value must contain a value.') @@ -166,13 +166,13 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase { $this->routeBuilder->rebuild(); // Check if the admin path has been disabled (unchecked). - if (!$form_state->getValue('admin_path')) { + if ((bool) $form_state->getValue('admin_path') === FALSE) { // If the path is disabled, revert the views' paths to the original ones. - $this->updateOrRevertViewPaths(TRUE, $dynamic_prefix); + $this->updateOrRevertViewPaths($dynamic_prefix, TRUE); } else { // If enabled, update the views' paths to reflect the new value. - $this->updateOrRevertViewPaths(FALSE, $dynamic_prefix); + $this->updateOrRevertViewPaths($dynamic_prefix, FALSE); } // Add confirmation message. -- GitLab From fa9114be02b71bac004a0fb9d8fbdcd37042584c Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Fri, 7 Mar 2025 17:19:01 +0530 Subject: [PATCH 04/10] Issue #3281676: Fix PHPCS and PHPstan issues for the unit test file as well. --- tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php b/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php index 25d151f..ed07113 100644 --- a/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php +++ b/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php @@ -13,6 +13,7 @@ use Drupal\rename_admin_paths\Config; use Drupal\rename_admin_paths\Form\RenameAdminPathsSettingsForm; use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; +use Drupal\Core\Cache\CacheBackendInterface; /** * Tests the behavior of the module settings form. @@ -107,8 +108,10 @@ class RenameAdminPathsSettingsFormTest extends UnitTestCase { $routeBuilder = $this->createMock(RouteBuilderInterface::class); + $cache = $this->createMock(CacheBackendInterface::class); + $settings_form = new RenameAdminPathsSettingsForm( - $config_factory, $typed_config_manager, $config, $routeBuilder, + $config_factory, $typed_config_manager, $config, $routeBuilder, $cache ); // Inject StringTranslationTrait. $settings_form->setStringTranslation($this->getStringTranslationStub()); -- GitLab From a846bb91e75d7e8ca92c14ee334b286e41d6d527 Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Fri, 7 Mar 2025 17:30:54 +0530 Subject: [PATCH 05/10] Issue #3281676: Fix php unit admin form issue. --- tests/src/Functional/AdminFormTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Functional/AdminFormTest.php b/tests/src/Functional/AdminFormTest.php index 4e7ccc3..3475d24 100644 --- a/tests/src/Functional/AdminFormTest.php +++ b/tests/src/Functional/AdminFormTest.php @@ -21,7 +21,7 @@ class AdminFormTest extends BrowserTestBase { /** * {@inheritdoc} */ - protected static $modules = ['rename_admin_paths']; + protected static $modules = ['views', 'rename_admin_paths']; /** * Setup admin user. -- GitLab From bc20c185b5c8bb86ead28da96d7bfde0667ae440 Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Fri, 7 Mar 2025 17:38:13 +0530 Subject: [PATCH 06/10] =?UTF-8?q?Issue=20#3281676:=20Fix=20undefined=20arr?= =?UTF-8?q?ay=20key=20=E2=80=9C#value=E2=80=9D=20in=20RenameAdminPathsSett?= =?UTF-8?q?ingsForm=20test.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php b/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php index ed07113..f7c2cf0 100644 --- a/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php +++ b/tests/src/Unit/Form/RenameAdminPathsSettingsFormTest.php @@ -28,7 +28,7 @@ class RenameAdminPathsSettingsFormTest extends UnitTestCase { * Test when an invalid value is provided. */ public function testValidatePathWithoutValue(): void { - $element = []; + $element = ['#value' => '']; $this->getForm()->validate($element, $this->getInvalidFormState()); } -- GitLab From f4f9b8a091c5011c30a9c8cf59ec1a0790f78901 Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Mon, 10 Mar 2025 11:52:45 +0530 Subject: [PATCH 07/10] Issue #3281676: Update uninstall hook to revert custom admin paths in views and clear render cache. --- rename_admin_paths.install | 46 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 rename_admin_paths.install diff --git a/rename_admin_paths.install b/rename_admin_paths.install new file mode 100644 index 0000000..c64b2e3 --- /dev/null +++ b/rename_admin_paths.install @@ -0,0 +1,46 @@ +<?php + +/** + * @file + * Uninstall hook for the Rename Admin Paths module. + */ + +declare(strict_types=1); + +use Drupal\views\Entity\View; + +/** + * Implements hook_uninstall(). + */ +function rename_admin_paths_uninstall(): void { + // Load the config to get the current custom admin path. + $config = \Drupal::config('rename_admin_paths.settings'); + $custom_admin_path = $config->get('admin_path_value') ?? 'admin'; + + // Load all views. + $views = View::loadMultiple(); + + foreach ($views as $view) { + $executable = $view->getExecutable(); + + foreach ($view->get('display') as $display_id => $display) { + if (!empty($display['display_options']['path'])) { + $current_path = $display['display_options']['path']; + + // If the path starts with the custom prefix, revert it back to 'admin'. + if (str_starts_with($current_path, $custom_admin_path . '/')) { + $new_path = str_replace($custom_admin_path, 'admin', $current_path); + + $executable->setDisplay($display_id); + $executable->display_handler->setOption('path', $new_path); + } + } + } + + // Save the updated view. + $view->save(); + } + + // Clear the render cache to reflect the changes. + \Drupal::service('cache.render')->invalidateAll(); +} -- GitLab From 9b66fa1ae1559371b9be0862f3487b13dbc96f63 Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Mon, 10 Mar 2025 12:25:34 +0530 Subject: [PATCH 08/10] Issue #3281676: Replace empty() with strict comparison for path check in uninstall hook --- rename_admin_paths.install | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/rename_admin_paths.install b/rename_admin_paths.install index c64b2e3..5255dc6 100644 --- a/rename_admin_paths.install +++ b/rename_admin_paths.install @@ -2,7 +2,6 @@ /** * @file - * Uninstall hook for the Rename Admin Paths module. */ declare(strict_types=1); @@ -24,7 +23,7 @@ function rename_admin_paths_uninstall(): void { $executable = $view->getExecutable(); foreach ($view->get('display') as $display_id => $display) { - if (!empty($display['display_options']['path'])) { + if (isset($display['display_options']['path']) && $display['display_options']['path'] !== '') { $current_path = $display['display_options']['path']; // If the path starts with the custom prefix, revert it back to 'admin'. -- GitLab From 1f63d35b995e347e13dc487a47d835dbeb91639f Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Mon, 10 Mar 2025 12:57:04 +0530 Subject: [PATCH 09/10] Issue #3281676: Add missing short description in the doc comment of uninstall hook. --- rename_admin_paths.install | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rename_admin_paths.install b/rename_admin_paths.install index 5255dc6..e48625b 100644 --- a/rename_admin_paths.install +++ b/rename_admin_paths.install @@ -2,6 +2,7 @@ /** * @file + * Provides the uninstall hook for the Rename Admin Paths module. */ declare(strict_types=1); @@ -10,6 +11,8 @@ use Drupal\views\Entity\View; /** * Implements hook_uninstall(). + * + * Reverts custom admin paths in views back to 'admin' on module uninstall. */ function rename_admin_paths_uninstall(): void { // Load the config to get the current custom admin path. -- GitLab From be21457c8e7ac1e252aa2afde640e97bdd57b8bf Mon Sep 17 00:00:00 2001 From: Jaydev Bhatt <jaydev.bhatt@qed42.com> Date: Mon, 10 Mar 2025 13:42:37 +0530 Subject: [PATCH 10/10] Issue #3281676: Replace deprecated invalidateAll() with deleteAll() in uninstall hook.. --- rename_admin_paths.install | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rename_admin_paths.install b/rename_admin_paths.install index e48625b..962d4f3 100644 --- a/rename_admin_paths.install +++ b/rename_admin_paths.install @@ -44,5 +44,5 @@ function rename_admin_paths_uninstall(): void { } // Clear the render cache to reflect the changes. - \Drupal::service('cache.render')->invalidateAll(); + \Drupal::service('cache_tags.invalidator')->invalidateTags(['rendered']); } -- GitLab