Chris@0: selectionManager = $selection_manager; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets matched labels based on a given search string. Chris@0: * Chris@0: * @param string $target_type Chris@0: * The ID of the target entity type. Chris@0: * @param string $selection_handler Chris@0: * The plugin ID of the entity reference selection handler. Chris@0: * @param array $selection_settings Chris@0: * An array of settings that will be passed to the selection handler. Chris@0: * @param string $string Chris@0: * (optional) The label of the entity to query by. Chris@0: * Chris@0: * @return array Chris@0: * An array of matched entity labels, in the format required by the AJAX Chris@0: * autocomplete API (e.g. array('value' => $value, 'label' => $label)). Chris@0: * Chris@0: * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException Chris@0: * Thrown when the current user doesn't have access to the specified entity. Chris@0: * Chris@0: * @see \Drupal\system\Controller\EntityAutocompleteController Chris@0: */ Chris@0: public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') { Chris@0: $matches = []; Chris@0: Chris@0: $options = $selection_settings + [ Chris@0: 'target_type' => $target_type, Chris@0: 'handler' => $selection_handler, Chris@0: ]; Chris@0: $handler = $this->selectionManager->getInstance($options); Chris@0: Chris@0: if (isset($string)) { Chris@0: // Get an array of matching entities. Chris@0: $match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS'; Chris@0: $entity_labels = $handler->getReferenceableEntities($string, $match_operator, 10); Chris@0: Chris@0: // Loop through the entities and convert them into autocomplete output. Chris@0: foreach ($entity_labels as $values) { Chris@0: foreach ($values as $entity_id => $label) { Chris@0: $key = "$label ($entity_id)"; Chris@0: // Strip things like starting/trailing white spaces, line breaks and Chris@0: // tags. Chris@0: $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key))))); Chris@0: // Names containing commas or quotes must be wrapped in quotes. Chris@0: $key = Tags::encode($key); Chris@0: $matches[] = ['value' => $key, 'label' => $label]; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $matches; Chris@0: } Chris@0: Chris@0: }