You are here

public function FileEditForm::save in File Entity (fieldable files) 8.2

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

src/Form/FileEditForm.php, line 108

Class

FileEditForm
Form controller for file type forms.

Namespace

Drupal\file_entity\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $file = $this->entity;
  $insert = $file
    ->isNew();
  $file
    ->save();
  $t_args = array(
    '%title' => $file
      ->label(),
  );
  if ($insert) {
    $this
      ->messenger()
      ->addMessage(t('%title has been created.', $t_args));
  }
  else {
    $this
      ->messenger()
      ->addMessage(t('%title has been updated.', $t_args));
  }

  // Check if file ID exists.
  if ($file
    ->id()) {
    $form_state
      ->setRedirectUrl($file
      ->toUrl());
  }
  else {

    // In the unlikely case something went wrong on save, the file will be
    // rebuilt and file form redisplayed the same way as in preview.
    $this
      ->messenger()
      ->addMessage(t('The post could not be saved.'), 'error');
    $form_state
      ->setRebuild();
  }
}