You are here

public function MenuImportForm::submitForm in Menu Import and Export 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ConfigFormBase::submitForm

File

src/Form/MenuImportForm.php, line 65

Class

MenuImportForm
Configure Menu Export settings.

Namespace

Drupal\menu_export\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $invalidMenus = [];
  $menus = $this
    ->config('menu_export.export_data')
    ->get();
  foreach ($menus as $key => $menu) {
    $menu_name = $menu['menu_name']['value'];
    if (!Menu::load($menu_name)) {
      $invalidMenus[] = $menu_name;
      continue;
    }
    unset($menu['id']);
    $menuLinkEntity = \Drupal::entityQuery('menu_link_content')
      ->condition('uuid', $menu['uuid'])
      ->execute();
    if (!$menuLinkEntity) {
      $menuLinkEntity = MenuLinkContent::create();
    }
    else {
      $menuLinkEntity = MenuLinkContent::load(reset($menuLinkEntity));
    }
    foreach ($menu as $kkey => $items) {
      $menuLinkEntity
        ->set($kkey, $items);
    }
    $menuLinkEntity
      ->save();
    unset($menuLinkEntity);
  }
  if (count($invalidMenus)) {
    $this
      ->messenger()
      ->addError($this
      ->t('Menu(s) @menus not found', [
      '@menus' => implode(',', $invalidMenus),
    ]));
  }
  else {
    $this
      ->messenger()
      ->addStatus($this
      ->t('Menu(s) imported successfully'));
  }
}