You are here

public function CacheControlForm::validateForm in Akamai 8.3

Same name and namespace in other branches
  1. 8 lib/Drupal/akamai/Form/CacheControlForm.php \Drupal\akamai\Form\CacheControlForm::validateForm()
  2. 8.2 src/Form/CacheControlForm.php \Drupal\akamai\Form\CacheControlForm::validateForm()

Form validation 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 FormBase::validateForm

File

src/Form/CacheControlForm.php, line 145

Class

CacheControlForm
A simple form for testing the Akamai integration, or doing manual clears.

Namespace

Drupal\akamai\Form

Code

public function validateForm(array &$form, FormStateInterface $form_state) {
  $objects = explode(PHP_EOL, $form_state
    ->getValue('paths'));
  $method = $form_state
    ->getValue('method');
  if ($method == 'url') {
    foreach ($objects as $path) {

      // Remove any leading slashes so we can control them later.
      if ($path[0] === '/') {
        $path = ltrim($path, '/');
      }
      $path = trim($path);
      if (UrlHelper::isExternal($path)) {
        $full_urls[] = $path;
      }
      else {
        $url = Url::fromUserInput('/' . $path);
        if ($url
          ->isRouted() || is_file($path)) {
          $paths_to_clear[] = $path;
        }
        else {
          $invalid_paths[] = $path;
        }
      }
    }
  }
  else {
    $paths_to_clear = $objects;
  }
  if (!empty($full_urls)) {
    $form_state
      ->setErrorByName('paths', $this
      ->t('Please enter only relative paths, not full URLs.'));
  }
  if (!empty($invalid_paths)) {
    $paths = implode(",", $invalid_paths);
    $message = $this
      ->formatPlural(count($invalid_paths), 'The \'@paths\' path is invalid and does not exist on the site. Please provide at least one valid URL for purging.', '@paths paths are invalid and do not exist on the site. Please provide valid URLs for purging.', [
      '@paths' => $paths,
    ]);
    $form_state
      ->setErrorByName('paths', $message);
  }
  if (empty($paths_to_clear)) {
    $form_state
      ->setErrorByName('paths', $this
      ->t('Please enter at least one valid object for %method purging.', [
      '%method' => $method,
    ]));
  }
}