class Link implements RenderableInterface (View source)

Defines an object that holds information about a link.

Properties

protected LinkGeneratorInterface $linkGenerator

The link generator.

protected string $text

The text of the link.

protected Url $url

The URL of the link.

Methods

__construct(string $text, Url $url)

Constructs a new Link object.

static Link
createFromRoute(string $text, string $route_name, array $route_parameters = [], array $options = [])

Creates a Link object from a given route name and parameters.

static Link
fromTextAndUrl(string|array|MarkupInterface $text, Url $url)

Creates a Link object from a given Url object.

string
getText()

Returns the text of the link.

$this
setText(string $text)

Sets the new text of the link.

Url
getUrl()

Returns the URL of the link.

$this
setUrl(Url $url)

Sets the URL of this link.

toString()

Generates the HTML for this Link object.

array
toRenderable()

Returns a render array representation of the object.

getLinkGenerator()

Returns the link generator.

$this
setLinkGenerator(LinkGeneratorInterface $generator)

Sets the link generator service.

Details

__construct(string $text, Url $url)

Constructs a new Link object.

Parameters

string $text

The text of the link.

Url $url

The url object.

static Link createFromRoute(string $text, string $route_name, array $route_parameters = [], array $options = [])

Creates a Link object from a given route name and parameters.

Parameters

string $text

The text of the link.

string $route_name

The name of the route

array $route_parameters

(optional) An associative array of parameter names and values.

array $options

The options parameter takes exactly the same structure. See \Drupal\Core\Url::fromUri() for details.

Return Value

Link

static Link fromTextAndUrl(string|array|MarkupInterface $text, Url $url)

Creates a Link object from a given Url object.

Parameters

string|array|MarkupInterface $text

The link text for the anchor tag as a translated string or render array.

Url $url

The Url to create the link for.

Return Value

Link

string getText()

Returns the text of the link.

Return Value

string

$this setText(string $text)

Sets the new text of the link.

Parameters

string $text

The new text.

Return Value

$this

Url getUrl()

Returns the URL of the link.

Return Value

Url

$this setUrl(Url $url)

Sets the URL of this link.

Parameters

Url $url

The URL object to set

Return Value

$this

GeneratedLink toString()

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 Value

GeneratedLink

The link HTML markup.

See also

Link::toRenderable

array toRenderable()

Returns a render array representation of the object.

Return Value

array

A render array.

protected LinkGeneratorInterface getLinkGenerator()

Returns the link generator.

Return Value

LinkGeneratorInterface

The link generator

$this setLinkGenerator(LinkGeneratorInterface $generator)

Sets the link generator service.

Parameters

LinkGeneratorInterface $generator

The link generator service.

Return Value

$this