Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. InlineFragmentRenderer.php

function InlineFragmentRenderer::render

Additional available options:

  • alt: an alternative URI to render in case of an error

Overrides FragmentRendererInterface::render

File

vendor/symfony/http-kernel/Fragment/InlineFragmentRenderer.php, line 41

Class

InlineFragmentRenderer
Implements the inline rendering strategy where the Request is rendered by the current HTTP kernel.

Namespace

Symfony\Component\HttpKernel\Fragment

Code

public function render(string|ControllerReference $uri, Request $request, array $options = []) : Response {
    $reference = null;
    if ($uri instanceof ControllerReference) {
        $reference = $uri;
        // Remove attributes from the generated URI because if not, the Symfony
        // routing system will use them to populate the Request attributes. We don't
        // want that as we want to preserve objects (so we manually set Request attributes
        // below instead)
        $attributes = $reference->attributes;
        $reference->attributes = [];
        // The request format and locale might have been overridden by the user
        foreach ([
            '_format',
            '_locale',
        ] as $key) {
            if (isset($attributes[$key])) {
                $reference->attributes[$key] = $attributes[$key];
            }
        }
        $uri = $this->generateFragmentUri($uri, $request, false, false);
        $reference->attributes = array_merge($attributes, $reference->attributes);
    }
    $subRequest = $this->createSubRequest($uri, $request);
    // override Request attributes as they can be objects (which are not supported by the generated URI)
    if (null !== $reference) {
        $subRequest->attributes
            ->add($reference->attributes);
    }
    $level = ob_get_level();
    try {
        return SubRequestHandler::handle($this->kernel, $subRequest, HttpKernelInterface::SUB_REQUEST, false);
    } catch (\Exception $e) {
        // we dispatch the exception event to trigger the logging
        // the response that comes back is ignored
        if (isset($options['ignore_errors']) && $options['ignore_errors'] && $this->dispatcher) {
            $event = new ExceptionEvent($this->kernel, $request, HttpKernelInterface::SUB_REQUEST, $e);
            $this->dispatcher
                ->dispatch($event, KernelEvents::EXCEPTION);
        }
        // let's clean up the output buffers that were created by the sub-request
        Response::closeOutputBuffers($level, false);
        if (isset($options['alt'])) {
            $alt = $options['alt'];
            unset($options['alt']);
            return $this->render($alt, $request, $options);
        }
        if (!isset($options['ignore_errors']) || !$options['ignore_errors']) {
            throw $e;
        }
        return new Response();
    }
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal