Skip to content
Snippets Groups Projects
Commit 62581af1 authored by Jaydev Bhatt's avatar Jaydev Bhatt
Browse files

Issue #3281676: Update Views’ paths dynamically based on admin path setting

parent e3de8a8d
No related branches found
No related tags found
1 merge request!28Issue #3281676: Update Views’ paths dynamically based on admin path setting
......@@ -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();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment