SuggestionManager.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Drupal\linkit;
  3. use Drupal\Component\Utility\Html;
  4. use Drupal\Core\StringTranslation\StringTranslationTrait;
  5. use Drupal\linkit\Suggestion\DescriptionSuggestion;
  6. use Drupal\linkit\Suggestion\SuggestionCollection;
  7. /**
  8. * Suggestion service to handle autocomplete suggestions.
  9. */
  10. class SuggestionManager {
  11. use StringTranslationTrait;
  12. /**
  13. * Gets the suggestions.
  14. *
  15. * @param ProfileInterface $linkitProfile
  16. * The linkit profile.
  17. * @param string $search_string
  18. * The string ro use in the matchers.
  19. *
  20. * @return \Drupal\linkit\Suggestion\SuggestionCollection
  21. * A suggestion collection.
  22. */
  23. public function getSuggestions(ProfileInterface $linkitProfile, $search_string) {
  24. $suggestions = new SuggestionCollection();
  25. if (empty(trim($search_string))) {
  26. return $suggestions;
  27. }
  28. foreach ($linkitProfile->getMatchers() as $plugin) {
  29. $suggestions->addSuggestions($plugin->execute($search_string));
  30. }
  31. return $suggestions;
  32. }
  33. /**
  34. * Adds an unscathed suggestion to the given suggestion collection.
  35. *
  36. * @param \Drupal\linkit\Suggestion\SuggestionCollection $suggestionCollection
  37. * A suggestion collection to add the unscathed suggestion to.
  38. * @param string $search_string
  39. * The string ro use in the matchers.
  40. *
  41. * @return \Drupal\linkit\Suggestion\SuggestionCollection
  42. * A suggestion collection.
  43. */
  44. public function addUnscathedSuggestion(SuggestionCollection $suggestionCollection, $search_string) {
  45. $suggestion = new DescriptionSuggestion();
  46. $suggestion->setLabel(Html::escape($search_string))
  47. ->setGroup($this->t('No results'))
  48. ->setDescription($this->t('Linkit could not find any suggestions. This URL will be used as is.'))
  49. ->setPath($search_string);
  50. $suggestionCollection->addSuggestion($suggestion);
  51. return $suggestionCollection;
  52. }
  53. }