EntityAutocompleteMatcher.php 2.9 KB

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