class Link
Same name in this branch
- 11.1.x vendor/open-telemetry/sdk/Trace/Link.php \OpenTelemetry\SDK\Trace\Link
- 11.1.x vendor/open-telemetry/gen-otlp-protobuf/Opentelemetry/Proto/Trace/V1/Span/Link.php \Opentelemetry\Proto\Trace\V1\Span\Link
- 11.1.x vendor/composer/composer/src/Composer/Package/Link.php \Composer\Package\Link
- 11.1.x vendor/symfony/dom-crawler/Link.php \Symfony\Component\DomCrawler\Link
- 11.1.x vendor/phpdocumentor/reflection-docblock/src/DocBlock/Tags/Link.php \phpDocumentor\Reflection\DocBlock\Tags\Link
- 11.1.x core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link
- 11.1.x core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link
Defines an object that holds information about a link.
Hierarchy
- class \Drupal\Core\Link implements \Drupal\Core\Render\RenderableInterface
Expanded class hierarchy of Link
58 files declare their use of Link
- AnnouncementsFeedHooks.php in core/
modules/ announcements_feed/ src/ Hook/ AnnouncementsFeedHooks.php - BlockHooks.php in core/
modules/ block/ src/ Hook/ BlockHooks.php - block_content.pages.inc in core/
modules/ block_content/ block_content.pages.inc - Breadcrumb.php in core/
lib/ Drupal/ Core/ Breadcrumb/ Breadcrumb.php - claro.theme in core/
themes/ claro/ claro.theme - Functions to support theming in the Claro theme.
61 string references to 'Link'
- ckeditor5.ckeditor5.yml in core/
modules/ ckeditor5/ ckeditor5.ckeditor5.yml - core/modules/ckeditor5/ckeditor5.ckeditor5.yml
- claro_form_alter in core/
themes/ claro/ claro.theme - Implements hook_form_alter().
- claro_page_attachments_alter in core/
themes/ claro/ claro.theme - Implements hook_page_attachments_alter().
- ContentTranslationHandler::entityFormSharedElements in core/
modules/ content_translation/ src/ ContentTranslationHandler.php - Process callback: determines which elements get clue in the form.
- contextual.views.schema.yml in core/
modules/ contextual/ config/ schema/ contextual.views.schema.yml - core/modules/contextual/config/schema/contextual.views.schema.yml
File
-
core/
lib/ Drupal/ Core/ Link.php, line 11
Namespace
Drupal\CoreView source
class Link implements RenderableInterface {
/**
* The link generator.
*
* @var \Drupal\Core\Utility\LinkGeneratorInterface
*/
protected $linkGenerator;
/**
* The link text for the anchor tag as a translated string or render array.
*
* Strings will be sanitized automatically. If you need to output HTML in
* the link text, use a render array or an already sanitized string such as
* the output of \Drupal\Component\Utility\Xss::filter() or
* \Drupal\Component\Render\FormattableMarkup.
*
* @var string|array|\Drupal\Component\Render\MarkupInterface
*/
protected $text;
/**
* The URL of the link.
*
* @var \Drupal\Core\Url
*/
protected $url;
/**
* Constructs a new Link object.
*
* @param string|array|\Drupal\Component\Render\MarkupInterface $text
* The link text for the anchor tag as a translated string or render array.
* Strings will be sanitized automatically. If you need to output HTML in
* the link text, use a render array or an already sanitized string such as
* the output of \Drupal\Component\Utility\Xss::filter() or
* \Drupal\Component\Render\FormattableMarkup.
* @param \Drupal\Core\Url $url
* The \Drupal\Core\Url object.
*/
public function __construct($text, Url $url) {
$this->text = $text;
$this->url = $url;
}
/**
* Creates a Link object from a given route name and parameters.
*
* @param string|array|\Drupal\Component\Render\MarkupInterface $text
* The link text for the anchor tag as a translated string or render array.
* Strings will be sanitized automatically. If you need to output HTML in
* the link text, use a render array or an already sanitized string such as
* the output of \Drupal\Component\Utility\Xss::filter() or
* \Drupal\Component\Render\FormattableMarkup.
* @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.
*
* @return static
*/
public static function createFromRoute($text, $route_name, $route_parameters = [], $options = []) {
return new static($text, new Url($route_name, $route_parameters, $options));
}
/**
* Creates a Link object from a given Url object.
*
* @param string|array|\Drupal\Component\Render\MarkupInterface $text
* The link text for the anchor tag as a translated string or render array.
* Strings will be sanitized automatically. If you need to output HTML in
* the link text, use a render array or an already sanitized string such as
* the output of \Drupal\Component\Utility\Xss::filter() or
* \Drupal\Component\Render\FormattableMarkup.
* @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);
}
/**
* Returns the text of the link.
*
* @return string|array|\Drupal\Component\Render\MarkupInterface
* The link text for the anchor tag as a translated string or render array.
* Strings will be sanitized automatically. If you need to output HTML in
* the link text, use a render array or an already sanitized string such as
* the output of \Drupal\Component\Utility\Xss::filter() or
* \Drupal\Component\Render\FormattableMarkup.
*/
public function getText() {
return $this->text;
}
/**
* Sets the new text of the link.
*
* @param string|array|\Drupal\Component\Render\MarkupInterface $text
* The link text for the anchor tag as a translated string or render array.
* Strings will be sanitized automatically. If you need to output HTML in
* the link text, use a render array or an already sanitized string such as
* the output of \Drupal\Component\Utility\Xss::filter() or
* \Drupal\Component\Render\FormattableMarkup.
*
* @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.
*
* 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.
*
* @see \Drupal\Core\Link::toRenderable()
*/
public function toString() {
return $this->getLinkGenerator()
->generate($this->text, $this->url);
}
/**
* {@inheritdoc}
*/
public function toRenderable() {
return [
'#type' => 'link',
'#url' => $this->url,
'#title' => $this->text,
];
}
/**
* 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;
}
}
Members
Title Sort descending | Modifiers | Object type | Summary | Overriden Title |
---|---|---|---|---|
Link::$linkGenerator | protected | property | The link generator. | |
Link::$text | protected | property | The link text for the anchor tag as a translated string or render array. | |
Link::$url | protected | property | The URL of the link. | |
Link::createFromRoute | public static | function | Creates a Link object from a given route name and parameters. | |
Link::fromTextAndUrl | public static | function | Creates a Link object from a given Url object. | |
Link::getLinkGenerator | protected | function | Returns the link generator. | |
Link::getText | public | function | Returns the text of the link. | |
Link::getUrl | public | function | Returns the URL of the link. | |
Link::setLinkGenerator | public | function | Sets the link generator service. | |
Link::setText | public | function | Sets the new text of the link. | |
Link::setUrl | public | function | Sets the URL of this link. | |
Link::toRenderable | public | function | Returns a render array representation of the object. | Overrides RenderableInterface::toRenderable |
Link::toString | public | function | Generates the HTML for this Link object. | |
Link::__construct | public | function | Constructs a new Link object. |