You are here

public function WeatherDisplayForm::save in Weather 2.0.x

Same name and namespace in other branches
  1. 8 src/Form/WeatherDisplayForm.php \Drupal\weather\Form\WeatherDisplayForm::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/WeatherDisplayForm.php, line 258

Class

WeatherDisplayForm
Form controller for the weather_display entity edit forms.

Namespace

Drupal\weather\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  $type = $form_state
    ->getValue('type');
  $display_number = $form_state
    ->getValue('number');

  // Set display number before save.
  if ($form_state
    ->getValue('number') == NULL) {
    $free_number = $this
      ->getFreeDisplayNumber($type);
    $this->entity
      ->set('number', $free_number);
  }
  $this->entity
    ->set('config', $form_state
    ->getValue('config'));

  // Make sure we have only one instance of display with type 'default'.
  if ($type == WeatherDisplayInterface::DEFAULT_TYPE) {
    $defaultDisplay = $this->weatherDisplayStorage
      ->loadByProperties([
      'type' => WeatherDisplayInterface::DEFAULT_TYPE,
    ]);
    $defaultDisplay = reset($defaultDisplay);
    if ($defaultDisplay instanceof WeatherDisplayInterface) {
      $this->entity->id = $defaultDisplay
        ->id();
      $this->entity
        ->enforceIsNew(FALSE);
    }
  }
  elseif ($type == WeatherDisplayInterface::USER_TYPE) {
    $userDisplayExists = $this->weatherDisplayStorage
      ->loadByProperties([
      'type' => WeatherDisplayInterface::USER_TYPE,
      'number' => $this
        ->currentUser()
        ->id(),
    ]);
    $userDisplay = reset($userDisplayExists);
    if ($userDisplay instanceof WeatherDisplayInterface) {
      $this->entity->id = $userDisplay
        ->id();
      $this->entity
        ->enforceIsNew(FALSE);
    }
  }
  elseif ($type == WeatherDisplayInterface::SYSTEM_WIDE_TYPE) {
    $displayExists = $this->weatherDisplayStorage
      ->loadByProperties([
      'type' => WeatherDisplayInterface::SYSTEM_WIDE_TYPE,
      'number' => $display_number,
    ]);
    $systemwideDisplay = reset($displayExists);
    if ($systemwideDisplay instanceof WeatherDisplayInterface) {
      $this->entity->id = $systemwideDisplay
        ->id();
      $this->entity
        ->enforceIsNew(FALSE);
    }
  }
  $status = parent::save($form, $form_state);
  if ($status == SAVED_NEW) {
    $message = $this
      ->t('Created new Weather display');
  }
  else {
    $message = $this
      ->t('Updated existing Weather display');
  }
  $this
    ->messenger()
    ->addStatus($message);
  switch ($this->entity->type->value) {
    case WeatherDisplayInterface::USER_TYPE:
      $form_state
        ->setRedirectUrl(Url::fromRoute('weather.user.settings', [
        'user' => $this->entity->number->value,
      ]));
      break;
    default:
      $form_state
        ->setRedirectUrl(Url::fromRoute('weather.settings'));
      break;
  }
  return $status;
}