8.3.x common.inc drupal_pre_render_link($element)
8.0.x common.inc drupal_pre_render_link($element)
8.1.x common.inc drupal_pre_render_link($element)
8.2.x common.inc drupal_pre_render_link($element)
8.4.x common.inc drupal_pre_render_link($element)
7.x common.inc drupal_pre_render_link($element)

#pre_render callback to render a link into #markup.

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

Parameters

$elements: A structured array whose keys form the arguments to l():

  • #title: The link text to pass as argument to l().
  • #href: The URL path component to pass as argument to l().
  • #options: (optional) An array of options to pass to l().

Return value

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

1 string reference to 'drupal_pre_render_link'
system_element_info in modules/system/system.module
Implements hook_element_info().

File

includes/common.inc, line 5721
Common functions that many Drupal modules will need to reference.

Code

function drupal_pre_render_link($element) {
  // By default, link options to pass to l() are normally set in #options.
  $element += array('#options' => array());
  // 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'] += array('attributes' => array());
    $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, a 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 ajax_pre_render_element(), 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'] = drupal_html_id('ajax-link');
    }
    // If #ajax['path] was not specified, use the href as Ajax request URL.
    if (!isset($element['#ajax']['path'])) {
      $element['#ajax']['path'] = $element['#href'];
      $element['#ajax']['options'] = $element['#options'];
    }
    $element = ajax_pre_render_element($element);
  }

  $element['#markup'] = l($element['#title'], $element['#href'], $element['#options']);
  return $element;
}

Comments

hgoto’s picture

This function drupal_pre_render_link() is used for rendering '#type' => 'link'.

  $build['outlink'] = array(
    '#type' => 'link',
    '#title' => 'Central help and support site',
    '#href' => 'http://help.example.com',
  );

(This sample code was provided by mlncn in the page of theme_link().)

On the other hand, the similar '#theme' => 'link' uses theme_link().

https://api.drupal.org/api/drupal/includes!theme.inc/function/theme_link...

Note that they look similar but completely different when you use them.