ResultManager.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @file
  4. * Contains \Drupal\linkit\ResultManager.
  5. */
  6. namespace Drupal\linkit;
  7. use Drupal\Component\Utility\Html;
  8. use Drupal\Core\Url;
  9. /**
  10. * Result service to handle autocomplete matcher results.
  11. */
  12. class ResultManager {
  13. /**
  14. * Gets the results.
  15. *
  16. * @param ProfileInterface $linkitProfile
  17. * The linkit profile.
  18. * @param $search_string
  19. * The string ro use in the matchers.
  20. *
  21. * @return array
  22. * An array of matches.
  23. */
  24. public function getResults(ProfileInterface $linkitProfile, $search_string) {
  25. $matches = array();
  26. if (empty(trim($search_string))) {
  27. return [[
  28. 'title' => t('No results'),
  29. ]];
  30. }
  31. // Special for link to front page.
  32. if (strpos($search_string, 'front') !== FALSE) {
  33. $matches[] = [
  34. 'title' => t('Front page'),
  35. 'description' => 'The front page for this site.',
  36. 'path' => Url::fromRoute('<front>')->toString(),
  37. 'group' => t('System'),
  38. ];
  39. }
  40. foreach ($linkitProfile->getMatchers() as $plugin) {
  41. $matches = array_merge($matches, $plugin->getMatches($search_string));
  42. }
  43. // Check for an e-mail address then return an e-mail match and create a
  44. // mail-to link if appropriate.
  45. if (filter_var($search_string, FILTER_VALIDATE_EMAIL)) {
  46. $matches[] = [
  47. 'title' => t('E-mail @email', ['@email' => $search_string]),
  48. 'description' => t('Opens your mail client ready to e-mail @email', ['@email' => $search_string]),
  49. 'path' => 'mailto:' . Html::escape($search_string),
  50. 'group' => t('E-mail'),
  51. ];
  52. }
  53. // If there is still no matches, return a "no results" array.
  54. if (empty($matches)) {
  55. return [[
  56. 'title' => t('No results'),
  57. ]];
  58. }
  59. return $matches;
  60. }
  61. }