This repository has been archived on 2025-01-19. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
drupalcampbristol/web/core/modules/contextual/src/ContextualController.php

48 lines
1.3 KiB
PHP
Raw Normal View History

<?php
namespace Drupal\contextual;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
/**
* Returns responses for Contextual module routes.
*/
class ContextualController implements ContainerAwareInterface {
use ContainerAwareTrait;
/**
* Returns the requested rendered contextual links.
*
* Given a list of contextual links IDs, render them. Hence this must be
* robust to handle arbitrary input.
*
* @see contextual_preprocess()
*
* @return \Symfony\Component\HttpFoundation\JsonResponse
* The JSON response.
*/
public function render(Request $request) {
$ids = $request->request->get('ids');
if (!isset($ids)) {
throw new BadRequestHttpException(t('No contextual ids specified.'));
}
2017-04-13 15:53:35 +01:00
$rendered = [];
foreach ($ids as $id) {
2017-04-13 15:53:35 +01:00
$element = [
'#type' => 'contextual_links',
'#contextual_links' => _contextual_id_to_links($id),
2017-04-13 15:53:35 +01:00
];
$rendered[$id] = $this->container->get('renderer')->renderRoot($element);
}
return new JsonResponse($rendered);
}
}