function ViewEditForm::save

Same name and namespace in other branches
  1. 9 core/modules/views_ui/src/ViewEditForm.php \Drupal\views_ui\ViewEditForm::save()
  2. 10 core/modules/views_ui/src/ViewEditForm.php \Drupal\views_ui\ViewEditForm::save()
  3. 11.x core/modules/views_ui/src/ViewEditForm.php \Drupal\views_ui\ViewEditForm::save()

Overrides EntityForm::save

File

core/modules/views_ui/src/ViewEditForm.php, line 251

Class

ViewEditForm
Form controller for the Views edit form.

Namespace

Drupal\views_ui

Code

public function save(array $form, FormStateInterface $form_state) {
    $view = $this->entity;
    $executable = $view->getExecutable();
    $executable->initDisplay();
    // Go through and remove displayed scheduled for removal.
    $displays = $view->get('display');
    foreach ($displays as $id => $display) {
        if (!empty($display['deleted'])) {
            // Remove view display from view attachment under the attachments
            // options.
            $display_handler = $executable->displayHandlers
                ->get($id);
            if ($attachments = $display_handler->getAttachedDisplays()) {
                foreach ($attachments as $attachment) {
                    $attached_options = $executable->displayHandlers
                        ->get($attachment)
                        ->getOption('displays');
                    unset($attached_options[$id]);
                    $executable->displayHandlers
                        ->get($attachment)
                        ->setOption('displays', $attached_options);
                }
            }
            $executable->displayHandlers
                ->remove($id);
            unset($displays[$id]);
        }
    }
    // Rename display ids if needed.
    foreach ($executable->displayHandlers as $id => $display) {
        if (!empty($display->display['new_id']) && $display->display['new_id'] !== $display->display['id'] && empty($display->display['deleted'])) {
            $new_id = $display->display['new_id'];
            $display->display['id'] = $new_id;
            unset($display->display['new_id']);
            $executable->displayHandlers
                ->set($new_id, $display);
            $displays[$new_id] = $displays[$id];
            unset($displays[$id]);
            // Redirect the user to the renamed display to be sure that the page itself exists and doesn't throw errors.
            $form_state->setRedirect('entity.view.edit_display_form', [
                'view' => $view->id(),
                'display_id' => $new_id,
            ]);
        }
        elseif (isset($display->display['new_id'])) {
            unset($display->display['new_id']);
        }
    }
    $view->set('display', $displays);
    // @todo: Revisit this when https://www.drupal.org/node/1668866 is in.
    $query = $this->requestStack
        ->getCurrentRequest()->query;
    $destination = $query->get('destination');
    if (!empty($destination)) {
        // Find out the first display which has a changed path and redirect to this url.
        $old_view = Views::getView($view->id());
        $old_view->initDisplay();
        foreach ($old_view->displayHandlers as $id => $display) {
            // Only check for displays with a path.
            $old_path = $display->getOption('path');
            if (empty($old_path)) {
                continue;
            }
            if ($display->getPluginId() == 'page' && $old_path == $destination && $old_path != $view->getExecutable()->displayHandlers
                ->get($id)
                ->getOption('path')) {
                $destination = $view->getExecutable()->displayHandlers
                    ->get($id)
                    ->getOption('path');
                $query->remove('destination');
            }
        }
        $form_state->setRedirectUrl(Url::fromUri("base:{$destination}"));
    }
    $view->save();
    $this->messenger()
        ->addStatus($this->t('The view %name has been saved.', [
        '%name' => $view->label(),
    ]));
    // Remove this view from cache so we can edit it properly.
    $this->tempStore
        ->delete($view->id());
}

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