Link.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. <?php
  2. namespace Drupal\Core;
  3. use Drupal\Core\Render\RenderableInterface;
  4. use Drupal\Core\Routing\LinkGeneratorTrait;
  5. /**
  6. * Defines an object that holds information about a link.
  7. */
  8. class Link implements RenderableInterface {
  9. /**
  10. * @deprecated in Drupal 8.0.x-dev, will be removed before Drupal 9.0.0.
  11. */
  12. use LinkGeneratorTrait;
  13. /**
  14. * The text of the link.
  15. *
  16. * @var string
  17. */
  18. protected $text;
  19. /**
  20. * The URL of the link.
  21. *
  22. * @var \Drupal\Core\Url
  23. */
  24. protected $url;
  25. /**
  26. * Constructs a new Link object.
  27. *
  28. * @param string $text
  29. * The text of the link.
  30. * @param \Drupal\Core\Url $url
  31. * The url object.
  32. */
  33. public function __construct($text, Url $url) {
  34. $this->text = $text;
  35. $this->url = $url;
  36. }
  37. /**
  38. * Creates a Link object from a given route name and parameters.
  39. *
  40. * @param string $text
  41. * The text of the link.
  42. * @param string $route_name
  43. * The name of the route
  44. * @param array $route_parameters
  45. * (optional) An associative array of parameter names and values.
  46. * @param array $options
  47. * The options parameter takes exactly the same structure.
  48. * See \Drupal\Core\Url::fromUri() for details.
  49. *
  50. * @return static
  51. */
  52. public static function createFromRoute($text, $route_name, $route_parameters = [], $options = []) {
  53. return new static($text, new Url($route_name, $route_parameters, $options));
  54. }
  55. /**
  56. * Creates a Link object from a given Url object.
  57. *
  58. * @param string $text
  59. * The text of the link.
  60. * @param \Drupal\Core\Url $url
  61. * The Url to create the link for.
  62. *
  63. * @return static
  64. */
  65. public static function fromTextAndUrl($text, Url $url) {
  66. return new static($text, $url);
  67. }
  68. /**
  69. * Returns the text of the link.
  70. *
  71. * @return string
  72. */
  73. public function getText() {
  74. return $this->text;
  75. }
  76. /**
  77. * Sets the new text of the link.
  78. *
  79. * @param string $text
  80. * The new text.
  81. *
  82. * @return $this
  83. */
  84. public function setText($text) {
  85. $this->text = $text;
  86. return $this;
  87. }
  88. /**
  89. * Returns the URL of the link.
  90. *
  91. * @return \Drupal\Core\Url
  92. */
  93. public function getUrl() {
  94. return $this->url;
  95. }
  96. /**
  97. * Sets the URL of this link.
  98. *
  99. * @param Url $url
  100. * The URL object to set
  101. *
  102. * @return $this
  103. */
  104. public function setUrl(Url $url) {
  105. $this->url = $url;
  106. return $this;
  107. }
  108. /**
  109. * Generates the HTML for this Link object.
  110. *
  111. * Do not use this method to render a link in an HTML context. In an HTML
  112. * context, self::toRenderable() should be used so that render cache
  113. * information is maintained. However, there might be use cases such as tests
  114. * and non-HTML contexts where calling this method directly makes sense.
  115. *
  116. * @return \Drupal\Core\GeneratedLink
  117. * The link HTML markup.
  118. *
  119. * @see \Drupal\Core\Link::toRenderable()
  120. */
  121. public function toString() {
  122. return $this->getLinkGenerator()->generateFromLink($this);
  123. }
  124. /**
  125. * {@inheritdoc}
  126. */
  127. public function toRenderable() {
  128. return [
  129. '#type' => 'link',
  130. '#url' => $this->url,
  131. '#title' => $this->text,
  132. ];
  133. }
  134. }