You are here

public function AutocompletionConfigurationEditForm::validateUriElement in Search Autocomplete 8

Same name and namespace in other branches
  1. 2.x src/Form/AutocompletionConfigurationEditForm.php \Drupal\search_autocomplete\Form\AutocompletionConfigurationEditForm::validateUriElement()

Validation callback for URI form.

Parameters

array $element: The element itself

FormStateInterface $form_state: The submitted form data.

File

src/Form/AutocompletionConfigurationEditForm.php, line 245

Class

AutocompletionConfigurationEditForm
Class AutocompletionConfigurationEditForm

Namespace

Drupal\search_autocomplete\Form

Code

public function validateUriElement($element, FormStateInterface $form_state) {
  $source = $form_state
    ->getValue('source');
  $entity_ids = NULL;

  // Check if source input is a valid View display.
  $input_source = explode('::', $source);
  if (count($input_source) == 2) {
    $entity_ids = Drupal::service('entity.query')
      ->get('view')
      ->condition('status', TRUE)
      ->condition('id', $input_source[0])
      ->condition("display.*.id", $input_source[1])
      ->execute();
  }

  // If Views are found: it's OK.
  if (!empty($entity_ids)) {
    return;
  }
  elseif (UrlHelper::isExternal($source)) {
    return;
  }
  else {
    if (!Drupal::service('path.validator')
      ->isValid($source)) {
      $form_state
        ->setErrorByName('source', $this
        ->t('The input source is not valid. Please enter a Drupal valid path, a View display (using completion) or a valid external URL.'));
    }
  }
}