PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/core/lib/Drupal/Core/Link.php

http://github.com/drupal/drupal
PHP | 179 lines | 53 code | 19 blank | 107 comment | 1 complexity | 6161f1339eb50b03cd80ac7c8d4abb54 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1
  1. <?php
  2. namespace Drupal\Core;
  3. use Drupal\Core\Render\RenderableInterface;
  4. use Drupal\Core\Utility\LinkGeneratorInterface;
  5. /**
  6. * Defines an object that holds information about a link.
  7. */
  8. class Link implements RenderableInterface {
  9. /**
  10. * The link generator.
  11. *
  12. * @var \Drupal\Core\Utility\LinkGeneratorInterface
  13. */
  14. protected $linkGenerator;
  15. /**
  16. * The text of the link.
  17. *
  18. * @var string
  19. */
  20. protected $text;
  21. /**
  22. * The URL of the link.
  23. *
  24. * @var \Drupal\Core\Url
  25. */
  26. protected $url;
  27. /**
  28. * Constructs a new Link object.
  29. *
  30. * @param string $text
  31. * The text of the link.
  32. * @param \Drupal\Core\Url $url
  33. * The url object.
  34. */
  35. public function __construct($text, Url $url) {
  36. $this->text = $text;
  37. $this->url = $url;
  38. }
  39. /**
  40. * Creates a Link object from a given route name and parameters.
  41. *
  42. * @param string $text
  43. * The text of the link.
  44. * @param string $route_name
  45. * The name of the route
  46. * @param array $route_parameters
  47. * (optional) An associative array of parameter names and values.
  48. * @param array $options
  49. * The options parameter takes exactly the same structure.
  50. * See \Drupal\Core\Url::fromUri() for details.
  51. *
  52. * @return static
  53. */
  54. public static function createFromRoute($text, $route_name, $route_parameters = [], $options = []) {
  55. return new static($text, new Url($route_name, $route_parameters, $options));
  56. }
  57. /**
  58. * Creates a Link object from a given Url object.
  59. *
  60. * @param string $text
  61. * The text of the link.
  62. * @param \Drupal\Core\Url $url
  63. * The Url to create the link for.
  64. *
  65. * @return static
  66. */
  67. public static function fromTextAndUrl($text, Url $url) {
  68. return new static($text, $url);
  69. }
  70. /**
  71. * Returns the text of the link.
  72. *
  73. * @return string
  74. */
  75. public function getText() {
  76. return $this->text;
  77. }
  78. /**
  79. * Sets the new text of the link.
  80. *
  81. * @param string $text
  82. * The new text.
  83. *
  84. * @return $this
  85. */
  86. public function setText($text) {
  87. $this->text = $text;
  88. return $this;
  89. }
  90. /**
  91. * Returns the URL of the link.
  92. *
  93. * @return \Drupal\Core\Url
  94. */
  95. public function getUrl() {
  96. return $this->url;
  97. }
  98. /**
  99. * Sets the URL of this link.
  100. *
  101. * @param Url $url
  102. * The URL object to set
  103. *
  104. * @return $this
  105. */
  106. public function setUrl(Url $url) {
  107. $this->url = $url;
  108. return $this;
  109. }
  110. /**
  111. * Generates the HTML for this Link object.
  112. *
  113. * Do not use this method to render a link in an HTML context. In an HTML
  114. * context, self::toRenderable() should be used so that render cache
  115. * information is maintained. However, there might be use cases such as tests
  116. * and non-HTML contexts where calling this method directly makes sense.
  117. *
  118. * @return \Drupal\Core\GeneratedLink
  119. * The link HTML markup.
  120. *
  121. * @see \Drupal\Core\Link::toRenderable()
  122. */
  123. public function toString() {
  124. return $this->getLinkGenerator()->generateFromLink($this);
  125. }
  126. /**
  127. * {@inheritdoc}
  128. */
  129. public function toRenderable() {
  130. return [
  131. '#type' => 'link',
  132. '#url' => $this->url,
  133. '#title' => $this->text,
  134. ];
  135. }
  136. /**
  137. * Returns the link generator.
  138. *
  139. * @return \Drupal\Core\Utility\LinkGeneratorInterface
  140. * The link generator
  141. */
  142. protected function getLinkGenerator() {
  143. if (!isset($this->linkGenerator)) {
  144. $this->linkGenerator = \Drupal::service('link_generator');
  145. }
  146. return $this->linkGenerator;
  147. }
  148. /**
  149. * Sets the link generator service.
  150. *
  151. * @param \Drupal\Core\Utility\LinkGeneratorInterface $generator
  152. * The link generator service.
  153. *
  154. * @return $this
  155. */
  156. public function setLinkGenerator(LinkGeneratorInterface $generator) {
  157. $this->linkGenerator = $generator;
  158. return $this;
  159. }
  160. }