You are here

public function SensorForm::save in Monitoring 8

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/SensorForm.php, line 271
Contains \Drupal\monitoring\Form\SensorForm.

Class

SensorForm
Sensor settings form controller.

Namespace

Drupal\monitoring\Form

Code

public function save(array $form, FormStateInterface $form_state) {
  parent::save($form, $form_state);
  $sensor_config = $this->entity;
  $sensor_id = $form_state
    ->getValue('id');
  if ($sensor_config
    ->isEnabled()) {
    $route = 'entity.monitoring_sensor_config.details_form';
  }
  else {
    $route = 'entity.monitoring_sensor_config.edit_form';
  }
  $form_state
    ->setRedirect('monitoring.sensors_overview_settings');
  $url = Url::fromRoute($route, array(
    'monitoring_sensor_config' => $sensor_id,
  ));

  // Message with the link to sensor details page.
  $this
    ->messenger()
    ->addMessage(t('Sensor <a href="@url">@label</a> saved.', array(
    '@url' => $url
      ->toString(),
    '@label' => $form_state
      ->getValue('label'),
  )));
}