Skip to content
Snippets Groups Projects

Issue #2980893: re-roll patch #2 that adds hooks for import, export and update.

Open Issue #2980893: re-roll patch #2 that adds hooks for import, export and update.
All threads resolved!
All threads resolved!
Files
7
@@ -3,6 +3,7 @@
namespace Drupal\structure_sync\Controller;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Menu\MenuLinkTreeInterface;
use Drupal\Core\Menu\MenuTreeParameters;
@@ -30,11 +31,20 @@ class MenuLinksController extends ControllerBase {
*/
protected $menuTree;
/**
* The module handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructor for menu links controller.
*
* @param \Drupal\Core\Menu\MenuLinkTreeInterface $menuTree
* The menu tree service.
* @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
* The module handler service.
*/
public function __construct(MenuLinkTreeInterface $menuTree) {
$this->config = $this->getEditableConfig();
@@ -46,9 +56,11 @@ public function __construct(MenuLinkTreeInterface $menuTree) {
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new self(
$instance = new self(
$container->get('menu.link_tree'),
);
$instance->moduleHandler = $container->get('module_handler');
return $instance;
}
/**
@@ -92,7 +104,7 @@ public function exportMenuLinks(array $form = NULL, FormStateInterface $form_sta
$customMenuLinks = [];
foreach ($menuLinks as $menuLink) {
$customMenuLinks[] = [
$customMenuLink = [
'menu_name' => $menuLink->menu_name->value,
'title' => $menuLink->title->value,
'parent' => $menuLink->parent->value,
@@ -107,6 +119,12 @@ public function exportMenuLinks(array $form = NULL, FormStateInterface $form_sta
'options' => $menuLink->link->options,
];
// Provide a way for other modules to alter the data that is being
// exported for menu items, so additional fields can be added.
$this->moduleHandler->alter('structure_sync_menu_link_export', $customMenuLink, $menuLink);
$customMenuLinks[] = $customMenuLink;
StructureSyncHelper::logMessage('Exported "' . $menuLink->title->value . '" of menu "' . $menuLink->menu_name->value . '"');
}
@@ -350,6 +368,9 @@ public static function importMenuLinksFull($menus, &$context) {
}
}
// Get the module handler service.
$moduleHandler = StructureSyncHelper::getModuleHandler();
$idsDone = [];
$idsLeft = [];
$firstRun = TRUE;
@@ -374,7 +395,7 @@ public static function importMenuLinksFull($menus, &$context) {
|| in_array($currentParent, $idsDone))
) {
if (count($ids) <= 0) {
MenuLinkContent::create([
$customMenuLink = [
'title' => $menuLink['title'],
'link' => [
'uri' => $menuLink['uri'],
@@ -389,7 +410,11 @@ public static function importMenuLinksFull($menus, &$context) {
'weight' => $menuLink['weight'],
'langcode' => $menuLink['langcode'],
'uuid' => $menuLink['uuid'],
])->save();
];
// Provide a way for other modules to alter the data being imported.
$moduleHandler->alter('structure_sync_menu_link_import', $customMenuLink, $menuLink);
MenuLinkContent::create($customMenuLink)->save();
}
else {
foreach ($entities as $entity) {
@@ -407,8 +432,13 @@ public static function importMenuLinksFull($menus, &$context) {
->set('enabled', in_array($menuLink['enabled'], ['1', TRUE], TRUE))
->set('parent', $menuLink['parent'])
->set('description', $menuLink['description'])
->set('weight', $menuLink['weight'])
->save();
->set('weight', $menuLink['weight']);
// Provide a way for other modules to alter the data that is
// being imported for menu items, when an item is updated.
$moduleHandler->alter('structure_sync_menu_link_update', $customMenuLink, $menuLink);
$customMenuLink->save();
}
break;
@@ -458,8 +488,11 @@ public static function importMenuLinksSafe($menus, &$context) {
}
}
// Get the module handler service.
$moduleHandler = StructureSyncHelper::getModuleHandler();
foreach ($menusFiltered as $menuLink) {
MenuLinkContent::create([
$customMenuLink = [
'title' => $menuLink['title'],
'link' => [
'uri' => $menuLink['uri'],
@@ -474,7 +507,12 @@ public static function importMenuLinksSafe($menus, &$context) {
'weight' => $menuLink['weight'],
'langcode' => $menuLink['langcode'],
'uuid' => $menuLink['uuid'],
])->save();
];
// Provide a way for other modules to alter the data being imported.
$moduleHandler->alter('structure_sync_menu_link_import', $customMenuLink, $menuLink);
MenuLinkContent::create($customMenuLink)->save();
StructureSyncHelper::logMessage('Imported "' . $menuLink['title'] . '" into "' . $menuLink['menu_name'] . '" menu');
}
@@ -502,7 +540,7 @@ public static function deleteMenuLinks(&$context) {
*/
public static function importMenuLinksForce($menus, &$context) {
foreach ($menus as $menuLink) {
MenuLinkContent::create([
$customMenuLink = [
'title' => $menuLink['title'],
'link' => [
'uri' => $menuLink['uri'],
@@ -517,7 +555,12 @@ public static function importMenuLinksForce($menus, &$context) {
'weight' => $menuLink['weight'],
'langcode' => $menuLink['langcode'],
'uuid' => $menuLink['uuid'],
])->save();
];
// Provide a way for other modules to alter the data being imported.
StructureSyncHelper::getModuleHandler()->alter('structure_sync_menu_link_import', $customMenuLink, $menuLink);
MenuLinkContent::create($customMenuLink)->save();
StructureSyncHelper::logMessage('Imported "' . $menuLink['title'] . '" into "' . $menuLink['menu_name'] . '" menu');
}
Loading