Skip to content
Snippets Groups Projects

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

Files
4
@@ -13,6 +13,8 @@ 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;
use Drupal\Core\Cache\CacheBackendInterface;
/**
* Settings form for the Rename Admin Paths module.
@@ -26,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);
}
@@ -40,6 +43,7 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase {
$container->get('config.typed'),
$container->get(Config::class),
$container->get('router.builder'),
$container->get('cache.default'),
);
}
@@ -126,8 +130,7 @@ 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'])) {
if ($element['#value'] === '' || $element['#value'] === NULL) {
$formState->setError(
$element,
$this->t('Path replacement value must contain a value.')
@@ -157,10 +160,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 ((bool) $form_state->getValue('admin_path') === FALSE) {
// If the path is disabled, revert the views' paths to the original ones.
$this->updateOrRevertViewPaths($dynamic_prefix, TRUE);
}
else {
// If enabled, update the views' paths to reflect the new value.
$this->updateOrRevertViewPaths($dynamic_prefix, FALSE);
}
// Add confirmation message.
parent::submitForm($form, $form_state);
@@ -188,4 +202,56 @@ final class RenameAdminPathsSettingsForm extends ConfigFormBase {
$this->config->save();
}
/**
* 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.
*/
private function updateOrRevertViewPaths(string $dynamic_prefix, bool $revert = FALSE): 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.
$this->cache->deleteAll();
}
}
Loading