Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hierarchy_manager-3343978
Manage
Activity
Members
Labels
Plan
Custom issue tracker
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
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
Issue forks
hierarchy_manager-3343978
Commits
0ffe506d
Commit
0ffe506d
authored
3 years ago
by
Mingsong Hu
Browse files
Options
Downloads
Patches
Plain Diff
Fix menu plugin bug
parent
04308533
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/Controller/HmMenuController.php
+52
-25
52 additions, 25 deletions
src/Controller/HmMenuController.php
with
52 additions
and
25 deletions
src/Controller/HmMenuController.php
+
52
−
25
View file @
0ffe506d
...
@@ -5,6 +5,7 @@ namespace Drupal\hierarchy_manager\Controller;
...
@@ -5,6 +5,7 @@ namespace Drupal\hierarchy_manager\Controller;
use
Drupal\Core\Url
;
use
Drupal\Core\Url
;
use
Drupal\Core\Access\CsrfTokenGenerator
;
use
Drupal\Core\Access\CsrfTokenGenerator
;
use
Drupal\Core\Controller\ControllerBase
;
use
Drupal\Core\Controller\ControllerBase
;
use
Drupal\Core\Entity\EntityRepository
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Entity\EntityTypeManagerInterface
;
use
Drupal\Core\Menu\MenuLinkTreeInterface
;
use
Drupal\Core\Menu\MenuLinkTreeInterface
;
use
Drupal\Core\Menu\MenuLinkManagerInterface
;
use
Drupal\Core\Menu\MenuLinkManagerInterface
;
...
@@ -58,16 +59,24 @@ class HmMenuController extends ControllerBase {
...
@@ -58,16 +59,24 @@ class HmMenuController extends ControllerBase {
*/
*/
protected
$menuLinkManager
;
protected
$menuLinkManager
;
/**
* The entity repository object
*
* @var \Drupal\Core\Entity\EntityRepository
*/
protected
$entityRepository
;
/**
/**
* {@inheritdoc}
* {@inheritdoc}
*/
*/
public
function
__construct
(
CsrfTokenGenerator
$csrfToken
,
EntityTypeManagerInterface
$entity_type_manager
,
$plugin_type_manager
,
MenuLinkTreeInterface
$menu_tree
,
MenuLinkManagerInterface
$menu_link_manager
)
{
public
function
__construct
(
CsrfTokenGenerator
$csrfToken
,
EntityTypeManagerInterface
$entity_type_manager
,
$plugin_type_manager
,
MenuLinkTreeInterface
$menu_tree
,
MenuLinkManagerInterface
$menu_link_manager
,
EntityRepository
$entity_repository
)
{
$this
->
csrfToken
=
$csrfToken
;
$this
->
csrfToken
=
$csrfToken
;
$this
->
entityTypeManager
=
$entity_type_manager
;
$this
->
entityTypeManager
=
$entity_type_manager
;
$this
->
storageController
=
$entity_type_manager
->
getStorage
(
'menu_link_content'
);
$this
->
storageController
=
$entity_type_manager
->
getStorage
(
'menu_link_content'
);
$this
->
hmPluginTypeManager
=
$plugin_type_manager
;
$this
->
hmPluginTypeManager
=
$plugin_type_manager
;
$this
->
menuTree
=
$menu_tree
;
$this
->
menuTree
=
$menu_tree
;
$this
->
menuLinkManager
=
$menu_link_manager
;
$this
->
menuLinkManager
=
$menu_link_manager
;
$this
->
entityRepository
=
$entity_repository
;
}
}
/**
/**
...
@@ -79,7 +88,8 @@ class HmMenuController extends ControllerBase {
...
@@ -79,7 +88,8 @@ class HmMenuController extends ControllerBase {
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'entity_type.manager'
),
$container
->
get
(
'hm.plugin_type_manager'
),
$container
->
get
(
'hm.plugin_type_manager'
),
$container
->
get
(
'menu.link_tree'
),
$container
->
get
(
'menu.link_tree'
),
$container
->
get
(
'plugin.manager.menu.link'
)
$container
->
get
(
'plugin.manager.menu.link'
),
$container
->
get
(
'entity.repository'
)
);
);
}
}
...
@@ -163,10 +173,8 @@ class HmMenuController extends ControllerBase {
...
@@ -163,10 +173,8 @@ class HmMenuController extends ControllerBase {
$target_position
=
$request
->
get
(
'target'
);
$target_position
=
$request
->
get
(
'target'
);
$parent
=
$request
->
get
(
'parent'
);
$parent
=
$request
->
get
(
'parent'
);
$updated_links
=
$request
->
get
(
'keys'
);
$updated_links
=
$request
->
get
(
'keys'
);
//$after = $request->get('after');
$before
=
$request
->
get
(
'before'
);
$all_siblings
=
[];
$all_siblings
=
[];
$in
sert_after
=
TRUE
;
$
l
in
ks_uuid
=
[]
;
if
(
is_array
(
$updated_links
)
&&
!
empty
(
$updated_links
))
{
if
(
is_array
(
$updated_links
)
&&
!
empty
(
$updated_links
))
{
if
(
empty
(
$parent
))
{
if
(
empty
(
$parent
))
{
...
@@ -175,6 +183,13 @@ class HmMenuController extends ControllerBase {
...
@@ -175,6 +183,13 @@ class HmMenuController extends ControllerBase {
$parent_links
=
$children
=
$this
->
loadMenuLinkObjs
(
$mid
,
$parent
,
1
);
$parent_links
=
$children
=
$this
->
loadMenuLinkObjs
(
$mid
,
$parent
,
1
);
}
}
else
{
else
{
$parent
=
$this
->
entityTypeManager
->
getStorage
(
'menu_link_content'
)
->
load
(
$parent
);
if
(
!
empty
(
$parent
))
{
$parent
=
$parent
->
getPluginId
();
}
else
{
$parent
=
''
;
}
// All children menu links (depth = 1).
// All children menu links (depth = 1).
$parent_links
=
$this
->
loadMenuLinkObjs
(
$mid
,
$parent
,
1
);
$parent_links
=
$this
->
loadMenuLinkObjs
(
$mid
,
$parent
,
1
);
}
}
...
@@ -195,28 +210,32 @@ class HmMenuController extends ControllerBase {
...
@@ -195,28 +210,32 @@ class HmMenuController extends ControllerBase {
// The parent menu has children.
// The parent menu has children.
$target_position
=
intval
(
$target_position
);
$target_position
=
intval
(
$target_position
);
$position
=
0
;
foreach
(
$children
as
$child
)
{
foreach
(
$children
as
$child
)
{
$link
=
$child
->
link
;
$link
=
$child
->
link
;
$link_id
=
$link
->
getPLuginId
();
$uuid
=
$link
->
getDerivativeId
();
// Figure out if the new links are inserted
$link_id
=
$this
->
entityRepository
->
loadEntityByUuid
(
'menu_link_content'
,
$uuid
)
->
id
();
// after the target position.
$links_uuid
[
$link_id
]
=
$link
->
getPluginId
();
if
(
$position
++
==
$target_position
&&
$link_id
!==
$before
)
{
$insert_after
=
FALSE
;
}
$all_siblings
[
$link_id
]
=
(
int
)
$link
->
getWeight
();
$all_siblings
[
$link_id
]
=
$link
->
getWeight
();
}
}
}
}
else
{
// The parent link doesn't have children.
$new_hierarchy
=
$this
->
hmPluginTypeManager
->
updateHierarchy
(
$target_position
,
$all_siblings
,
$updated_links
);
foreach
(
$updated_links
as
$id
)
{
if
(
!
isset
(
$links_uuid
[
$id
]))
{
$entity
=
$this
->
entityTypeManager
->
getStorage
(
'menu_link_content'
)
->
load
(
$id
);
if
(
!
empty
(
$entity
))
{
$links_uuid
[
$id
]
=
$entity
->
getPluginId
();
}
else
{
// The updated menu link doesn't exist.
return
new
JsonResponse
([
'result'
=>
'fail'
]);
}
}
}
}
$new_hierarchy
=
$this
->
hmPluginTypeManager
->
updateHierarchy
(
$target_position
,
$all_siblings
,
$updated_links
,
$insert_after
);
// Update all links need to update.
// Update all links need to update.
foreach
(
$new_hierarchy
as
$link_id
=>
$link_weight
)
{
foreach
(
$new_hierarchy
as
$link_id
=>
$link_weight
)
{
$this
->
menuLinkManager
->
updateDefinition
(
$link_id
,
[
'weight'
=>
$link_weight
,
'parent'
=>
$parent
]);
$this
->
menuLinkManager
->
updateDefinition
(
$
links_uuid
[
$
link_id
]
,
[
'weight'
=>
$link_weight
,
'parent'
=>
$parent
]);
}
}
return
new
JsonResponse
([
'result'
=>
'success'
]);
return
new
JsonResponse
([
'result'
=>
'success'
]);
...
@@ -261,7 +280,8 @@ class HmMenuController extends ControllerBase {
...
@@ -261,7 +280,8 @@ class HmMenuController extends ControllerBase {
$element
[
'title'
],
$element
[
'title'
],
$element
[
'parent'
],
$element
[
'parent'
],
$element
[
'url'
],
$element
[
'url'
],
$element
[
'status'
]
$element
[
'status'
],
$element
[
'weight'
]
);
);
}
}
...
@@ -320,7 +340,9 @@ class HmMenuController extends ControllerBase {
...
@@ -320,7 +340,9 @@ class HmMenuController extends ControllerBase {
$link
=
$element
->
link
;
$link
=
$element
->
link
;
if
(
$link
)
{
if
(
$link
)
{
// The id consistes of plugin ID and link ID.
// The id consistes of plugin ID and link ID.
$id
=
$link
->
getPluginId
();
$uuid
=
$link
->
getDerivativeId
();
$menu_entity
=
$this
->
entityRepository
->
loadEntityByUuid
(
'menu_link_content'
,
$uuid
);
$id
=
$menu_entity
->
id
();
$this
->
overviewTree
[
$id
][
'id'
]
=
$id
;
$this
->
overviewTree
[
$id
][
'id'
]
=
$id
;
$this
->
overviewTree
[
$id
][
'status'
]
=
$link
->
isEnabled
();
$this
->
overviewTree
[
$id
][
'status'
]
=
$link
->
isEnabled
();
if
(
!
$link
->
isEnabled
())
{
if
(
!
$link
->
isEnabled
())
{
...
@@ -338,7 +360,16 @@ class HmMenuController extends ControllerBase {
...
@@ -338,7 +360,16 @@ class HmMenuController extends ControllerBase {
$this
->
overviewTree
[
$id
][
'title'
]
=
$link
->
getTitle
();
$this
->
overviewTree
[
$id
][
'title'
]
=
$link
->
getTitle
();
}
}
$this
->
overviewTree
[
$id
][
'parent'
]
=
$link
->
getParent
();
$parent_uuid
=
explode
(
':'
,
$link
->
getParent
());
if
(
isset
(
$parent_uuid
[
1
]))
{
$parent_id
=
$this
->
entityRepository
->
loadEntityByUuid
(
$parent_uuid
[
0
],
$parent_uuid
[
1
])
->
id
();
}
else
{
$parent_id
=
''
;
}
$this
->
overviewTree
[
$id
][
'parent'
]
=
$parent_id
;
$this
->
overviewTree
[
$id
][
'weight'
]
=
$link
->
getWeight
();
// Build the edit url.
// Build the edit url.
// Allow for a custom edit link per plugin.
// Allow for a custom edit link per plugin.
$edit_route
=
$link
->
getEditRoute
();
$edit_route
=
$link
->
getEditRoute
();
...
@@ -356,10 +387,6 @@ class HmMenuController extends ControllerBase {
...
@@ -356,10 +387,6 @@ class HmMenuController extends ControllerBase {
}
}
}
}
/* $tree_access_cacheability
->merge(CacheableMetadata::createFromRenderArray($this->overviewTree))
->applyTo($form); */
return
$this
->
overviewTree
;
return
$this
->
overviewTree
;
}
}
}
}
...
...
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