You are here

public function MiconForm::save in Micon 2.x

Same name and namespace in other branches
  1. 8 src/Form/MiconForm.php \Drupal\micon\Form\MiconForm::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

src/Form/MiconForm.php, line 163

Class

MiconForm
Class MiconForm.

Namespace

Drupal\micon\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $micon = $this->entity;
  if ($this->file) {
    try {
      $zip_path = $this->file
        ->getFileUri();
      $micon
        ->setArchive($zip_path);
    } catch (Exception $e) {
      $form_state
        ->setErrorByName('file', $e
        ->getMessage());
      return;
    }
  }
  $status = $micon
    ->save();
  switch ($status) {
    case SAVED_NEW:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Created the %label Micon package.', [
        '%label' => $micon
          ->label(),
      ]));
      break;
    default:
      $this
        ->messenger()
        ->addStatus($this
        ->t('Saved the %label Micon package.', [
        '%label' => $micon
          ->label(),
      ]));
  }
  drupal_flush_all_caches();
  $form_state
    ->setRedirectUrl($micon
    ->toUrl('collection'));
}