You are here

public function TourTipForm::submitForm in Tour UI 8

Form submission handler.

Parameters

array $form: An associative array containing the structure of the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides FormInterface::submitForm

File

src/Form/TourTipForm.php, line 159

Class

TourTipForm
Form controller for the tour tip plugin edit forms.

Namespace

Drupal\tour_ui\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $storage = $form_state
    ->getStorage();
  $tour = $storage['#tour'];
  $tip = $storage['#tip'];

  // Get available fields from current tip plugin.
  $configuration = $tip
    ->getConfiguration();

  // Build a new tip.
  $new_tip = $tip
    ->getConfiguration();
  foreach ($configuration as $name => $configuration_value) {
    $value = $form_state
      ->getValue($name);
    $new_tip[$name] = is_array($value) ? array_filter($value) : $value;
  }

  // Rebuild the tips.
  $new_tip_list = $tour
    ->getTips();
  $new_tips = [];
  if (!empty($new_tip_list)) {
    foreach ($new_tip_list as $tip) {
      $new_tips[$tip
        ->id()] = $tip
        ->getConfiguration();
    }
  }

  // We have to cleanup the attributes as these are selection dependent.
  // @see \Drupal\tour_ui\Plugin\tour_ui\tip\TipPluginTextExtended::buildConfigurationForm
  $input = $form_state
    ->getUserInput();
  $selected = $input['attributes']['selector_type'];
  $attributes =& $new_tip['attributes'];
  if ($selected !== 'data-id') {
    unset($attributes['data-id']);
  }
  if ($selected !== 'data-class') {
    unset($attributes['data-class']);
  }
  if (!is_array($new_tip['attributes'])) {
    $new_tip['attributes'] = [];
  }

  // Add our tip and save.
  $new_tips[$new_tip['id']] = $new_tip;
  $tour
    ->set('tips', $new_tips);
  $tour
    ->save();
  if (isset($storage['#new'])) {
    $this->messenger
      ->addMessage($this
      ->t('The %tip tip has been created.', [
      '%tip' => $new_tip['label'],
    ]));
  }
  else {
    $this->messenger
      ->addMessage($this
      ->t('Updated the %tip tip.', [
      '%tip' => $new_tip['label'],
    ]));
  }
  $form_state
    ->setRedirect('entity.tour.edit_form', [
    'tour' => $tour
      ->id(),
  ]);
  return $tour;
}