function public static function CdnCustom::validateFormElement

8.x-3.x CdnCustom.php public static CdnCustom::validateFormElement(Element $form, FormStateInterface $form_state)

Form validation handler.

Parameters

\Drupal\bootstrap\Utility\Element $form: The Element object that comprises the form.

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

Overrides SettingBase::validateFormElement

Class

CdnCustom
The "cdn_custom" theme setting.

Namespace

Drupal\bootstrap\Plugin\Setting\Advanced\Cdn

Source src/Plugin/Setting/Advanced/Cdn/CdnCustom.php (line 105)

public static function validateFormElement(Element $form, FormStateInterface $form_state) {
  // Immediately return if this isn't the currently selected CDN Provider.
  if ($form_state->getValue('cdn_provider') !== 'custom') {
    return;
  }

  $theme = SystemThemeSettings::getTheme($form, $form_state);

  /** @var \Drupal\bootstrap\Plugin\Provider\Custom $provider */
  $provider = ProviderManager::load($theme, 'custom');

  $urls = array_filter(array_map('trim', preg_split("/\r\n|\n/", $form_state->getValue('cdn_custom', ''))));

  $invalid = [];
  foreach ($urls as $url) {
    try {
      $provider->validateUrl($url);
    }
    catch (\Exception $e) {
      $invalid[] = $e->getMessage();
    }
  }
  if ($invalid) {
    $form_state->setErrorByName('cdn_custom', t('Invalid Custom URLs: <ul><li>@invalid</li></ul>', [
      '@invalid' => new FormattableMarkup(implode('</li><li>', $invalid), []),
    ]));

    // Unfortunately, any errors set during validation prevents the form
    // rebuilding functionality from working. This has to be changed here.
    $form->cdn->cdn_provider->custom->setProperty('open', TRUE);
  }
}