LinkGeneratorTrait.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Drupal\Core\Routing;
  3. use Drupal\Core\Url;
  4. use Drupal\Core\Utility\LinkGeneratorInterface;
  5. /**
  6. * Wrapper methods for the Link Generator.
  7. *
  8. * This utility trait should only be used in application-level code, such as
  9. * classes that would implement ContainerInjectionInterface. Services registered
  10. * in the Container should not use this trait but inject the appropriate service
  11. * directly for easier testing.
  12. *
  13. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0.
  14. * Use \Drupal\Core\Link instead.
  15. *
  16. * @see https://www.drupal.org/node/2614344
  17. */
  18. trait LinkGeneratorTrait {
  19. /**
  20. * The link generator.
  21. *
  22. * @var \Drupal\Core\Utility\LinkGeneratorInterface
  23. */
  24. protected $linkGenerator;
  25. /**
  26. * Renders a link to a route given a route name and its parameters.
  27. *
  28. * For details on the arguments, usage, and possible exceptions see
  29. * \Drupal\Core\Utility\LinkGeneratorInterface::generate().
  30. *
  31. * @return \Drupal\Core\GeneratedLink
  32. * A GeneratedLink object containing a link to the given route and
  33. * parameters and bubbleable metadata.
  34. *
  35. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use
  36. * \Drupal\Core\Link::fromTextAndUrl() instead.
  37. *
  38. * @see https://www.drupal.org/node/2614344
  39. * @see \Drupal\Core\Utility\LinkGeneratorInterface::generate()
  40. */
  41. protected function l($text, Url $url) {
  42. @trigger_error(__NAMESPACE__ . "\LinkGeneratorTrait::l() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Use \Drupal\Core\Link::fromTextAndUrl() instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
  43. return $this->getLinkGenerator()->generate($text, $url);
  44. }
  45. /**
  46. * Returns the link generator.
  47. *
  48. * @return \Drupal\Core\Utility\LinkGeneratorInterface
  49. * The link generator
  50. *
  51. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Inject the
  52. * 'link_generator' service or use \Drupal\Core\Link instead
  53. *
  54. * @see https://www.drupal.org/node/2614344
  55. */
  56. protected function getLinkGenerator() {
  57. @trigger_error(__NAMESPACE__ . "\LinkGeneratorTrait::getLinkGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Inject the 'link_generator' service or use \Drupal\Core\Link instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
  58. if (!isset($this->linkGenerator)) {
  59. $this->linkGenerator = \Drupal::service('link_generator');
  60. }
  61. return $this->linkGenerator;
  62. }
  63. /**
  64. * Sets the link generator service.
  65. *
  66. * @param \Drupal\Core\Utility\LinkGeneratorInterface $generator
  67. * The link generator service.
  68. *
  69. * @return $this
  70. *
  71. * @deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Inject the
  72. * 'link_generator' service or use \Drupal\Core\Link instead
  73. *
  74. * @see https://www.drupal.org/node/2614344
  75. */
  76. public function setLinkGenerator(LinkGeneratorInterface $generator) {
  77. @trigger_error(__NAMESPACE__ . "\LinkGeneratorTrait::setLinkGenerator() is deprecated in drupal:8.0.0 and is removed from drupal:9.0.0. Inject the 'link_generator' service or use \Drupal\Core\Link instead. See https://www.drupal.org/node/2614344", E_USER_DEPRECATED);
  78. $this->linkGenerator = $generator;
  79. return $this;
  80. }
  81. }