function Link::preRenderLink

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link::preRenderLink()
  2. 10 core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link::preRenderLink()
  3. 11.x core/lib/Drupal/Core/Render/Element/Link.php \Drupal\Core\Render\Element\Link::preRenderLink()

Pre-render callback: Renders a link into #markup.

Doing so during pre_render gives modules a chance to alter the link parts.

Parameters

array $element: A structured array whose keys form the arguments to \Drupal\Core\Utility\LinkGeneratorInterface::generate():

  • #title: The link text.
  • #url: The URL info either pointing to a route or a non routed path.
  • #options: (optional) An array of options to pass to the link generator.

Return value

array The passed-in element containing a rendered link in '#markup'.

File

core/lib/Drupal/Core/Render/Element/Link.php, line 59

Class

Link
Provides a link render element.

Namespace

Drupal\Core\Render\Element

Code

public static function preRenderLink($element) {
    // By default, link options to pass to the link generator are normally set
    // in #options.
    $element += [
        '#options' => [],
    ];
    // However, within the scope of renderable elements, #attributes is a valid
    // way to specify attributes, too. Take them into account, but do not override
    // attributes from #options.
    if (isset($element['#attributes'])) {
        $element['#options'] += [
            'attributes' => [],
        ];
        $element['#options']['attributes'] += $element['#attributes'];
    }
    // This #pre_render callback can be invoked from inside or outside of a Form
    // API context, and depending on that, an HTML ID may be already set in
    // different locations. #options should have precedence over Form API's #id.
    // #attributes have been taken over into #options above already.
    if (isset($element['#options']['attributes']['id'])) {
        $element['#id'] = $element['#options']['attributes']['id'];
    }
    elseif (isset($element['#id'])) {
        $element['#options']['attributes']['id'] = $element['#id'];
    }
    // Conditionally invoke self::preRenderAjaxForm(), if #ajax is set.
    if (isset($element['#ajax']) && !isset($element['#ajax_processed'])) {
        // If no HTML ID was found above, automatically create one.
        if (!isset($element['#id'])) {
            $element['#id'] = $element['#options']['attributes']['id'] = HtmlUtility::getUniqueId('ajax-link');
        }
        $element = static::preRenderAjaxForm($element);
    }
    if (!empty($element['#url']) && $element['#url'] instanceof CoreUrl) {
        $options = NestedArray::mergeDeep($element['#url']->getOptions(), $element['#options']);
        
        /** @var \Drupal\Core\Utility\LinkGenerator $link_generator */
        $link_generator = \Drupal::service('link_generator');
        $generated_link = $link_generator->generate($element['#title'], $element['#url']->setOptions($options));
        $element['#markup'] = $generated_link;
        $generated_link->merge(BubbleableMetadata::createFromRenderArray($element))
            ->applyTo($element);
    }
    return $element;
}

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