Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\contextual;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
|
Chris@0
|
6 use Drupal\Core\Render\RendererInterface;
|
Chris@0
|
7 use Symfony\Component\DependencyInjection\ContainerInterface;
|
Chris@0
|
8 use Symfony\Component\HttpFoundation\JsonResponse;
|
Chris@0
|
9 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
10 use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Returns responses for Contextual module routes.
|
Chris@0
|
14 */
|
Chris@0
|
15 class ContextualController implements ContainerInjectionInterface {
|
Chris@0
|
16
|
Chris@0
|
17 /**
|
Chris@0
|
18 * The renderer.
|
Chris@0
|
19 * @var \Drupal\Core\Render\RendererInterface
|
Chris@0
|
20 */
|
Chris@0
|
21 protected $render;
|
Chris@0
|
22
|
Chris@0
|
23 /**
|
Chris@0
|
24 * Constructors a new ContextualController
|
Chris@0
|
25 *
|
Chris@0
|
26 * @param \Drupal\Core\Render\RendererInterface $renderer
|
Chris@0
|
27 * The renderer.
|
Chris@0
|
28 */
|
Chris@0
|
29 public function __construct(RendererInterface $renderer) {
|
Chris@0
|
30 $this->renderer = $renderer;
|
Chris@0
|
31 }
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * {@inheritdoc}
|
Chris@0
|
35 */
|
Chris@0
|
36 public static function create(ContainerInterface $container) {
|
Chris@0
|
37 return new static(
|
Chris@0
|
38 $container->get('renderer')
|
Chris@0
|
39 );
|
Chris@0
|
40 }
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * Returns the requested rendered contextual links.
|
Chris@0
|
44 *
|
Chris@0
|
45 * Given a list of contextual links IDs, render them. Hence this must be
|
Chris@0
|
46 * robust to handle arbitrary input.
|
Chris@0
|
47 *
|
Chris@0
|
48 * @see contextual_preprocess()
|
Chris@0
|
49 *
|
Chris@0
|
50 * @return \Symfony\Component\HttpFoundation\JsonResponse
|
Chris@0
|
51 * The JSON response.
|
Chris@0
|
52 */
|
Chris@0
|
53 public function render(Request $request) {
|
Chris@0
|
54 $ids = $request->request->get('ids');
|
Chris@0
|
55 if (!isset($ids)) {
|
Chris@0
|
56 throw new BadRequestHttpException(t('No contextual ids specified.'));
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 $rendered = [];
|
Chris@0
|
60 foreach ($ids as $id) {
|
Chris@0
|
61 $element = [
|
Chris@0
|
62 '#type' => 'contextual_links',
|
Chris@0
|
63 '#contextual_links' => _contextual_id_to_links($id),
|
Chris@0
|
64 ];
|
Chris@0
|
65 $rendered[$id] = $this->renderer->renderRoot($element);
|
Chris@0
|
66 }
|
Chris@0
|
67
|
Chris@0
|
68 return new JsonResponse($rendered);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 }
|