Skip to content
Snippets Groups Projects
Commit 22264abb authored by Louis Cuny's avatar Louis Cuny
Browse files

Issue #3354162 by tostinni, andrew.farquharson, keshav.k, louis-cuny: Export...

Issue #3354162 by tostinni, andrew.farquharson, keshav.k, louis-cuny: Export menu doesn't sort item according to their position in menu tree
parent 812c2ddb
No related branches found
No related tags found
1 merge request!24Issue #3354162: Export menu doesn't sort item according to their position in menu tree
......@@ -5,8 +5,11 @@
use Drupal\Core\Controller\ControllerBase;
use Drupal\structure_sync\StructureSyncHelper;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drush\Drush;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Controller for syncing menu links.
......@@ -15,12 +18,31 @@ class MenuLinksController extends ControllerBase {
private $config;
/**
* The menu tree service.
*
* @var \Drupal\Core\Menu\MenuLinkTreeInterface
*/
protected $menuTree;
/**
* Constructor for menu links controller.
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuTree
* The menu tree service.
*/
public function __construct() {
public function __construct(MenuLinkTreeInterface $menuTree) {
$this->config = $this->getEditableConfig();
$this->entityTypeManager();
$this->menuTree = $menuTree;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('menu.link_tree'),
);
}
/**
......@@ -39,19 +61,22 @@ public function exportMenuLinks(array $form = NULL, FormStateInterface $form_sta
StructureSyncHelper::logMessage('Menu links export started');
if (is_object($form_state) && $form_state->hasValue('export_menu_list')) {
$menu_list = $form_state->getValue('export_menu_list');
$menu_list = array_filter($menu_list, 'is_string');
$menuList = $form_state->getValue('export_menu_list');
$menuList = array_filter($menuList, 'is_string');
}
$this->config->clear('menus')->save();
if (isset($menu_list)) {
if (isset($menuList)) {
$menuLinks = [];
foreach ($menu_list as $menu_name) {
$menuLinks = array_merge($this->entityTypeManager
foreach ($menuList as $menuName) {
// Retrieve all menu links.
$unsortedMenuLinks = $this->entityTypeManager
->getStorage('menu_link_content')
->loadByProperties(['menu_name' => $menu_name]), $menuLinks);
->loadByProperties(['menu_name' => $menuName]);
// Sort them by their position in the menu tree.
$menuLinks = array_merge($this->sortMenuLinks($unsortedMenuLinks, $menuName), $menuLinks);
}
}
else {
......@@ -84,6 +109,49 @@ public function exportMenuLinks(array $form = NULL, FormStateInterface $form_sta
StructureSyncHelper::logMessage('Menu links exported');
}
/**
* Function to sort menu links by weight.
*
* When retrieving menu links using EntityTypeManager, they will be in order
* of creation (sorted by id). However if menu links have been rearranged,
* you can have a smaller id as a child of a greater id.
* Sorting the menu links array will ensure that childrens are created
* after their father.
*/
public function sortMenuLinks(array $unsortedMenuLinks, $menuName) {
// Retrieve the full menu tree of $menuName.
$tree = $this->menuTree->load($menuName, new MenuTreeParameters());
// Flatten it to have an array to use as sorting reference.
$flatMenuTree = $this->menuTreeFlatten($tree);
$menuLinksSorted = [];
// Use $flatMenuTree as the order reference to sort menu links.
foreach ($flatMenuTree as $uuid) {
foreach ($unsortedMenuLinks as $menuLink) {
if ($menuLink->uuid() === $uuid) {
$menuLinksSorted[] = $menuLink;
}
}
}
return $menuLinksSorted;
}
/**
* Helper function to recursively get all UUID of menu links from a menu.
*/
public function menuTreeFlatten(array $tree) {
$flatTree = [];
foreach ($tree as $menuLinkContentId => $menuLinkTree) {
$flatTree[] = str_replace('menu_link_content:', '', $menuLinkContentId);
if ($menuLinkTree->hasChildren) {
$flatTree = array_merge($flatTree, $this->menuTreeFlatten($menuLinkTree->subtree));
}
}
return $flatTree;
}
/**
* Function to import menu links.
*
......
......@@ -32,7 +32,7 @@ public static function exportCustomBlocks(array $form = NULL, FormStateInterface
* Function to export menu links.
*/
public static function exportMenuLinks(array $form = NULL, FormStateInterface $form_state = NULL) {
$menuLinksController = new MenuLinksController();
$menuLinksController = MenuLinksController::create(\Drupal::getContainer());
$menuLinksController->exportMenuLinks($form, $form_state);
}
......@@ -68,7 +68,7 @@ public static function importCustomBlocks(array $form, FormStateInterface $form_
* 'safe' or 'force' to apply that import style.
*/
public static function importMenuLinks(array $form, FormStateInterface $form_state = NULL) {
$menuLinksController = new MenuLinksController();
$menuLinksController = MenuLinksController::create(\Drupal::getContainer());
$menuLinksController->importMenuLinks($form, $form_state);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment