You are here

public static function MenuLinksController::importMenuLinksSafe in Structure Sync 8

Same name and namespace in other branches
  1. 2.x src/Controller/MenuLinksController.php \Drupal\structure_sync\Controller\MenuLinksController::importMenuLinksSafe()

Function to import menu links safe (only adding what isn't already there).

1 call to MenuLinksController::importMenuLinksSafe()
MenuLinksController::importMenuLinks in src/Controller/MenuLinksController.php
Function to import menu links.

File

src/Controller/MenuLinksController.php, line 367

Class

MenuLinksController
Controller for syncing menu links.

Namespace

Drupal\structure_sync\Controller

Code

public static function importMenuLinksSafe($menus, &$context) {
  $menusFiltered = $menus;
  $entities = StructureSyncHelper::getEntityManager()
    ->getStorage('menu_link_content')
    ->loadMultiple();
  foreach ($entities as $entity) {
    for ($i = 0; $i < count($menus); $i++) {
      if ($entity
        ->uuid() === $menus[$i]['uuid']) {
        unset($menusFiltered[$i]);
      }
    }
  }
  foreach ($menusFiltered as $menuLink) {
    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();
    if (array_key_exists('drush', $context) && $context['drush'] === TRUE) {
      drush_log('Imported "' . $menuLink['title'] . '" into "' . $menuLink['menu_name'] . '" menu', 'ok');
    }
    StructureSyncHelper::logMessage('Imported "' . $menuLink['title'] . '" into "' . $menuLink['menu_name'] . '" menu');
  }
}