You are here

trait LinkGeneratorTrait in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Routing/LinkGeneratorTrait.php \Drupal\Core\Routing\LinkGeneratorTrait

Wrapper methods for the Link Generator.

This utility trait should only be used in application-level code, such as classes that would implement ContainerInjectionInterface. Services registered in the Container should not use this trait but inject the appropriate service directly for easier testing.

Hierarchy

4 files declare their use of LinkGeneratorTrait
ControllerBase.php in core/lib/Drupal/Core/Controller/ControllerBase.php
Contains \Drupal\Core\Controller\ControllerBase.
FormBase.php in core/lib/Drupal/Core/Form/FormBase.php
Contains \Drupal\Core\Form\FormBase.
FormErrorHandler.php in core/modules/inline_form_errors/src/FormErrorHandler.php
Contains \Drupal\inline_form_errors\FormErrorHandler.
Link.php in core/lib/Drupal/Core/Link.php
Contains \Drupal\Core\Link.

File

core/lib/Drupal/Core/Routing/LinkGeneratorTrait.php, line 22
Contains \Drupal\Core\Routing\LinkGeneratorTrait.

Namespace

Drupal\Core\Routing
View source
trait LinkGeneratorTrait {

  /**
   * The link generator.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $linkGenerator;

  /**
   * Renders a link to a route given a route name and its parameters.
   *
   * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate() for details
   *   on the arguments, usage, and possible exceptions.
   *
   * @return \Drupal\Core\GeneratedLink
   *   A GeneratedLink object containing a link to the given route and
   *   parameters and bubbleable metadata.
   */
  protected function l($text, Url $url) {
    return $this
      ->getLinkGenerator()
      ->generate($text, $url);
  }

  /**
   * Returns the link generator.
   *
   * @return \Drupal\Core\Utility\LinkGeneratorInterface
   *   The link generator
   */
  protected function getLinkGenerator() {
    if (!isset($this->linkGenerator)) {
      $this->linkGenerator = \Drupal::service('link_generator');
    }
    return $this->linkGenerator;
  }

  /**
   * Sets the link generator service.
   *
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $generator
   *   The link generator service.
   *
   * @return $this
   */
  public function setLinkGenerator(LinkGeneratorInterface $generator) {
    $this->linkGenerator = $generator;
    return $this;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator protected function Returns the link generator.
LinkGeneratorTrait::l protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator public function Sets the link generator service.