Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
frontend_routing
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
frontend_routing
Commits
615238bd
Commit
615238bd
authored
11 months ago
by
Jon Minder
Browse files
Options
Downloads
Patches
Plain Diff
Support generic node patterns.
parent
baea6d59
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
frontend_routing.module
+22
-6
22 additions, 6 deletions
frontend_routing.module
src/Form/SettingsForm.php
+48
-5
48 additions, 5 deletions
src/Form/SettingsForm.php
with
70 additions
and
11 deletions
frontend_routing.module
+
22
−
6
View file @
615238bd
...
...
@@ -10,11 +10,27 @@
*/
function
frontend_routing_form_node_form_alter
(
&
$form
,
&
$form_state
)
{
$nodes
=
\Drupal
::
state
()
->
get
(
'frontend_routing.settings'
);
$nodes
=
array_combine
(
$nodes
,
$nodes
);
$node
=
$form_state
->
getFormObject
()
->
getEntity
();
if
(
!
$node
->
isNew
()
&&
in_array
(
$node
->
id
(),
$nodes
))
{
$form
[
'path'
][
'#access'
]
=
FALSE
;
$form
[
'path_replace'
][
'#weight'
]
=
$form
[
'path'
][
'#weight'
];
$form
[
'path_replace'
][
'#markup'
]
=
'<strong>'
.
t
(
'This node is managed by Frontend Routing.'
)
.
'</strong>'
;
// Check if the alias matches for a single alias.
if
(
!
empty
(
$nodes
))
{
$nodes
=
array_combine
(
$nodes
,
$nodes
);
$node
=
$form_state
->
getFormObject
()
->
getEntity
();
if
(
!
$node
->
isNew
()
&&
in_array
(
$node
->
id
(),
$nodes
))
{
$form
[
'path'
][
'#access'
]
=
FALSE
;
$form
[
'path_replace'
][
'#weight'
]
=
$form
[
'path'
][
'#weight'
];
$form
[
'path_replace'
][
'#markup'
]
=
'<strong>'
.
t
(
'This node is managed by Frontend Routing.'
)
.
'</strong>'
;
}
}
// Check if the alias matches for the pattern.
$keys
=
\Drupal
::
config
(
'frontend_routing.settings'
)
->
get
(
'keys'
)
??
[];
foreach
(
$keys
as
$key
=>
$value
)
{
if
(
empty
(
$value
[
'entity_id'
])
&&
!
empty
(
$value
[
'bundle'
]))
{
if
(
!
$node
->
isNew
()
&&
$node
->
bundle
()
===
$value
[
'bundle'
])
{
$form
[
'path'
][
'#access'
]
=
FALSE
;
$form
[
'path_replace'
][
'#weight'
]
=
$form
[
'path'
][
'#weight'
];
$form
[
'path_replace'
][
'#markup'
]
=
'<strong>'
.
t
(
'This node is managed by Frontend Routing.'
)
.
'</strong>'
;
}
}
}
}
This diff is collapsed.
Click to expand it.
src/Form/SettingsForm.php
+
48
−
5
View file @
615238bd
...
...
@@ -5,6 +5,7 @@ declare(strict_types=1);
namespace
Drupal\frontend_routing\Form
;
use
Drupal\Core\Config\ConfigFactoryInterface
;
use
Drupal\Core\Entity\EntityTypeBundleInfoInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Form\ConfigFormBase
;
use
Drupal\Core\Form\FormStateInterface
;
...
...
@@ -23,7 +24,8 @@ final class SettingsForm extends ConfigFormBase {
public
function
__construct
(
protected
ConfigFactoryInterface
$config_factory
,
protected
StateInterface
$state
,
protected
EntityTypeManagerInterface
$entityTypeManager
protected
EntityTypeManagerInterface
$entityTypeManager
,
protected
EntityTypeBundleInfoInterface
$entityTypeBundleInfo
,
)
{
parent
::
__construct
(
$config_factory
);
}
...
...
@@ -36,6 +38,7 @@ final class SettingsForm extends ConfigFormBase {
$container
->
get
(
'config.factory'
),
$container
->
get
(
'state'
),
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'entity_type.bundle.info'
),
);
}
...
...
@@ -93,11 +96,29 @@ final class SettingsForm extends ConfigFormBase {
'#disabled'
=>
TRUE
,
];
$form
[
'existing'
][
'table'
][
$key
][
'bundle'
]
=
[
'#title'
=>
$this
->
t
(
'Node Type'
),
'#type'
=>
'select'
,
'#options'
=>
$this
->
getNodeTypes
(),
'#required'
=>
TRUE
,
'#default_value'
=>
!
empty
(
$keys
[
$key
][
'bundle'
])
?
$keys
[
$key
][
'bundle'
]
:
NULL
,
];
$required
=
TRUE
;
// If we have a generic alias, we do not require a node.
if
(
!
empty
(
$aliases
))
{
$alias
=
reset
(
$aliases
);
if
(
!
empty
(
$alias
)
&&
strpos
(
$alias
,
'/:slug'
)
!==
FALSE
)
{
$required
=
FALSE
;
}
}
$form
[
'existing'
][
'table'
][
$key
][
'entity_id'
]
=
[
'#title'
=>
$this
->
t
(
'Node'
),
'#type'
=>
'entity_autocomplete'
,
'#target_type'
=>
'node'
,
'#required'
=>
TRUE
,
'#required'
=>
$required
,
'#disabled'
=>
!
$required
,
'#default_value'
=>
!
empty
(
$nodes
[
$key
])
?
$this
->
entityTypeManager
->
getStorage
(
'node'
)
->
load
(
$nodes
[
$key
])
:
NULL
,
];
...
...
@@ -160,6 +181,9 @@ final class SettingsForm extends ConfigFormBase {
public
function
validateForm
(
array
&
$form
,
FormStateInterface
$form_state
):
void
{
if
(
$form_state
->
getValue
(
'key'
))
{
if
(
empty
(
$form_state
->
getValue
(
'bundle'
)))
{
$form_state
->
setErrorByName
(
'bundle'
,
$this
->
t
(
'Node Type required.'
));
}
if
(
empty
(
$form_state
->
getValue
(
'entity_id'
)))
{
$form_state
->
setErrorByName
(
'entity_id'
,
$this
->
t
(
'Node is required.'
));
}
...
...
@@ -204,7 +228,7 @@ final class SettingsForm extends ConfigFormBase {
public
function
submitForm
(
array
&
$form
,
FormStateInterface
$form_state
):
void
{
$nodes
=
[];
// Save data from the table
// Save data from the table
.
$keys
=
$this
->
config
(
'frontend_routing.settings'
)
->
get
(
'keys'
)
??
[];
// Add a new item.
...
...
@@ -212,7 +236,7 @@ final class SettingsForm extends ConfigFormBase {
if
(
!
empty
(
$values
[
'key'
]))
{
$key
=
!
empty
(
$keys
[
$values
[
'key'
]])
?
$keys
[
$values
[
'key'
]]
:
[];
$aliases
=
$key
[
'aliases'
]
??
[];
// Prepend alias with / if it does not have one
// Prepend alias with / if it does not have one
.
if
(
strpos
(
$values
[
'alias'
],
'/'
)
!==
0
)
{
$values
[
'alias'
]
=
'/'
.
$values
[
'alias'
];
}
...
...
@@ -223,6 +247,8 @@ final class SettingsForm extends ConfigFormBase {
$nodes
[
$values
[
'key'
]]
=
$values
[
'entity_id'
];
$key
[
'bundle'
]
=
$values
[
'bundle'
];
$this
->
config
(
'frontend_routing.settings'
)
->
set
(
'keys'
,
$keys
)
->
save
();
...
...
@@ -231,7 +257,10 @@ final class SettingsForm extends ConfigFormBase {
$table
=
$form_state
->
getValue
(
'table'
);
if
(
is_array
(
$table
))
{
foreach
(
$table
as
$key
=>
$value
)
{
$nodes
[
$key
]
=
$value
[
'entity_id'
];
$keys
[
$key
][
'bundle'
]
=
$value
[
'bundle'
];
if
(
!
empty
(
$value
[
'entity_id'
]))
{
$nodes
[
$key
]
=
$value
[
'entity_id'
];
}
}
$this
->
config
(
'frontend_routing.settings'
)
->
set
(
'keys'
,
$keys
)
->
save
();
...
...
@@ -280,4 +309,18 @@ final class SettingsForm extends ConfigFormBase {
}
}
/**
* Get Node bundles.
*
* @return array
*/
private
function
getNodeTypes
()
{
$list
=
[];
$bundles
=
$this
->
entityTypeBundleInfo
->
getBundleInfo
(
'node'
);
foreach
(
$bundles
as
$key
=>
$bundle
)
{
$list
[
$key
]
=
$bundle
[
'label'
];
}
return
$list
;
}
}
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