You are here

public function MenuLinksController::importMenuLinks in Structure Sync 8

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

Function to import menu links.

When this function is used without the designated form, you should assign an array with a key value pair for form with key 'style' and value 'full', 'safe' or 'force' to apply that import style.

File

src/Controller/MenuLinksController.php, line 96

Class

MenuLinksController
Controller for syncing menu links.

Namespace

Drupal\structure_sync\Controller

Code

public function importMenuLinks(array $form, FormStateInterface $form_state = NULL) {
  StructureSyncHelper::logMessage('Menu links import started');

  // Check if the there is a selection made in a form for what menus need to
  // be imported.
  if (is_object($form_state) && $form_state
    ->hasValue('import_menu_list')) {
    $menusSelected = $form_state
      ->getValue('import_menu_list');
    $menusSelected = array_filter($menusSelected, 'is_string');
  }
  if (array_key_exists('style', $form)) {
    $style = $form['style'];
  }
  else {
    StructureSyncHelper::logMessage('No style defined on menu links import', 'error');
    return;
  }
  StructureSyncHelper::logMessage('Using "' . $style . '" style for menu links import');

  // Get menu links from config.
  $menusConfig = $this->config
    ->get('menus');
  $menus = [];
  if (isset($menusSelected)) {
    foreach ($menusConfig as $menu) {
      if (in_array($menu['menu_name'], array_keys($menusSelected))) {
        $menus[] = $menu;
      }
    }
  }
  else {
    $menus = $menusConfig;
  }
  if (array_key_exists('drush', $form) && $form['drush'] === TRUE) {
    $context = [];
    $context['drush'] = TRUE;
    switch ($style) {
      case 'full':
        self::deleteDeletedMenuLinks($menus, $context);
        self::importMenuLinksFull($menus, $context);
        self::menuLinksImportFinishedCallback(NULL, NULL, NULL);
        break;
      case 'safe':
        self::importMenuLinksSafe($menus, $context);
        self::menuLinksImportFinishedCallback(NULL, NULL, NULL);
        break;
      case 'force':
        self::deleteMenuLinks($context);
        self::importMenuLinksForce($menus, $context);
        self::menuLinksImportFinishedCallback(NULL, NULL, NULL);
        break;
    }
    return;
  }

  // Import the menu links with the chosen style of importing.
  switch ($style) {
    case 'full':
      $batch = [
        'title' => $this
          ->t('Importing menu links...'),
        'operations' => [
          [
            '\\Drupal\\structure_sync\\Controller\\MenuLinksController::deleteDeletedMenuLinks',
            [
              $menus,
            ],
          ],
          [
            '\\Drupal\\structure_sync\\Controller\\MenuLinksController::importMenuLinksFull',
            [
              $menus,
            ],
          ],
        ],
        'finished' => '\\Drupal\\structure_sync\\Controller\\MenuLinksController::menuLinksImportFinishedCallback',
      ];
      batch_set($batch);
      break;
    case 'safe':
      $batch = [
        'title' => $this
          ->t('Importing menu links...'),
        'operations' => [
          [
            '\\Drupal\\structure_sync\\Controller\\MenuLinksController::importMenuLinksSafe',
            [
              $menus,
            ],
          ],
        ],
        'finished' => '\\Drupal\\structure_sync\\Controller\\MenuLinksController::menuLinksImportFinishedCallback',
      ];
      batch_set($batch);
      break;
    case 'force':
      $batch = [
        'title' => $this
          ->t('Importing menu links...'),
        'operations' => [
          [
            '\\Drupal\\structure_sync\\Controller\\MenuLinksController::deleteMenuLinks',
            [],
          ],
          [
            '\\Drupal\\structure_sync\\Controller\\MenuLinksController::importMenuLinksForce',
            [
              $menus,
            ],
          ],
        ],
        'finished' => '\\Drupal\\structure_sync\\Controller\\MenuLinksController::menuLinksImportFinishedCallback',
      ];
      batch_set($batch);
      break;
    default:
      StructureSyncHelper::logMessage('Style not recognized', 'error');
      break;
  }
}