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