diff --git a/plugins/selection/EntityReference_SelectionHandler_Views.class.php b/plugins/selection/EntityReference_SelectionHandler_Views.class.php old mode 100644 new mode 100755 index 1b036a7..51f4eb2 --- a/plugins/selection/EntityReference_SelectionHandler_Views.class.php +++ b/plugins/selection/EntityReference_SelectionHandler_Views.class.php @@ -147,7 +147,48 @@ class EntityReference_SelectionHandler_Views implements EntityReference_Selectio * Implements EntityReferenceHandler::validateAutocompleteInput(). */ public function validateAutocompleteInput($input, &$element, &$form_state, $form) { - return NULL; + $entities = $this->getReferencableEntities($input, '=', 6); + + // If no entity matched, raise an error. + if (empty($entities)) { + // Error if there are no entities available for a required field. + form_error($element, t('There are no entities matching "%value"', array('%value' => $input))); + } + elseif (count($entities) > 1) { + // Error if there are more than 1 bundle matching. + form_error($element, t('Many entity types match %value. Specify the one you want by appending the id in parentheses, like "example (42)"', array( + '%value' => $input, + ))); + } + else { + // One entity type matched, retrieves corresponding items: + $entities = current($entities); + + if (empty($entities)) { + // Error if there are no entities available for a required field. + form_error($element, t('There are no entities matching "%value"', array('%value' => $input))); + } + elseif (count($entities) > 5) { + // 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)"', array( + '%value' => $input, + '@value' => $input, + '@id' => key($entities), + ))); + } + elseif (count($entities) > 1) { + // More helpful error if there are only a few matching entities. + $multiples = array(); + foreach ($entities as $id => $name) { + $multiples[] = strip_tags($name) . ' (' . $id . ')'; + } + form_error($element, t('Multiple entities match this reference; "%multiple"', array('%multiple' => implode('", "', $multiples)))); + } + else { + // Take the one and only matching entity. + return key($entities); + } + } } /**