Same name and namespace in other branches
  1. 8.9.x core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php \Drupal\Core\Render\MetadataBubblingUrlGenerator
  2. 9 core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php \Drupal\Core\Render\MetadataBubblingUrlGenerator

Decorator for the URL generator, which bubbles bubbleable URL metadata.

Implements a decorator for the URL generator that allows to automatically collect and bubble up bubbleable metadata associated with URLs due to outbound path and route processing. This approach helps keeping the render and the routing subsystems decoupled.

Hierarchy

Expanded class hierarchy of MetadataBubblingUrlGenerator

See also

\Drupal\Core\RouteProcessor\OutboundRouteProcessorInterface

\Drupal\Core\PathProcessor\OutboundPathProcessorInterface

\Drupal\Core\Render\BubbleableMetadata

1 file declares its use of MetadataBubblingUrlGenerator
MetadataBubblingUrlGeneratorTest.php in core/tests/Drupal/Tests/Core/Render/MetadataBubblingUrlGeneratorTest.php
1 string reference to 'MetadataBubblingUrlGenerator'
core.services.yml in core/core.services.yml
core/core.services.yml
1 service uses MetadataBubblingUrlGenerator
url_generator in core/core.services.yml
Drupal\Core\Render\MetadataBubblingUrlGenerator

File

core/lib/Drupal/Core/Render/MetadataBubblingUrlGenerator.php, line 21

Namespace

Drupal\Core\Render
View source
class MetadataBubblingUrlGenerator implements UrlGeneratorInterface {

  /**
   * The non-bubbling URL generator.
   *
   * @var \Drupal\Core\Routing\UrlGeneratorInterface
   */
  protected $urlGenerator;

  /**
   * The renderer.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Constructs a new bubbling URL generator service.
   *
   * @param \Drupal\Core\Routing\UrlGeneratorInterface $url_generator
   *   The non-bubbling URL generator.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   The renderer.
   */
  public function __construct(UrlGeneratorInterface $url_generator, RendererInterface $renderer) {
    $this->urlGenerator = $url_generator;
    $this->renderer = $renderer;
  }

  /**
   * {@inheritdoc}
   *
   * phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn
   * @return void
   */
  public function setContext(SymfonyRequestContext $context) {
    $this->urlGenerator
      ->setContext($context);
  }

  /**
   * {@inheritdoc}
   */
  public function getContext() : SymfonyRequestContext {
    return $this->urlGenerator
      ->getContext();
  }

  /**
   * {@inheritdoc}
   */
  public function getPathFromRoute($name, $parameters = []) {
    return $this->urlGenerator
      ->getPathFromRoute($name, $parameters);
  }

  /**
   * Bubbles the bubbleable metadata to the current render context.
   *
   * @param \Drupal\Core\GeneratedUrl $generated_url
   *   The generated URL whose bubbleable metadata to bubble.
   * @param array $options
   *   (optional) The URL options. Defaults to none.
   */
  protected function bubble(GeneratedUrl $generated_url, array $options = []) {

    // Bubbling metadata makes sense only if the code is executed inside a
    // render context. All code running outside controllers has no render
    // context by default, so URLs used there are not supposed to affect the
    // response cacheability.
    if ($this->renderer
      ->hasRenderContext()) {
      $build = [];
      $generated_url
        ->applyTo($build);
      $this->renderer
        ->render($build);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function generate($name, $parameters = [], $referenceType = self::ABSOLUTE_PATH) : string {
    $options['absolute'] = is_bool($referenceType) ? $referenceType : $referenceType === self::ABSOLUTE_URL;
    $generated_url = $this
      ->generateFromRoute($name, $parameters, $options, TRUE);
    $this
      ->bubble($generated_url);
    return $generated_url
      ->getGeneratedUrl();
  }

  /**
   * {@inheritdoc}
   */
  public function generateFromRoute($name, $parameters = [], $options = [], $collect_bubbleable_metadata = FALSE) {
    $generated_url = $this->urlGenerator
      ->generateFromRoute($name, $parameters, $options, TRUE);
    if (!$collect_bubbleable_metadata) {
      $this
        ->bubble($generated_url, $options);
    }
    return $collect_bubbleable_metadata ? $generated_url : $generated_url
      ->getGeneratedUrl();
  }

  /**
   * Checks if route name is a string or route object.
   *
   * @param string|\Symfony\Component\Routing\Route $name
   *   The route "name" which may also be an object or anything.
   *
   * @return bool
   *   TRUE if the passed in value a valid route, FALSE otherwise.
   *
   * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Only string
   *   route names are supported.
   *
   * @see https://www.drupal.org/node/3172303
   */
  public function supports($name) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Only string route names are supported. See https://www.drupal.org/node/3172303', E_USER_DEPRECATED);
    return $this->urlGenerator
      ->supports($name);
  }

  /**
   * Gets either the route name or a string based on the route object.
   *
   * @param string|\Symfony\Component\Routing\Route $name
   *   The route "name" which may also be an object or anything.
   * @param array $parameters
   *   Route parameters array.
   *
   * @return string
   *   Either the route name, or a string that uniquely identifies the route.
   *
   * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use
   *   the route name instead.
   *
   * @see https://www.drupal.org/node/3172303
   */
  public function getRouteDebugMessage($name, array $parameters = []) {
    @trigger_error(__METHOD__ . '() is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. Use the route name instead. See https://www.drupal.org/node/3172303', E_USER_DEPRECATED);
    return $this->urlGenerator
      ->getRouteDebugMessage($name, $parameters);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MetadataBubblingUrlGenerator::$renderer protected property The renderer.
MetadataBubblingUrlGenerator::$urlGenerator protected property The non-bubbling URL generator.
MetadataBubblingUrlGenerator::bubble protected function Bubbles the bubbleable metadata to the current render context.
MetadataBubblingUrlGenerator::generate public function
MetadataBubblingUrlGenerator::generateFromRoute public function
MetadataBubblingUrlGenerator::getContext public function
MetadataBubblingUrlGenerator::getPathFromRoute public function
MetadataBubblingUrlGenerator::getRouteDebugMessage Deprecated public function Gets either the route name or a string based on the route object.
MetadataBubblingUrlGenerator::setContext public function phpcs:ignore Drupal.Commenting.FunctionComment.VoidReturn
MetadataBubblingUrlGenerator::supports Deprecated public function Checks if route name is a string or route object.
MetadataBubblingUrlGenerator::__construct public function Constructs a new bubbling URL generator service.