public static function MenuLinksController::importMenuLinksFull in Structure Sync 8
Same name and namespace in other branches
- 2.x src/Controller/MenuLinksController.php \Drupal\structure_sync\Controller\MenuLinksController::importMenuLinksFull()
Function to fully import the menu links.
Basically a safe import with update actions for already existing menu links.
1 call to MenuLinksController::importMenuLinksFull()
- MenuLinksController::importMenuLinks in src/
Controller/ MenuLinksController.php - Function to import menu links.
File
- src/
Controller/ MenuLinksController.php, line 246
Class
- MenuLinksController
- Controller for syncing menu links.
Namespace
Drupal\structure_sync\ControllerCode
public static function importMenuLinksFull($menus, &$context) {
$uuidsInConfig = [];
foreach ($menus as $menuLink) {
$uuidsInConfig[] = $menuLink['uuid'];
}
$entities = [];
if (!empty($uuidsInConfig)) {
$query = StructureSyncHelper::getEntityQuery('menu_link_content');
$query
->condition('uuid', $uuidsInConfig, 'IN');
$ids = $query
->execute();
$controller = StructureSyncHelper::getEntityManager()
->getStorage('menu_link_content');
$entities = $controller
->loadMultiple($ids);
}
$parents = array_column($menus, 'parent');
foreach ($parents as &$parent) {
if (!is_null($parent)) {
if (($pos = strpos($parent, ":")) !== FALSE) {
$parent = substr($parent, $pos + 1);
}
else {
$parent = NULL;
}
}
}
$idsDone = [];
$idsLeft = [];
$firstRun = TRUE;
$context['sandbox']['max'] = count($menus);
$context['sandbox']['progress'] = 0;
while ($firstRun || count($idsLeft) > 0) {
foreach ($menus as $menuLink) {
$query = StructureSyncHelper::getEntityQuery('menu_link_content');
$query
->condition('uuid', $menuLink['uuid']);
$ids = $query
->execute();
$currentParent = $menuLink['parent'];
if (!is_null($currentParent)) {
if (($pos = strpos($currentParent, ":")) !== FALSE) {
$currentParent = substr($currentParent, $pos + 1);
}
}
if (!in_array($menuLink['uuid'], $idsDone) && ($menuLink['parent'] === NULL || !in_array($menuLink['parent'], $parents) || in_array($currentParent, $idsDone))) {
if (count($ids) <= 0) {
MenuLinkContent::create([
'title' => $menuLink['title'],
'link' => [
'uri' => $menuLink['uri'],
'title' => $menuLink['link_title'],
],
'menu_name' => $menuLink['menu_name'],
'expanded' => $menuLink['expanded'] === '1' ? TRUE : FALSE,
'enabled' => $menuLink['enabled'] === '1' ? TRUE : FALSE,
'parent' => $menuLink['parent'],
'description' => $menuLink['description'],
'weight' => $menuLink['weight'],
'langcode' => $menuLink['langcode'],
'uuid' => $menuLink['uuid'],
])
->save();
}
else {
foreach ($entities as $entity) {
if ($menuLink['uuid'] === $entity
->uuid()) {
$customMenuLink = MenuLinkContent::load($entity
->id());
if (!empty($customMenuLink)) {
$customMenuLink
->set('title', $menuLink['title'])
->set('link', [
'uri' => $menuLink['uri'],
'title' => $menuLink['link_title'],
])
->set('expanded', $menuLink['expanded'] === '1' ? TRUE : FALSE)
->set('enabled', $menuLink['enabled'] === '1' ? TRUE : FALSE)
->set('parent', $menuLink['parent'])
->set('description', $menuLink['description'])
->set('weight', $menuLink['weight'])
->save();
}
break;
}
}
}
$idsDone[] = $menuLink['uuid'];
if (in_array($menuLink['uuid'], $idsLeft)) {
unset($idsLeft[$menuLink['uuid']]);
}
if (array_key_exists('drush', $context) && $context['drush'] === TRUE) {
drush_log('Imported "' . $menuLink['title'] . '" into ' . $menuLink['menu_name'], 'ok');
}
StructureSyncHelper::logMessage('Imported "' . $menuLink['title'] . '" into ' . $menuLink['menu_name']);
$context['sandbox']['progress']++;
if ($context['sandbox']['progress'] != $context['sandbox']['max']) {
$context['finished'] = $context['sandbox']['progress'] / $context['sandbox']['max'];
}
}
else {
$idsLeft[$menuLink['uuid']] = $menuLink['uuid'];
}
}
$firstRun = FALSE;
}
$context['finished'] = 1;
}