diff --git a/src/Controller/HmMenuController.php b/src/Controller/HmMenuController.php index e44a4d80e8da3353d28327a44f167600cf9706f2..f7ad4c237f00d66adebe2cac87de611acd941c73 100644 --- a/src/Controller/HmMenuController.php +++ b/src/Controller/HmMenuController.php @@ -5,6 +5,7 @@ namespace Drupal\hierarchy_manager\Controller; use Drupal\Core\Url; use Drupal\Core\Access\CsrfTokenGenerator; use Drupal\Core\Controller\ControllerBase; +use Drupal\Core\Entity\EntityRepository; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Menu\MenuLinkTreeInterface; use Drupal\Core\Menu\MenuLinkManagerInterface; @@ -58,16 +59,24 @@ class HmMenuController extends ControllerBase { */ protected $menuLinkManager; + /** + * The entity repository object + * + * @var \Drupal\Core\Entity\EntityRepository + */ + protected $entityRepository; + /** * {@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->entityTypeManager = $entity_type_manager; $this->storageController = $entity_type_manager->getStorage('menu_link_content'); $this->hmPluginTypeManager = $plugin_type_manager; $this->menuTree = $menu_tree; $this->menuLinkManager = $menu_link_manager; + $this->entityRepository = $entity_repository; } /** @@ -79,7 +88,8 @@ class HmMenuController extends ControllerBase { $container->get('entity_type.manager'), $container->get('hm.plugin_type_manager'), $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 { $target_position = $request->get('target'); $parent = $request->get('parent'); $updated_links = $request->get('keys'); - //$after = $request->get('after'); - $before = $request->get('before'); $all_siblings = []; - $insert_after = TRUE; + $links_uuid = []; if (is_array($updated_links) && !empty($updated_links)) { if (empty($parent)) { @@ -175,6 +183,13 @@ class HmMenuController extends ControllerBase { $parent_links = $children = $this->loadMenuLinkObjs($mid, $parent, 1); } else { + $parent = $this->entityTypeManager->getStorage('menu_link_content')->load($parent); + if (!empty($parent)) { + $parent = $parent->getPluginId(); + } + else { + $parent = ''; + } // All children menu links (depth = 1). $parent_links = $this->loadMenuLinkObjs($mid, $parent, 1); } @@ -195,28 +210,32 @@ class HmMenuController extends ControllerBase { // The parent menu has children. $target_position = intval($target_position); - $position = 0; foreach ($children as $child) { $link = $child->link; - $link_id = $link->getPLuginId(); - // Figure out if the new links are inserted - // after the target position. - if ($position++ == $target_position && $link_id !== $before) { - $insert_after = FALSE; - } + $uuid = $link->getDerivativeId(); + $link_id = $this->entityRepository->loadEntityByUuid('menu_link_content', $uuid)->id(); + $links_uuid[$link_id] = $link->getPluginId(); - $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. 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']); @@ -261,7 +280,8 @@ class HmMenuController extends ControllerBase { $element['title'], $element['parent'], $element['url'], - $element['status'] + $element['status'], + $element['weight'] ); } @@ -320,7 +340,9 @@ class HmMenuController extends ControllerBase { $link = $element->link; if ($link) { // 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]['status'] = $link->isEnabled(); if (!$link->isEnabled()) { @@ -338,7 +360,16 @@ class HmMenuController extends ControllerBase { $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. // Allow for a custom edit link per plugin. $edit_route = $link->getEditRoute(); @@ -356,10 +387,6 @@ class HmMenuController extends ControllerBase { } } - /* $tree_access_cacheability - ->merge(CacheableMetadata::createFromRenderArray($this->overviewTree)) - ->applyTo($form); */ - return $this->overviewTree; } }