function public static function CdnProvider::validateFormElement

8.x-3.x CdnProvider.php public static CdnProvider::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

CdnProvider
The "cdn_provider" theme setting.

Namespace

Drupal\bootstrap\Plugin\Setting\Advanced\Cdn

Source src/Plugin/Setting/Advanced/Cdn/CdnProvider.php (line 166)

public static function validateFormElement(Element $form, FormStateInterface $form_state) {
  parent::validateFormElement($form, $form_state);
  $theme = SystemThemeSettings::getTheme($form, $form_state);
  $provider = ProviderManager::load($theme, $form_state->getValue('cdn_provider'));

  // Validate the provider.
  if (!($provider instanceof Broken)) {
    $cdnVersion = $form_state->getValue('cdn_version', $theme->getSetting('cdn_version', Bootstrap::FRAMEWORK_VERSION));
    $cdnTheme = $form_state->getValue('cdn_theme', $theme->getSetting('cdn_theme'));
    $assets = $provider->getCdnAssets($cdnVersion, $cdnTheme);

    // Now validate that each asset is reachable.
    $unreachable = [];
    foreach ($assets->toArray() as $asset) {
      $url = $asset->getUrl();
      if (!Bootstrap::checkUrlIsReachable($url)) {
        $unreachable[] = Link::fromTextAndUrl($url, Url::fromUri($url)->setOption('attributes', ['target' => '_blank']));
      }
    }

    if ($unreachable) {
      $form_state->setErrorByName('cdn_provider', t('Unable to reach the following @provider assets: <ul><li>@unreachable</li>', [
        '@provider' => $provider->getLabel(),
        '@unreachable' => Markup::create(implode('</li><li>', $unreachable)),
      ]));
      return;
    }

    // Check for exceptions (API HTTP request errors).
    if (static::checkCdnExceptions($provider)) {
      $form_state->setErrorByName('cdn_provider', t('Unable to use @provider assets. Please choose a different CDN Provider.', [
        '@provider' => $provider->getLabel(),
      ]));
      return;
    }
  }

  if ($provider instanceof FormInterface) {
    $provider->validateFormElement($form, $form_state);
  }
}