You are here

protected function MenuForm::submitOverviewForm in Colossal Menu 2.x

Same name and namespace in other branches
  1. 8 src/Form/MenuForm.php \Drupal\colossal_menu\Form\MenuForm::submitOverviewForm()

Submit handler for the menu overview form.

This function takes great care in saving parent items first, then items underneath them. Saving items in the incorrect order can break the tree.

1 call to MenuForm::submitOverviewForm()
MenuForm::save in src/Form/MenuForm.php
Form submission handler for the 'save' action.

File

src/Form/MenuForm.php, line 125

Class

MenuForm
Settings form for menus.

Namespace

Drupal\colossal_menu\Form

Code

protected function submitOverviewForm(array $complete_form, FormStateInterface $form_state) {
  $input = $form_state
    ->getUserInput();
  foreach ($input['links'] as $id => $input) {
    $storage = $this->entityTypeManager
      ->getStorage('colossal_menu_link');

    /** @var \Drupal\colossal_menu\Entity\Link $link */
    $link = $storage
      ->load($id);
    $diff = FALSE;
    if (array_key_exists('parent', $input) && $link
      ->get('parent')
      ->access('edit')) {
      if (!$link
        ->getParent() && $input['parent']) {
        $diff = TRUE;
        $link
          ->setParent($input['parent']);
      }
      elseif ($link
        ->getParent() && $link
        ->getParent()
        ->id() != $input['parent']) {
        $diff = TRUE;
        $link
          ->setParent($input['parent']);
      }
    }
    if (array_key_exists('weight', $input) && $link
      ->get('weight')
      ->access('edit')) {
      if ($link
        ->getWeight() != $input['weight']) {
        $diff = TRUE;
        $link
          ->setWeight($input['weight']);
      }
    }
    if (array_key_exists('enabled', $input) && $link
      ->get('enabled')
      ->access('edit')) {
      $enabled = (bool) $input['enabled'];
      if ($link
        ->isEnabled() != $enabled) {
        $diff = TRUE;
        $link
          ->setEnabled($enabled);
      }
    }
    if ($diff) {
      $link
        ->save();
    }
  }
}