Newer
Older

Angie Byron
committed
<?php
namespace Drupal\Core;

Alex Pott
committed
use Drupal\Core\Render\RenderableInterface;
use Drupal\Core\Utility\LinkGeneratorInterface;

Angie Byron
committed

Angie Byron
committed
/**
* Defines an object that holds information about a link.
*/

Alex Pott
committed
class Link implements RenderableInterface {

Angie Byron
committed

Alex Pott
committed
/**
* The link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface

Alex Pott
committed
*/
protected $linkGenerator;

Angie Byron
committed

Angie Byron
committed
/**
* The text of the link.
*
* @var string
*/
protected $text;
/**
* The URL of the link.
*
* @var \Drupal\Core\Url
*/
protected $url;
/**
* Constructs a new Link object.
*
* @param string $text
* The text of the link.
* @param \Drupal\Core\Url $url
* The url object.
*/
public function __construct($text, Url $url) {
$this->text = $text;
$this->url = $url;
}
/**

Alex Pott
committed
* Creates a Link object from a given route name and parameters.

Angie Byron
committed
*
* @param string $text
* The text of the link.
* @param string $route_name
* The name of the route
* @param array $route_parameters
* (optional) An associative array of parameter names and values.
* @param array $options
* The options parameter takes exactly the same structure.
* See \Drupal\Core\Url::fromUri() for details.

Angie Byron
committed
*
* @return static
*/
public static function createFromRoute($text, $route_name, $route_parameters = [], $options = []) {

Angie Byron
committed
return new static($text, new Url($route_name, $route_parameters, $options));
}

Alex Pott
committed
/**
* Creates a Link object from a given Url object.
*
* @param string $text
* The text of the link.
* @param \Drupal\Core\Url $url
* The Url to create the link for.
*
* @return static
*/
public static function fromTextAndUrl($text, Url $url) {
return new static($text, $url);
}

Angie Byron
committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/**
* Returns the text of the link.
*
* @return string
*/
public function getText() {
return $this->text;
}
/**
* Sets the new text of the link.
*
* @param string $text
* The new text.
*
* @return $this
*/
public function setText($text) {
$this->text = $text;
return $this;
}
/**
* Returns the URL of the link.
*
* @return \Drupal\Core\Url
*/
public function getUrl() {
return $this->url;
}
/**
* Sets the URL of this link.
*
* @param Url $url
* The URL object to set
*
* @return $this
*/
public function setUrl(Url $url) {
$this->url = $url;
return $this;
}

Angie Byron
committed
/**
* Generates the HTML for this Link object.
*
* Do not use this method to render a link in an HTML context. In an HTML
* context, self::toRenderable() should be used so that render cache
* information is maintained. However, there might be use cases such as tests
* and non-HTML contexts where calling this method directly makes sense.
*
* @return \Drupal\Core\GeneratedLink
* The link HTML markup.

Alex Pott
committed
*
* @see \Drupal\Core\Link::toRenderable()

Angie Byron
committed
*/
public function toString() {
return $this->getLinkGenerator()->generateFromLink($this);

Angie Byron
committed
}

Alex Pott
committed
/**
* {@inheritdoc}
*/
public function toRenderable() {
return [
'#type' => 'link',
'#url' => $this->url,
'#title' => $this->text,
];
}
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/**
* 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;
}