class ContextualController

Same name in other branches
  1. 9 core/modules/contextual/src/ContextualController.php \Drupal\contextual\ContextualController
  2. 8.9.x core/modules/contextual/src/ContextualController.php \Drupal\contextual\ContextualController
  3. 10 core/modules/contextual/src/ContextualController.php \Drupal\contextual\ContextualController

Returns responses for Contextual module routes.

Hierarchy

Expanded class hierarchy of ContextualController

File

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

Namespace

Drupal\contextual
View source
class ContextualController implements ContainerInjectionInterface {
    
    /**
     * The renderer.
     *
     * @var \Drupal\Core\Render\RendererInterface
     */
    protected $renderer;
    
    /**
     * Constructors a new ContextualController.
     *
     * @param \Drupal\Core\Render\RendererInterface $renderer
     *   The renderer.
     */
    public function __construct(RendererInterface $renderer) {
        $this->renderer = $renderer;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container) {
        return new static($container->get('renderer'));
    }
    
    /**
     * Returns the requested rendered contextual links.
     *
     * Given a list of contextual links IDs, render them. Hence this must be
     * robust to handle arbitrary input.
     *
     * @param \Symfony\Component\HttpFoundation\Request $request
     *   The Symfony request object.
     *
     * @return \Symfony\Component\HttpFoundation\JsonResponse
     *   The JSON response.
     *
     * @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException
     *   Thrown when the request contains no ids.
     *
     * @internal
     *
     * @see contextual_preprocess()
     */
    public function render(Request $request) {
        if (!$request->request
            ->has('ids')) {
            throw new BadRequestHttpException('No contextual ids specified.');
        }
        $ids = $request->request
            ->all('ids');
        if (!$request->request
            ->has('tokens')) {
            throw new BadRequestHttpException('No contextual ID tokens specified.');
        }
        $tokens = $request->request
            ->all('tokens');
        $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);
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title
ContextualController::$renderer protected property The renderer.
ContextualController::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ContextualController::render public function Returns the requested rendered contextual links.
ContextualController::__construct public function Constructors a new ContextualController.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.