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