You are here

function menu_item_extras_menu_submit in Menu Item Extras 8

Save extras settings for menus.

Parameters

array $form: Menu edit/add from.

\Drupal\Core\Form\FormStateInterface $form_state: Form state.

1 string reference to 'menu_item_extras_menu_submit'
menu_item_extras_form_alter in ./menu_item_extras.module
Implements hook_form_alter().

File

./menu_item_extras.module, line 193
Adds body field to the menu item.

Code

function menu_item_extras_menu_submit(array &$form, FormStateInterface $form_state) {
  $add_extras = $form_state
    ->getValue('add_extras');
  $extras_settings = \Drupal::configFactory()
    ->getEditable('menu_item_extras.settings');
  $allowed_menus = (array) $extras_settings
    ->get('allowed_menus');
  $menu_name = $form_state
    ->getValue('id');
  if ($add_extras) {

    // If not enabled, enable extras for this menu.
    if (!in_array($menu_name, $allowed_menus)) {
      $allowed_menus[] = $menu_name;
      $extras_settings
        ->set('allowed_menus', $allowed_menus);
      $extras_settings
        ->save(TRUE);
    }
  }
  else {
    if (in_array($menu_name, $allowed_menus)) {
      $allowed_menus = array_diff($allowed_menus, [
        $menu_name,
      ]);
      $extras_settings
        ->set('allowed_menus', $allowed_menus);
      $extras_settings
        ->save(TRUE);
      $query = \Drupal::entityQuery('menu_link_content')
        ->condition('menu_name', $menu_name);
      $results = $query
        ->execute();

      // TODO remove think about batch.
      if ($results) {
        $controller = \Drupal::entityTypeManager()
          ->getStorage('menu_link_content');

        /** @var \Drupal\menu_link_content\MenuLinkContentInterface[] $entities */
        $entities = $controller
          ->loadMultiple($results);
        foreach ($entities as $entity) {
          $entity
            ->set('body', NULL);
          $entity
            ->save();
        }
      }
    }
  }
}