ViewEditForm::save

public ViewEditForm::save(array $form, FormStateInterface $form_state)

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

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

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

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

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'])) {
      $executable->displayHandlers->remove($id);
      unset($displays[$id]);
    }
  }

  // Rename display ids if needed.
  foreach ($executable->displayHandlers as $id => $display) {
    if (!empty($display->display['new_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', array(
        'view' => $view->id(),
        'display_id' => $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');
      }
    }
    // @todo Use Url::fromPath() once https://www.drupal.org/node/2351379 is
    //   resolved.
    $form_state->setRedirectUrl(Url::fromUri("base:$destination"));
  }

  $view->save();

  drupal_set_message($this->t('The view %name has been saved.', array('%name' => $view->label())));

  // Remove this view from cache so we can edit it properly.
  $this->tempStore->delete($view->id());
}
doc_Drupal
2016-10-29 09:54:00
Comments
Leave a Comment

Please login to continue.