Chris@0: text = $text; Chris@0: $this->url = $url; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a Link object from a given route name and parameters. Chris@0: * Chris@0: * @param string $text Chris@0: * The text of the link. Chris@0: * @param string $route_name Chris@0: * The name of the route Chris@0: * @param array $route_parameters Chris@0: * (optional) An associative array of parameter names and values. Chris@0: * @param array $options Chris@0: * The options parameter takes exactly the same structure. Chris@0: * See \Drupal\Core\Url::fromUri() for details. Chris@0: * Chris@0: * @return static Chris@0: */ Chris@0: public static function createFromRoute($text, $route_name, $route_parameters = [], $options = []) { Chris@0: return new static($text, new Url($route_name, $route_parameters, $options)); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a Link object from a given Url object. Chris@0: * Chris@0: * @param string $text Chris@0: * The text of the link. Chris@0: * @param \Drupal\Core\Url $url Chris@0: * The Url to create the link for. Chris@0: * Chris@0: * @return static Chris@0: */ Chris@0: public static function fromTextAndUrl($text, Url $url) { Chris@0: return new static($text, $url); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the text of the link. Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: public function getText() { Chris@0: return $this->text; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the new text of the link. Chris@0: * Chris@0: * @param string $text Chris@0: * The new text. Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setText($text) { Chris@0: $this->text = $text; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the URL of the link. Chris@0: * Chris@0: * @return \Drupal\Core\Url Chris@0: */ Chris@0: public function getUrl() { Chris@0: return $this->url; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the URL of this link. Chris@0: * Chris@0: * @param Url $url Chris@0: * The URL object to set Chris@0: * Chris@0: * @return $this Chris@0: */ Chris@0: public function setUrl(Url $url) { Chris@0: $this->url = $url; Chris@0: return $this; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates the HTML for this Link object. Chris@0: * Chris@0: * Do not use this method to render a link in an HTML context. In an HTML Chris@0: * context, self::toRenderable() should be used so that render cache Chris@0: * information is maintained. However, there might be use cases such as tests Chris@0: * and non-HTML contexts where calling this method directly makes sense. Chris@0: * Chris@0: * @return \Drupal\Core\GeneratedLink Chris@0: * The link HTML markup. Chris@0: * Chris@0: * @see \Drupal\Core\Link::toRenderable() Chris@0: */ Chris@0: public function toString() { Chris@0: return $this->getLinkGenerator()->generateFromLink($this); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function toRenderable() { Chris@0: return [ Chris@0: '#type' => 'link', Chris@0: '#url' => $this->url, Chris@0: '#title' => $this->text, Chris@0: ]; Chris@0: } Chris@0: Chris@0: }