Newer
Older
Angie Byron
committed
<?php
/**
* @file
* Contains \Drupal\entity_reference/EntityReferenceAutocomplete.
*/
Angie Byron
committed
namespace Drupal\entity_reference;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\entity_reference\Plugin\Type\SelectionPluginManager;
Angie Byron
committed
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
/**
* Helper class to get autocompletion results for entity reference.
*/
class EntityReferenceAutocomplete {
/**
* The entity manager.
*
Angie Byron
committed
* @var \Drupal\Core\Entity\EntityManagerInterface
Angie Byron
committed
*/
protected $entityManager;
/**
* The Entity reference selection handler plugin manager.
*
* @var \Drupal\entity_reference\Plugin\Type\SelectionPluginManager
*/
protected $selectionHandlerManager;
Angie Byron
committed
/**
* Constructs a EntityReferenceAutocomplete object.
*
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Angie Byron
committed
* The entity manager.
* @param \Drupal\entity_reference\Plugin\Type\SelectionPluginManager $selection_manager
* The Entity reference selection handler plugin manager.
Angie Byron
committed
*/
Angie Byron
committed
public function __construct(EntityManagerInterface $entity_manager, SelectionPluginManager $selection_manager) {
Angie Byron
committed
$this->entityManager = $entity_manager;
$this->selectionHandlerManager = $selection_manager;
Angie Byron
committed
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
}
/**
* 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 = '') {
$matches = array();
$entity = NULL;
if ($entity_id !== 'NULL') {
Dries Buytaert
committed
$entity = $this->entityManager->getStorageController($entity_type)->load($entity_id);
Angie Byron
committed
if (!$entity || !$entity->access('view')) {
Angie Byron
committed
throw new AccessDeniedHttpException();
}
}
$handler = $this->selectionHandlerManager->getSelectionHandler($instance, $entity);
Angie Byron
committed
if (isset($string)) {
// Get an array of matching entities.
Alex Pott
committed
$widget = entity_get_form_display($instance->entity_type, $instance->bundle, 'default')->getComponent($instance->getFieldName());
Alex Pott
committed
$match_operator = !empty($widget['settings']['match_operator']) ? $widget['settings']['match_operator'] : 'CONTAINS';
$entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10);
Angie Byron
committed
// 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] = $label;
Angie Byron
committed
}
}
}
return $matches;
}
}