You are here

public function BeautytipsConfigForm::submitForm in BeautyTips 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 ConfigFormBase::submitForm

File

src/Form/BeautytipsConfigForm.php, line 245

Class

BeautytipsConfigForm
Beautytips admin settings form.

Namespace

Drupal\beautytips\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  $config = $this
    ->configFactory()
    ->getEditable('beautytips.basic');
  $values = $form_state
    ->getValues();
  if (count($values)) {
    $custom_style = [];
    $css_style = [];
    foreach ($values as $option => $value) {
      if (strpos($option, 'bt-options-box-') === 0) {
        $option = str_replace('bt-options-box-', '', $option);
        $custom_style[$option] = $value;
      }
      elseif ($option == 'bt-options-cssClass') {
        $option = str_replace('bt-options-', '', $option);
        $custom_style[$option] = $value;
      }
      elseif (strpos($option, 'bt-options-css-') === 0) {
        $option = str_replace('bt-options-css-', '', $option);
        if ($value) {
          $css_style[$option] = $value;
        }
      }
    }

    // Store the defaults - they will be passed to javascript.
    $style = beautytips_get_style($values['beautytips_default_style']);
    if (count($custom_style)) {
      foreach ($custom_style as $option => $value) {
        if ($option == 'shadow') {
          if ($value != 'default') {
            $style['shadow'] = $value == 'shadow' ? TRUE : FALSE;
          }
        }
        elseif (!empty($value) || $value == '0') {
          $style[$option] = is_numeric($value) ? (int) $value : (string) $value;
        }
      }
    }
    if (count($css_style)) {
      foreach ($css_style as $option => $value) {
        if (!empty($value)) {
          $style['cssStyles'][$option] = (string) $value;
        }
      }
      if (!empty($css_style)) {
        $custom_style['cssStyles'] = $css_style;
      }
    }
    $config
      ->set('beautytips_defaults', $style);
    $config
      ->set('beautytips_custom_style', $custom_style);
    $config
      ->set('beautytips_default_style', $values['beautytips_default_style']);
    $config
      ->set('beautytips_always_add', $values['beautytips_always_add']);
    $config
      ->set('beautytips_ltr', $values['beautytips_ltr']);
    Cache::invalidateTags([
      'beautytips',
    ]);

    // Store array of selectors that bt will be added to on every page.
    $selectors = explode(",", $values['beautytips_added_selectors_array']);
    if (count($selectors)) {
      foreach ($selectors as $key => $selector) {
        $selectors[$key] = trim($selector);
      }
    }
    $config
      ->set('beautytips_added_selectors_array', $selectors);
    if (!empty($values)) {
      foreach ($values as $key => $value) {
        if (in_array($key, $this->fieldNames)) {
          \Drupal::state()
            ->set($key, $value);
        }
      }
    }
  }
  if ($this->moduleHandler
    ->moduleExists('beautytips_ui')) {
    beautytips_ui_admin_submit($form, $form_state);
  }
  $config
    ->save();
  parent::submitForm($form, $form_state);
}