ContextualController.php 1.3 KB

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