You are here

public function RegistrationForm::save in RNG - Events and Registrations 8

Same name and namespace in other branches
  1. 8.2 src/Form/RegistrationForm.php \Drupal\rng\Form\RegistrationForm::save()
  2. 3.x src/Form/RegistrationForm.php \Drupal\rng\Form\RegistrationForm::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/RegistrationForm.php, line 183

Class

RegistrationForm
Form controller for registrations.

Namespace

Drupal\rng\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $registration = $this->entity;
  $t_args = [
    '@type' => $registration
      ->bundle(),
    '%label' => $registration
      ->label(),
    '%id' => $registration
      ->id(),
  ];
  if (!$registration
    ->isNew()) {
    $registration
      ->setNewRevision(!$form_state
      ->isValueEmpty('revision'));
  }
  if ($registration
    ->save() == SAVED_NEW) {
    drupal_set_message($this
      ->t('Registration has been created.', $t_args));
  }
  else {
    drupal_set_message($this
      ->t('Registration was updated.', $t_args));
  }

  /** @var \Drupal\rng\RegistrantInterface[] $registrants */
  $registrants = $form_state
    ->get('registrants_after');
  foreach ($registrants as $registrant) {
    $registrant
      ->setRegistration($registration);
    $registrant
      ->save();
  }

  /** @var \Drupal\rng\RegistrantInterface[] $registrants_delete */
  $registrants_delete = $form_state
    ->get('registrants_delete');
  foreach ($registrants_delete as $registrant) {
    $registrant
      ->delete();
  }
  if ($registration
    ->access('view')) {
    $form_state
      ->setRedirectUrl($registration
      ->toUrl());
  }
  else {
    $form_state
      ->setRedirect('<front>');
  }
}