Added missing modules, including some as submodules.
[yaffs-website] / web / modules / contrib / linkit / src / Controller / AutocompleteController.php
1 <?php
2
3 /**
4  * @file
5  * Contains \Drupal\linkit\Controller\AutocompleteController.
6  */
7
8 namespace Drupal\linkit\Controller;
9
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;
17
18 class AutocompleteController implements ContainerInjectionInterface {
19
20   /**
21    * The linkit profile storage service.
22    *
23    * @var \Drupal\Core\Entity\EntityStorageInterface
24    */
25   protected $linkitProfileStorage;
26
27   /**
28    * The result manager.
29    *
30    * @var \Drupal\linkit\ResultManager
31    */
32   protected $resultManager;
33
34   /**
35    * The linkit profile.
36    *
37    * @var \Drupal\linkit\ProfileInterface
38    */
39   protected $linkitProfile;
40
41   /**
42    * Constructs a EntityAutocompleteController object.
43    *
44    * @param ResultManager $resultManager
45    *   The result service.
46    * @param \Drupal\Core\Entity\EntityStorageInterface $linkit_profile_storage
47    *   The linkit profile storage service.
48    */
49   public function __construct(EntityStorageInterface $linkit_profile_storage, ResultManager $resultManager) {
50     $this->linkitProfileStorage = $linkit_profile_storage;
51     $this->resultManager = $resultManager;
52   }
53
54   /**
55    * {@inheritdoc}
56    */
57   public static function create(ContainerInterface $container) {
58     return new static(
59       $container->get('entity.manager')->getStorage('linkit_profile'),
60       $container->get('linkit.result_manager')
61     );
62   }
63
64   /**
65    * Menu callback for linkit search autocompletion.
66    *
67    * Like other autocomplete functions, this function inspects the 'q' query
68    * parameter for the string to use to search for suggestions.
69    *
70    * @param Request $request
71    *   The request.
72    * @param $linkit_profile_id
73    *   The linkit profile id.
74    * @return JsonResponse
75    *   A JSON response containing the autocomplete suggestions.
76    */
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'));
80
81     $matches = $this->resultManager->getResults($this->linkitProfile, $string);
82
83     $json_object = new \stdClass();
84     $json_object->matches = $matches;
85
86     return new JsonResponse($json_object);
87   }
88
89 }