5 * Contains \Drupal\linkit\Controller\AutocompleteController.
8 namespace Drupal\linkit\Controller;
10 use Drupal\Component\Utility\Unicode;
11 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
12 use Drupal\Core\Entity\EntityStorageInterface;
13 use Drupal\linkit\ResultManager;
14 use Symfony\Component\DependencyInjection\ContainerInterface;
15 use Symfony\Component\HttpFoundation\JsonResponse;
16 use Symfony\Component\HttpFoundation\Request;
18 class AutocompleteController implements ContainerInjectionInterface {
21 * The linkit profile storage service.
23 * @var \Drupal\Core\Entity\EntityStorageInterface
25 protected $linkitProfileStorage;
30 * @var \Drupal\linkit\ResultManager
32 protected $resultManager;
37 * @var \Drupal\linkit\ProfileInterface
39 protected $linkitProfile;
42 * Constructs a EntityAutocompleteController object.
44 * @param ResultManager $resultManager
46 * @param \Drupal\Core\Entity\EntityStorageInterface $linkit_profile_storage
47 * The linkit profile storage service.
49 public function __construct(EntityStorageInterface $linkit_profile_storage, ResultManager $resultManager) {
50 $this->linkitProfileStorage = $linkit_profile_storage;
51 $this->resultManager = $resultManager;
57 public static function create(ContainerInterface $container) {
59 $container->get('entity.manager')->getStorage('linkit_profile'),
60 $container->get('linkit.result_manager')
65 * Menu callback for linkit search autocompletion.
67 * Like other autocomplete functions, this function inspects the 'q' query
68 * parameter for the string to use to search for suggestions.
70 * @param Request $request
72 * @param $linkit_profile_id
73 * The linkit profile id.
74 * @return JsonResponse
75 * A JSON response containing the autocomplete suggestions.
77 public function autocomplete(Request $request, $linkit_profile_id) {
78 $this->linkitProfile = $this->linkitProfileStorage->load($linkit_profile_id);
79 $string = Unicode::strtolower($request->query->get('q'));
81 $matches = $this->resultManager->getResults($this->linkitProfile, $string);
83 $json_object = new \stdClass();
84 $json_object->matches = $matches;
86 return new JsonResponse($json_object);