Mercurial > hg > isophonics-drupal-site
comparison core/lib/Drupal/Core/Entity/EntityAutocompleteMatcher.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\Core\Entity; | |
4 | |
5 use Drupal\Component\Utility\Html; | |
6 use Drupal\Component\Utility\Tags; | |
7 use Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface; | |
8 | |
9 /** | |
10 * Matcher class to get autocompletion results for entity reference. | |
11 */ | |
12 class EntityAutocompleteMatcher { | |
13 | |
14 /** | |
15 * The entity reference selection handler plugin manager. | |
16 * | |
17 * @var \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface | |
18 */ | |
19 protected $selectionManager; | |
20 | |
21 /** | |
22 * Constructs a EntityAutocompleteMatcher object. | |
23 * | |
24 * @param \Drupal\Core\Entity\EntityReferenceSelection\SelectionPluginManagerInterface $selection_manager | |
25 * The entity reference selection handler plugin manager. | |
26 */ | |
27 public function __construct(SelectionPluginManagerInterface $selection_manager) { | |
28 $this->selectionManager = $selection_manager; | |
29 } | |
30 | |
31 /** | |
32 * Gets matched labels based on a given search string. | |
33 * | |
34 * @param string $target_type | |
35 * The ID of the target entity type. | |
36 * @param string $selection_handler | |
37 * The plugin ID of the entity reference selection handler. | |
38 * @param array $selection_settings | |
39 * An array of settings that will be passed to the selection handler. | |
40 * @param string $string | |
41 * (optional) The label of the entity to query by. | |
42 * | |
43 * @return array | |
44 * An array of matched entity labels, in the format required by the AJAX | |
45 * autocomplete API (e.g. array('value' => $value, 'label' => $label)). | |
46 * | |
47 * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException | |
48 * Thrown when the current user doesn't have access to the specified entity. | |
49 * | |
50 * @see \Drupal\system\Controller\EntityAutocompleteController | |
51 */ | |
52 public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') { | |
53 $matches = []; | |
54 | |
55 $options = $selection_settings + [ | |
56 'target_type' => $target_type, | |
57 'handler' => $selection_handler, | |
58 ]; | |
59 $handler = $this->selectionManager->getInstance($options); | |
60 | |
61 if (isset($string)) { | |
62 // Get an array of matching entities. | |
63 $match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS'; | |
64 $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10); | |
65 | |
66 // Loop through the entities and convert them into autocomplete output. | |
67 foreach ($entity_labels as $values) { | |
68 foreach ($values as $entity_id => $label) { | |
69 $key = "$label ($entity_id)"; | |
70 // Strip things like starting/trailing white spaces, line breaks and | |
71 // tags. | |
72 $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key))))); | |
73 // Names containing commas or quotes must be wrapped in quotes. | |
74 $key = Tags::encode($key); | |
75 $matches[] = ['value' => $key, 'label' => $label]; | |
76 } | |
77 } | |
78 } | |
79 | |
80 return $matches; | |
81 } | |
82 | |
83 } |