Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wse
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
wse
Merge requests
!91
3494762: Prevent updating links tracked in a WS
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
3494762: Prevent updating links tracked in a WS
issue/wse-3494762:3494762-prevent-updating-links
into
2.0.x
Overview
1
Commits
5
Pipelines
7
Changes
4
1 unresolved thread
Hide all comments
Merged
Alec Smrekar
requested to merge
issue/wse-3494762:3494762-prevent-updating-links
into
2.0.x
5 months ago
Overview
1
Commits
5
Pipelines
7
Changes
4
1 unresolved thread
Hide all comments
Expand
Closes
#3494762
0
0
Merge request reports
Compare
2.0.x
version 6
d0228c28
5 months ago
version 5
874f3981
5 months ago
version 4
2c60af5b
5 months ago
version 3
79189800
5 months ago
version 2
db51d086
5 months ago
version 1
4e3422ad
5 months ago
2.0.x (base)
and
latest version
latest version
d0228c28
5 commits,
5 months ago
version 6
d0228c28
5 commits,
5 months ago
version 5
874f3981
4 commits,
5 months ago
version 4
2c60af5b
3 commits,
5 months ago
version 3
79189800
2 commits,
5 months ago
version 2
db51d086
1 commit,
5 months ago
version 1
4e3422ad
1 commit,
5 months ago
4 files
+
140
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
4
Search (e.g. *.vue) (Ctrl+P)
modules/wse_menu/src/Form/TrackedLinkValidationTrait.php
0 → 100644
+
117
−
0
Options
<?php
declare
(
strict_types
=
1
);
namespace
Drupal\wse_menu\Form
;
use
Drupal\Component\Plugin\Exception\PluginException
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Form\FormStateInterface
;
use
Drupal\Core\Menu\MenuLinkManagerInterface
;
use
Drupal\menu_link_content
\MenuLinkContentInterface
;
use
Drupal\menu_link_content
\Plugin\Menu\MenuLinkContent
;
use
Drupal\workspaces\WorkspaceAssociationInterface
;
use
Drupal\workspaces\WorkspaceInterface
;
use
Drupal\workspaces\WorkspaceManagerInterface
;
/**
* Trait for validating links.
*/
trait
TrackedLinkValidationTrait
{
/**
* Validates that no form link is tracked in a non-active workspace.
*
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
protected
function
validateLinks
(
FormStateInterface
$form_state
):
void
{
$links
=
$form_state
->
getValue
(
'links'
)
??
[];
$entity_ids
=
[];
foreach
(
$links
as
$link
)
{
$plugin_id
=
$link
[
'id'
]
??
''
;
if
(
!
str_starts_with
(
$plugin_id
,
'menu_link_content:'
))
{
continue
;
}
try
{
$instance
=
$this
->
getLinkPluginManager
()
->
createInstance
(
$plugin_id
);
}
catch
(
PluginException
)
{
continue
;
}
if
(
!
(
$instance
instanceof
MenuLinkContent
))
{
continue
;
}
$entity_id
=
$instance
->
getPluginDefinition
()[
'metadata'
][
'entity_id'
]
??
NULL
;
if
(
$entity_id
)
{
$entity_ids
[]
=
$entity_id
;
}
}
$link_entities
=
$this
->
getEntityTypeManager
()
->
getStorage
(
'menu_link_content'
)
->
loadMultiple
(
$entity_ids
);
$active_workspace
=
$this
->
getWorkspaceManager
()
->
getActiveWorkspace
();
$association
=
$this
->
getWorkspaceAssociation
();
foreach
(
$link_entities
as
$link_entity
)
{
assert
(
$link_entity
instanceof
MenuLinkContentInterface
);
$tracked
=
$association
->
getEntityTrackingWorkspaceIds
(
$link_entity
);
// Check if this link is tracked in a not currently active workspace.
$diff
=
$active_workspace
?
array_diff
(
$tracked
,
[
$active_workspace
->
id
()])
:
$tracked
;
if
(
$diff
)
{
$tracking_workspaces
=
$this
->
getEntityTypeManager
()
->
getStorage
(
'workspace'
)
->
loadMultiple
(
$diff
);
$labels
=
array_map
(
function
(
WorkspaceInterface
$workspace
)
{
return
$workspace
->
label
();
},
$tracking_workspaces
);
$form_state
->
setErrorByName
(
'links]['
.
$link_entity
->
getPluginId
()
.
'][id'
,
$this
->
t
(
"Could not update menu link <em>@title</em> because it's tracked in other workspaces (@workspace_labels)."
,
[
'@title'
=>
$link_entity
->
label
(),
'@workspace_labels'
=>
implode
(
', '
,
$labels
),
]));
}
}
}
/**
* The entity type manager.
*
* @return \Drupal\Core\Entity\EntityTypeManagerInterface
* The service.
*/
private
function
getEntityTypeManager
():
EntityTypeManagerInterface
{
return
\Drupal
::
entityTypeManager
();
}
/**
* The menu link plugin manager.
*
* @return \Drupal\Core\Menu\MenuLinkManagerInterface
* The service.
*/
private
function
getLinkPluginManager
():
MenuLinkManagerInterface
{
return
\Drupal
::
service
(
'plugin.manager.menu.link'
);
}
/**
* The workspace manager service.
*
* @return \Drupal\workspaces\WorkspaceManagerInterface
* The service.
*/
private
function
getWorkspaceManager
():
WorkspaceManagerInterface
{
return
\Drupal
::
service
(
'workspaces.manager'
);
}
/**
* The workspace association service.
*
* @return \Drupal\workspaces\WorkspaceAssociationInterface
* The service.
*/
private
function
getWorkspaceAssociation
():
WorkspaceAssociationInterface
{
return
\Drupal
::
service
(
'workspaces.association'
);
}
}
Loading