Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rename_admin_paths
Manage
Activity
Members
Labels
Plan
Wiki
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Model registry
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
project
rename_admin_paths
Commits
62581af1
Commit
62581af1
authored
2 months ago
by
Jaydev Bhatt
Browse files
Options
Downloads
Patches
Plain Diff
Issue
#3281676
: Update Views’ paths dynamically based on admin path setting
parent
e3de8a8d
No related branches found
No related tags found
1 merge request
!28
Issue #3281676: Update Views’ paths dynamically based on admin path setting
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Form/RenameAdminPathsSettingsForm.php
+64
-0
64 additions, 0 deletions
src/Form/RenameAdminPathsSettingsForm.php
with
64 additions
and
0 deletions
src/Form/RenameAdminPathsSettingsForm.php
+
64
−
0
View file @
62581af1
...
...
@@ -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
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment