You are here

public function SharrreSettingsForm::submitForm in Share Message 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/SharrreSettingsForm.php, line 117

Class

SharrreSettingsForm
Defines a form that configures Share Message settings.

Namespace

Drupal\sharemessage\Form

Code

public function submitForm(array &$form, FormStateInterface $form_state) {
  parent::submitForm($form, $form_state);

  // Check if the name of the library in the remote URL is as expected.
  $url = $form_state
    ->getValue('library_url');
  $sharrre_cdn_url = 'http://jster.net/library/sharrre';
  if (!empty($url) && parse_url($url, PHP_URL_HOST) && parse_url($url, PHP_URL_PATH)) {
    $js = pathinfo($url, PATHINFO_BASENAME);
    if (!preg_match('/^jquery\\.sharrre-[\\d.]*\\.min\\.js$/', $js)) {
      $this
        ->messenger()
        ->addWarning(t('The naming of the library is unexpected. Double check that this is the real Sharrre library. The URL for the minimized version of the library can be found on <a href=":url">Sharrre CDN</a>.', [
        ':url' => $sharrre_cdn_url,
      ]), 'warning');
    }
  }
  elseif (!empty($url)) {
    $this
      ->messenger()
      ->addError(t('The remote URL is unexpected. Please, provide the correct URL to the minimized version of the library found on <a href=":url">Sharrre CDN</a>.', [
      ':url' => $sharrre_cdn_url,
    ]), 'error');
  }

  // If the profile id changes then we need to rebuild the library cache.
  Cache::invalidateTags([
    'library_info',
  ]);
  $this
    ->config('sharemessage.sharrre')
    ->set('services', $form_state
    ->getValue('default_services'))
    ->set('library_url', $form_state
    ->getValue('library_url'))
    ->set('shorter_total', $form_state
    ->getValue('shorter_total'))
    ->set('enable_hover', $form_state
    ->getValue('enable_hover'))
    ->set('enable_counter', $form_state
    ->getValue('enable_counter'))
    ->set('enable_tracking', $form_state
    ->getValue('enable_tracking'))
    ->save();
}