You are here

public function CalendarEventForm::save in Opigno calendar event 8

Same name and namespace in other branches
  1. 3.x src/Form/CalendarEventForm.php \Drupal\opigno_calendar_event\Form\CalendarEventForm::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/CalendarEventForm.php, line 21

Class

CalendarEventForm
Form handler for calendar event type forms.

Namespace

Drupal\opigno_calendar_event\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $calendar_event = $this->entity;
  $insert = $calendar_event
    ->isNew();
  $calendar_event
    ->setValidationRequired(FALSE);
  parent::save($form, $form_state);
  try {
    $link = $calendar_event
      ->toLink($this
      ->t('View'))
      ->toString();
    $context = [
      '%title' => $calendar_event
        ->label(),
      'link' => $link,
    ];
    $t_args = [
      '%title' => $calendar_event
        ->toLink($calendar_event
        ->label())
        ->toString(),
    ];
    $redirect_url = $calendar_event
      ->toUrl();
    if ($insert) {
      $this
        ->logger('content')
        ->notice('%title calendar event created.', $context);
      $this
        ->messenger()
        ->addStatus($this
        ->t('The calendar event %title has been created.', $t_args));
    }
    else {
      $this
        ->logger('content')
        ->notice('%title calendar event updated.', $context);
      $this
        ->messenger()
        ->addStatus($this
        ->t('The calendar event %title has been updated.', $t_args));
    }
  } catch (EntityMalformedException $e) {
    $this
      ->logException($e);
  }
  if ($calendar_event
    ->id()) {
    if (isset($redirect_url) && $calendar_event
      ->access('view')) {
      $form_state
        ->setRedirectUrl($redirect_url);
    }
    else {
      $form_state
        ->setRedirect('<front>');
    }
  }
  else {

    // In the unlikely case something went wrong on save, the calendar event
    // will be rebuilt and the form redisplayed.
    $this
      ->messenger()
      ->addError($this
      ->t('The calendar event could not be saved.'));
    $form_state
      ->setRebuild();
  }
}