EntityAutocompleteMatcher.php 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. namespace Drupal\materio_commerce;
  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 extends \Drupal\Core\Entity\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 an 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. public function getMatches($target_type, $selection_handler, $selection_settings, $string = '') {
  29. $matches = [];
  30. $options = $selection_settings + [
  31. 'target_type' => $target_type,
  32. 'handler' => $selection_handler,
  33. ];
  34. $handler = $this->selectionManager->getInstance($options);
  35. if (isset($string)) {
  36. // Get an array of matching entities.
  37. $match_operator = !empty($selection_settings['match_operator']) ? $selection_settings['match_operator'] : 'CONTAINS';
  38. $match_limit = isset($selection_settings['match_limit']) ? (int) $selection_settings['match_limit'] : 10;
  39. $entity_labels = $handler->getReferenceableEntities($string, $match_operator, $match_limit);
  40. // Loop through the entities and convert them into autocomplete output.
  41. foreach ($entity_labels as $values) {
  42. foreach ($values as $entity_id => $label) {
  43. $entity = \Drupal::entityTypeManager()->getStorage($target_type)->load($entity_id);
  44. $entity = \Drupal::service('entity.repository')->getTranslationFromContext($entity);
  45. $key = $label . ' (' . $entity_id . ')';
  46. // Strip things like starting/trailing white spaces, line breaks and tags.
  47. $key = preg_replace('/\s\s+/', ' ', str_replace("\n", '', trim(Html::decodeEntities(strip_tags($key)))));
  48. // Names containing commas or quotes must be wrapped in quotes.
  49. $key = Tags::encode($key);
  50. switch ($entity->getEntityType()->id()) {
  51. case 'node':
  52. $type = !empty($entity->type->entity) ? $entity->type->entity->label() : $entity->bundle();
  53. $status = ($entity->isPublished()) ? ", Published" : ", Unpublished";
  54. break;
  55. case 'user':
  56. $email = $entity->getEmail();
  57. $label = $label . ' (' . $entity_id . ') [' . $email . ']';
  58. break;
  59. }
  60. $matches[] = ['value' => $key, 'label' => $label];
  61. }
  62. }
  63. }
  64. return $matches;
  65. }
  66. }