function ReorderDisplays::submitForm

Same name in other branches
  1. 8.9.x core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays::submitForm()
  2. 10 core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays::submitForm()
  3. 11.x core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php \Drupal\views_ui\Form\Ajax\ReorderDisplays::submitForm()

Overrides ViewsFormBase::submitForm

File

core/modules/views_ui/src/Form/Ajax/ReorderDisplays.php, line 148

Class

ReorderDisplays
Displays the display reorder form.

Namespace

Drupal\views_ui\Form\Ajax

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
    
    /** @var \Drupal\views_ui\ViewUI $view */
    $view = $form_state->get('view');
    $order = [];
    $user_input = $form_state->getUserInput();
    foreach ($user_input['displays'] as $display => $info) {
        // Add each value that is a field with a weight to our list, but only if
        // it has had its 'removed' checkbox checked.
        if (is_array($info) && isset($info['weight']) && empty($info['removed']['checkbox'])) {
            $order[$display] = $info['weight'];
        }
    }
    // Sort the order array.
    asort($order);
    // Remove the default display from ordering.
    unset($order['default']);
    // Increment up positions.
    $position = 1;
    foreach (array_keys($order) as $display) {
        $order[$display] = $position++;
    }
    // Setting up position and removing deleted displays.
    $displays = $view->get('display');
    foreach ($displays as $display_id => &$display) {
        // Don't touch the default.
        if ($display_id === 'default') {
            $display['position'] = 0;
            continue;
        }
        if (isset($order[$display_id])) {
            $display['position'] = $order[$display_id];
        }
        else {
            $display['deleted'] = TRUE;
        }
    }
    $view->set('display', $displays);
    // Store in cache.
    $view->cacheSet();
    $url = $view->toUrl('edit-form')
        ->setOption('fragment', 'views-tab-default');
    $form_state->setRedirectUrl($url);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.