You are here

public function SearchApiFederatedSolrSearchAppSettingsForm::formValidationPathValidate in Search API Federated Solr 8

Same name and namespace in other branches
  1. 8.3 src/Form/SearchApiFederatedSolrSearchAppSettingsForm.php \Drupal\search_api_federated_solr\Form\SearchApiFederatedSolrSearchAppSettingsForm::formValidationPathValidate()
  2. 8.2 src/Form/SearchApiFederatedSolrSearchAppSettingsForm.php \Drupal\search_api_federated_solr\Form\SearchApiFederatedSolrSearchAppSettingsForm::formValidationPathValidate()
  3. 4.x src/Form/SearchApiFederatedSolrSearchAppSettingsForm.php \Drupal\search_api_federated_solr\Form\SearchApiFederatedSolrSearchAppSettingsForm::formValidationPathValidate()

Validates that the provided search path is not in use by an existing route.

Parameters

array $form: An associative array containing the structure of the form.

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

File

src/Form/SearchApiFederatedSolrSearchAppSettingsForm.php, line 283
Contains \Drupal\search_api_solr_federated\Form\SearchApiFederatedSolrSearchAppSettingsForm.

Class

SearchApiFederatedSolrSearchAppSettingsForm
Class SearchApiFederatedSolrSearchAppSettingsForm.

Namespace

Drupal\search_api_federated_solr\Form

Code

public function formValidationPathValidate(array &$form, FormStateInterface $form_state) {
  $path = $form_state
    ->getValue('path');
  if ($path) {

    // Check if a route with the config path value already exists.
    $router = \Drupal::service('router.no_access_checks');
    $result = FALSE;
    try {
      $result = $router
        ->match($path);
    } catch (\Exception $e) {

      // This is what we want, indicates the route path doesn't exist.
    }

    // If the route path exists for something other than the search route,
    // set an error on the form.
    if ($result && $result['_route'] !== "search_api_federated_solr.search") {
      $form_state
        ->setErrorByName('path', t('The path you have entered already exists'));
    }
  }
}