public function WeatherDisplayPlaceForm::save in Weather 2.0.x
Same name and namespace in other branches
- 8 src/Form/WeatherDisplayPlaceForm.php \Drupal\weather\Form\WeatherDisplayPlaceForm::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/ WeatherDisplayPlaceForm.php, line 182
Class
- WeatherDisplayPlaceForm
- Form controller for the weather_display_place entity edit forms.
Namespace
Drupal\weather\FormCode
public function save(array $form, FormStateInterface $form_state) {
$status = parent::save($form, $form_state);
// Invalidate related weather display cache, once place is saved.
$display_type = $this->entity->display_type->value;
$display_number = $this->entity->display_number->value;
$displays = $this->entityTypeManager
->getStorage('weather_display')
->loadByProperties([
'type' => $display_type,
'number' => $display_number,
]);
foreach ($displays as $display) {
$tags = $display
->getCacheTags();
// Separately invalidate cache_tag for user.
// For correct page update after adding new location.
$tags[] = 'weather_display:' . $this
->currentUser()
->id();
Cache::invalidateTags($tags);
}
// Show message.
if ($status == SAVED_NEW) {
$message = $this
->t('Added new place to weather display');
}
else {
$message = $this
->t('Updated existing place in weather display');
}
$this
->messenger()
->addStatus($message);
switch ($this->entity->display_type->value) {
case WeatherDisplayInterface::USER_TYPE:
$form_state
->setRedirectUrl(Url::fromRoute('weather.user.settings', [
'user' => $this->entity->display_number->value,
]));
break;
default:
$form_state
->setRedirectUrl(Url::fromRoute('weather.settings'));
break;
}
return $status;
}