public function SelectionBase::validateAutocompleteInput

Implements SelectionInterface::validateAutocompleteInput().

Overrides SelectionInterface::validateAutocompleteInput

File

drupal/core/modules/entity_reference/lib/Drupal/entity_reference/Plugin/entity_reference/selection/SelectionBase.php, line 221
Contains \Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase.

Class

SelectionBase
Plugin implementation of the 'selection' entity_reference.

Namespace

Drupal\entity_reference\Plugin\entity_reference\selection

Code

public function validateAutocompleteInput($input, &$element, &$form_state, $form, $strict = TRUE) {
  $entities = $this
    ->getReferencableEntities($input, '=', 6);
  $params = array(
    '%value' => $input,
    '@value' => $input,
  );
  if (empty($entities)) {
    if ($strict) {

      // Error if there are no entities available for a required field.
      form_error($element, t('There are no entities matching "%value".', $params));
    }
  }
  elseif (count($entities) > 5) {
    $params['@id'] = key($entities);

    // Error if there are more than 5 matching entities.
    form_error($element, t('Many entities are called %value. Specify the one you want by appending the id in parentheses, like "@value (@id)".', $params));
  }
  elseif (count($entities) > 1) {

    // More helpful error if there are only a few matching entities.
    $multiples = array();
    foreach ($entities as $id => $name) {
      $multiples[] = $name . ' (' . $id . ')';
    }
    $params['@id'] = $id;
    form_error($element, t('Multiple entities match this reference; "%multiple". Specify the one you want by appending the id in parentheses, like "@value (@id)".', array(
      '%multiple' => implode('", "', $multiples),
    )));
  }
  else {

    // Take the one and only matching entity.
    return key($entities);
  }
}