diff --git a/core/modules/entity_reference/entity_reference.module b/core/modules/entity_reference/entity_reference.module index fd51ff6..c07f0d2 100644 --- a/core/modules/entity_reference/entity_reference.module +++ b/core/modules/entity_reference/entity_reference.module @@ -63,18 +63,16 @@ function entity_reference_menu() { $items['entity_reference/autocomplete/single/%/%/%'] = array( 'title' => 'Entity Reference Autocomplete', - 'page callback' => 'entity_reference_autocomplete_callback', + 'page callback' => 'NOT_USED', 'page arguments' => array(2, 3, 4, 5), - 'access callback' => 'entity_reference_autocomplete_access_callback', - 'access arguments' => array(2, 3, 4, 5), + 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); $items['entity_reference/autocomplete/tags/%/%/%'] = array( 'title' => 'Entity Reference Autocomplete', - 'page callback' => 'entity_reference_autocomplete_callback', + 'page callback' => 'NOT_USED', 'page arguments' => array(2, 3, 4, 5), - 'access callback' => 'entity_reference_autocomplete_access_callback', - 'access arguments' => array(2, 3, 4, 5), + 'access callback' => TRUE, 'type' => MENU_CALLBACK, ); diff --git a/core/modules/entity_reference/entity_reference.routing.yml b/core/modules/entity_reference/entity_reference.routing.yml new file mode 100644 index 0000000..185a3c0 --- /dev/null +++ b/core/modules/entity_reference/entity_reference.routing.yml @@ -0,0 +1,7 @@ +entity_reference.autocomplete: + pattern: '/entity_reference/autocomplete/{type}/{field_name}/{entity_type}/{bundle_name}/{entity_id}' + defaults: + _controller: 'entity_reference.controller:handleAutocomplete' + entity_id: 'NULL' + requirements: + _access: 'TRUE' diff --git a/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php new file mode 100644 index 0000000..a5b41af --- /dev/null +++ b/core/modules/entity_reference/lib/Drupal/entity_reference/EntityReferenceAutocomplete.php @@ -0,0 +1,102 @@ +entityManager = $entity_manager; + } + + /** + * Returns matched labels based on a given field, instance and search string. + * + * This function can be used by other modules that wish to pass a mocked + * definition of the field on instance. + * + * @param array $field + * The field array definition. + * @param array $instance + * The instance array definition. + * @param string $entity_type + * The entity type. + * @param string $entity_id + * (optional) The entity ID the entity reference field is attached to. + * Defaults to ''. + * @param string $prefix + * (optional) A prefix for all the keys returned by this function. + * @param string $string + * (optional) The label of the entity to query by. + * + * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException + * Thrown when the current user doesn't have access to the specifies entity. + * + * @return array + * A list of matched entity labels. + * + * @see \Drupal\entity_reference\EntityReferenceController + */ + public function getMatches($field, $instance, $entity_type, $entity_id = '', $prefix = '', $string = '') { + $target_type = $field['settings']['target_type']; + $matches = array(); + $entity = NULL; + + if ($entity_id !== 'NULL') { + $entities = $this->entityManager->getStorageController($entity_type)->load(array($entity_id)); + $entity = reset($entities); + // @todo: Improve when we have entity_access(). + $entity_access = $target_type == 'node' ? node_access('view', $entity) : TRUE; + if (!$entity || !$entity_access) { + throw new AccessDeniedHttpException(); + } + } + $handler = entity_reference_get_selection_handler($field, $instance, $entity); + + if (isset($string)) { + // Get an array of matching entities. + $match_operator = !empty($instance['widget']['settings']['match_operator']) ? $instance['widget']['settings']['match_operator'] : 'CONTAINS'; + $entity_labels = $handler->getReferencableEntities($string, $match_operator, 10); + + // Loop through the entities and convert them into autocomplete output. + foreach ($entity_labels as $values) { + foreach ($values as $entity_id => $label) { + $key = "$label ($entity_id)"; + // Strip things like starting/trailing white spaces, line breaks and + // tags. + $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(decode_entities(strip_tags($key))))); + // Names containing commas or quotes must be wrapped in quotes. + if (strpos($key, ',') !== FALSE || strpos($key, '"') !== FALSE) { + $key = '"' . str_replace('"', '""', $key) . '"'; + } + $matches[$prefix . $key] = '