You are here

public function ContextualController::render in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/contextual/src/ContextualController.php \Drupal\contextual\ContextualController::render()
  2. 10 core/modules/contextual/src/ContextualController.php \Drupal\contextual\ContextualController::render()

Returns the requested rendered contextual links.

Given a list of contextual links IDs, render them. Hence this must be robust to handle arbitrary input.

Parameters

\Symfony\Component\HttpFoundation\Request $request: The Symfony request object.

Return value

\Symfony\Component\HttpFoundation\JsonResponse The JSON response.

Throws

\Symfony\Component\HttpKernel\Exception\BadRequestHttpException Thrown when the request contains no ids.

See also

contextual_preprocess()

1 string reference to 'ContextualController::render'
contextual.routing.yml in core/modules/contextual/contextual.routing.yml
core/modules/contextual/contextual.routing.yml

File

core/modules/contextual/src/ContextualController.php, line 62

Class

ContextualController
Returns responses for Contextual module routes.

Namespace

Drupal\contextual

Code

public function render(Request $request) {
  $ids = $request->request
    ->get('ids');
  if (!isset($ids)) {
    throw new BadRequestHttpException('No contextual ids specified.');
  }
  $tokens = $request->request
    ->get('tokens');
  if (!isset($tokens)) {
    throw new BadRequestHttpException('No contextual ID tokens specified.');
  }
  $rendered = [];
  foreach ($ids as $key => $id) {
    if (!isset($tokens[$key]) || !hash_equals($tokens[$key], Crypt::hmacBase64($id, Settings::getHashSalt() . \Drupal::service('private_key')
      ->get()))) {
      throw new BadRequestHttpException('Invalid contextual ID specified.');
    }
    $element = [
      '#type' => 'contextual_links',
      '#contextual_links' => _contextual_id_to_links($id),
    ];
    $rendered[$id] = $this->renderer
      ->renderRoot($element);
  }
  return new JsonResponse($rendered);
}