You are here

public function MenuLinksController::exportMenuLinks in Structure Sync 8

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

Function to export menu links.

File

src/Controller/MenuLinksController.php, line 37

Class

MenuLinksController
Controller for syncing menu links.

Namespace

Drupal\structure_sync\Controller

Code

public function exportMenuLinks(array $form = NULL, FormStateInterface $form_state = NULL) {
  StructureSyncHelper::logMessage('Menu links export started');
  if (is_object($form_state) && $form_state
    ->hasValue('export_menu_list')) {
    $menu_list = $form_state
      ->getValue('export_menu_list');
    $menu_list = array_filter($menu_list, 'is_string');
  }
  $this->config
    ->clear('menus')
    ->save();
  if (isset($menu_list)) {
    $menuLinks = [];
    foreach ($menu_list as $menu_name) {
      $menuLinks = array_merge($this->entityTypeManager
        ->getStorage('menu_link_content')
        ->loadByProperties([
        'menu_name' => $menu_name,
      ]), $menuLinks);
    }
  }
  else {
    $menuLinks = $this
      ->entityTypeManager()
      ->getStorage('menu_link_content')
      ->loadMultiple();
  }
  $customMenuLinks = [];
  foreach ($menuLinks as $menuLink) {
    $customMenuLinks[] = [
      'menu_name' => $menuLink->menu_name->value,
      'title' => $menuLink->title->value,
      'parent' => $menuLink->parent->value,
      'uri' => $menuLink->link->uri,
      'link_title' => $menuLink->link->title,
      'description' => $menuLink->description->value,
      'enabled' => $menuLink->enabled->value,
      'expanded' => $menuLink->expanded->value,
      'weight' => $menuLink->weight->value,
      'langcode' => $menuLink->langcode->value,
      'uuid' => $menuLink
        ->uuid(),
    ];
    if (array_key_exists('drush', $form) && $form['drush'] === TRUE) {
      drush_log('Exported "' . $menuLink->title
        ->getValue()[0]['value'] . '" of menu "' . $menuLink->menu_name
        ->getValue()[0]['value'] . '"', 'ok');
    }
    StructureSyncHelper::logMessage('Exported "' . $menuLink->title->value . '" of menu "' . $menuLink->menu_name->value . '"');
  }
  $this->config
    ->set('menus', $customMenuLinks)
    ->save();
  drupal_set_message($this
    ->t('The menu links have been successfully exported.'));
  StructureSyncHelper::logMessage('Menu links exported');
}