You are here

public function MultigraphForm::addSensorSubmit in Monitoring 8

Adds sensor to entity when 'Add sensor' button is pressed.

Parameters

array $form: The form structure array

\Drupal\Core\Form\FormStateInterface $form_state: The form state structure.

File

modules/multigraph/src/Form/MultigraphForm.php, line 207
Contains \Drupal\monitoring_multigraph\Form\MultigraphForm.

Class

MultigraphForm
Multigraph settings form controller.

Namespace

Drupal\monitoring_multigraph\Form

Code

public function addSensorSubmit(array $form, FormStateInterface $form_state) {
  $form_state
    ->setRebuild();

  /** @var \Drupal\monitoring_multigraph\Entity\Multigraph $multigraph */
  $multigraph = $this->entity;

  // Add any selected sensor to entity.
  if ($sensor_name = $form_state
    ->getValue(array(
    'sensor_add_select',
  ))) {
    $sensor_label = $this->entityTypeManager
      ->getStorage('monitoring_sensor_config')
      ->load($sensor_name)
      ->getLabel();
    $multigraph
      ->addSensor($sensor_name);
    $this
      ->messenger()
      ->addWarning($this
      ->t('Sensor "@sensor_label" added. You have unsaved changes.', array(
      '@sensor_label' => $sensor_label,
    )));
  }
  else {
    $this
      ->messenger()
      ->addWarning($this
      ->t('No sensor selected.'));
  }
}