You are here

public function EntityTestForm::save in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/system/tests/modules/entity_test/src/EntityTestForm.php \Drupal\entity_test\EntityTestForm::save()

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/system/tests/modules/entity_test/src/EntityTestForm.php, line 52
Contains \Drupal\entity_test\EntityTestForm.

Class

EntityTestForm
Form controller for the test entity edit forms.

Namespace

Drupal\entity_test

Code

public function save(array $form, FormStateInterface $form_state) {
  try {
    $entity = $this->entity;

    // Save as a new revision if requested to do so.
    if (!$form_state
      ->isValueEmpty('revision')) {
      $entity
        ->setNewRevision();
    }
    $is_new = $entity
      ->isNew();
    $entity
      ->save();
    if ($is_new) {
      $message = t('%entity_type @id has been created.', array(
        '@id' => $entity
          ->id(),
        '%entity_type' => $entity
          ->getEntityTypeId(),
      ));
    }
    else {
      $message = t('%entity_type @id has been updated.', array(
        '@id' => $entity
          ->id(),
        '%entity_type' => $entity
          ->getEntityTypeId(),
      ));
    }
    drupal_set_message($message);
    if ($entity
      ->id()) {
      $entity_type = $entity
        ->getEntityTypeId();
      $form_state
        ->setRedirect("entity.{$entity_type}.edit_form", array(
        $entity_type => $entity
          ->id(),
      ));
    }
    else {

      // Error on save.
      drupal_set_message(t('The entity could not be saved.'), 'error');
      $form_state
        ->setRebuild();
    }
  } catch (\Exception $e) {
    \Drupal::state()
      ->set('entity_test.form.save.exception', get_class($e) . ': ' . $e
      ->getMessage());
  }
}