class Link

Same name in this branch
  1. 8.9.x core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link
  2. 8.9.x core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link
Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link
  2. 11.x core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link
  3. 11.x core/lib/Drupal/Core/Link.php \Drupal\Core\Link
  4. 9 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link
  5. 9 core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link
  6. 9 core/lib/Drupal/Core/Link.php \Drupal\Core\Link
  7. 10 core/modules/jsonapi/src/JsonApiResource/Link.php \Drupal\jsonapi\JsonApiResource\Link
  8. 10 core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link
  9. 10 core/lib/Drupal/Core/Link.php \Drupal\Core\Link

Defines an object that holds information about a link.

Hierarchy

Expanded class hierarchy of Link

93 files declare their use of Link
block.module in core/modules/block/block.module
Controls the visual building blocks a page is constructed with.
block_content.pages.inc in core/modules/block_content/block_content.pages.inc
Provides page callbacks for custom blocks.
BookBreadcrumbBuilder.php in core/modules/book/src/BookBreadcrumbBuilder.php
BookController.php in core/modules/book/src/Controller/BookController.php
BookTestTrait.php in core/modules/book/tests/src/Functional/BookTestTrait.php

... See full list

137 string references to 'Link'
AnnotatedClassDiscoveryTest::provideBadAnnotations in core/tests/Drupal/Tests/Component/Plugin/Discovery/AnnotatedClassDiscoveryTest.php
All the Drupal documentation standards tags.
CKEditorPluginManagerTest::providerGetEnabledButtons in core/modules/ckeditor/tests/src/Unit/CKEditorPluginManagerTest.php
Provides a list of configs to test.
claro_form_alter in core/themes/claro/claro.theme
Implements hook_form_alter().
ContentTranslationHandler::entityFormSharedElements in core/modules/content_translation/src/ContentTranslationHandler.php
Process callback: determines which elements get clue in the form.
ContentTranslationHandlerTest::providerTestEntityFormSharedElements in core/modules/content_translation/tests/src/Kernel/ContentTranslationHandlerTest.php
Returns test cases for ::testEntityFormSharedElements().

... See full list

File

core/lib/Drupal/Core/Link.php, line 11

Namespace

Drupal\Core
View source
class Link implements RenderableInterface {
  
  /**
   * The link generator.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $linkGenerator;
  
  /**
   * 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;
  }
  
  /**
   * Creates a Link object from a given route name and parameters.
   *
   * @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
   *   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.
   * @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
   */
  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.
   *
   * 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()
      ->generateFromLink($this);
  }
  
  /**
   * {@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;
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.