function multiversion_menu_links_discovered_alter in Multiversion 8
Same name and namespace in other branches
- 8.2 multiversion.module \multiversion_menu_links_discovered_alter()
Implements hook_menu_links_discovered_alter().
File
- ./
multiversion.module, line 552
Code
function multiversion_menu_links_discovered_alter(&$links) {
// Get all custom menu links and set links with the correct ID.
// The ID format now will be 'menu_link_content:ENTITY_UUID:ENTITY_ID' - we
// need to change it because we need new entry in the menu_tree table for the
// same link on different workspaces.
// The old ID format is 'menu_link_content:ENTITY_UUID'.
if (\Drupal::moduleHandler()
->moduleExists('menu_link_content')) {
$storage = \Drupal::entityTypeManager()
->getStorage('menu_link_content');
if (\Drupal::service('multiversion.manager')
->isEnabledEntityType($storage
->getEntityType())) {
$workspaces = Workspace::loadMultiple();
foreach ($workspaces as $workspace_id => $workspace) {
$storage
->useWorkspace($workspace_id);
$menu_link_content_entities = $storage
->loadMultiple();
$new_ids = [];
$links_to_purge = [];
/** @var \Drupal\menu_link_content\MenuLinkContentInterface $menu_link_content */
foreach ($menu_link_content_entities as $menu_link_content) {
// Unset links with old ID format.
$uuid = $menu_link_content
->uuid();
$old_id = "menu_link_content:{$uuid}";
$new_id = "{$old_id}:" . $menu_link_content
->id();
if (isset($links[$old_id])) {
$links_to_purge[] = $old_id;
unset($links[$old_id]);
}
$new_ids[$old_id] = $new_id;
if (!isset($links[$new_id])) {
$links[$new_id] = $menu_link_content
->getPluginDefinition();
}
// Set a new plugin class tha will handle new ID format.
$links[$new_id]['class'] = 'Drupal\\multiversion\\Plugin\\Menu\\MenuLinkContent';
}
if ($links_to_purge) {
\Drupal::service('menu.tree_storage')
->purgeMultiple($links_to_purge);
}
foreach ($links as $id => $link) {
if (!empty($link['parent']) && in_array($link['parent'], array_keys($new_ids))) {
$links[$id]['parent'] = $new_ids[$link['parent']];
}
}
$storage
->useWorkspace(NULL);
}
}
}
}