2015-08-17 17:00:26 -07:00
< ? php
namespace Drupal\Core ;
2015-10-08 11:40:12 -07:00
use Drupal\Core\Render\RenderableInterface ;
2015-08-17 17:00:26 -07:00
use Drupal\Core\Routing\LinkGeneratorTrait ;
/**
* Defines an object that holds information about a link .
*/
2015-10-08 11:40:12 -07:00
class Link implements RenderableInterface {
2015-08-17 17:00:26 -07:00
2015-10-08 11:40:12 -07:00
/**
* @ deprecated in Drupal 8.0 . x - dev , will be removed before Drupal 9.0 . 0.
*/
2015-08-17 17:00:26 -07:00
use LinkGeneratorTrait ;
/**
* 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 ;
}
/**
2015-10-08 11:40:12 -07:00
* Creates a Link object from a given route name and parameters .
2015-08-17 17:00:26 -07:00
*
* @ 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
2017-02-02 16:28:38 -08:00
* The options parameter takes exactly the same structure .
* See \Drupal\Core\Url :: fromUri () for details .
2015-08-17 17:00:26 -07:00
*
* @ return static
*/
public static function createFromRoute ( $text , $route_name , $route_parameters = array (), $options = array ()) {
return new static ( $text , new Url ( $route_name , $route_parameters , $options ));
}
2015-10-08 11:40:12 -07:00
/**
* 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 );
}
2015-08-17 17:00:26 -07:00
/**
* 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 ;
}
/**
* Generates the HTML for this Link object .
*
2016-01-06 16:31:26 -08:00
* 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 .
*
2015-10-08 11:40:12 -07:00
* @ return \Drupal\Core\GeneratedLink
2015-08-17 17:00:26 -07:00
* The link HTML markup .
2015-10-08 11:40:12 -07:00
*
2016-01-06 16:31:26 -08:00
* @ see \Drupal\Core\Link :: toRenderable ()
2015-10-08 11:40:12 -07:00
*/
public function toString () {
return $this -> getLinkGenerator () -> generateFromLink ( $this );
}
/**
* { @ inheritdoc }
2015-08-17 17:00:26 -07:00
*/
2015-10-08 11:40:12 -07:00
public function toRenderable () {
return [
'#type' => 'link' ,
'#url' => $this -> url ,
'#title' => $this -> text ,
];
2015-08-17 17:00:26 -07:00
}
}