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