b1fe245c040eb012b21c90d7d1ca309597f26646
[yaffs-website] / web / core / modules / contextual / src / ContextualController.php
1 <?php
2
3 namespace Drupal\contextual;
4
5 use Symfony\Component\DependencyInjection\ContainerAwareInterface;
6 use Symfony\Component\DependencyInjection\ContainerAwareTrait;
7 use Symfony\Component\HttpFoundation\JsonResponse;
8 use Symfony\Component\HttpFoundation\Request;
9 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
10
11 /**
12  * Returns responses for Contextual module routes.
13  */
14 class ContextualController implements ContainerAwareInterface {
15
16   use ContainerAwareTrait;
17
18   /**
19    * Returns the requested rendered contextual links.
20    *
21    * Given a list of contextual links IDs, render them. Hence this must be
22    * robust to handle arbitrary input.
23    *
24    * @see contextual_preprocess()
25    *
26    * @return \Symfony\Component\HttpFoundation\JsonResponse
27    *   The JSON response.
28    */
29   public function render(Request $request) {
30     $ids = $request->request->get('ids');
31     if (!isset($ids)) {
32       throw new BadRequestHttpException(t('No contextual ids specified.'));
33     }
34
35     $rendered = [];
36     foreach ($ids as $id) {
37       $element = [
38         '#type' => 'contextual_links',
39         '#contextual_links' => _contextual_id_to_links($id),
40       ];
41       $rendered[$id] = $this->container->get('renderer')->renderRoot($element);
42     }
43
44     return new JsonResponse($rendered);
45   }
46
47 }