annotate core/lib/Drupal/Core/Link.php @ 19:fa3358dc1485 tip

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