function views_handler_field::render_as_link

3.x views_handler_field.inc views_handler_field::render_as_link($alter, $text, $tokens)
2.x views_handler_field.inc views_handler_field::render_as_link($alter, $text, $tokens)

Render this field as a link, with the info from a fieldset set by the user.

1 call to views_handler_field::render_as_link()
views_handler_field::render_text in handlers/views_handler_field.inc
Perform an advanced text render for the item.

File

handlers/views_handler_field.inc, line 584

Class

views_handler_field
Base field handler that has no options and renders an unformatted field.

Code

function render_as_link($alter, $text, $tokens) {
  $value = '';

  if (!empty($alter['prefix'])) {
    $value .= filter_xss_admin(strtr($alter['prefix'], $tokens));
  }

  $options = array(
    'html' => TRUE,
    'absolute' => !empty($alter['absolute']) ? TRUE : FALSE,
  );

  // $path will be run through check_url() by l() so we do not need to
  // sanitize it ourselves.
  $path = $alter['path'];

  // html_entity_decode removes <front>, so check whether its different to front.
  if ($path != '<front>') {
    // Use strip tags as there should never be HTML in the path.
    // However, we need to preserve special characters like " that
    // were removed by check_plain().
    $path = strip_tags(html_entity_decode(strtr($path, $tokens)));
  }

  // If the path is empty do not build a link around the given text and return
  // it as is.
  if (empty($path)) {
    return $text;
  }

  // Parse the URL and move any query and fragment parameters out of the path.
  $url = parse_url($path);
  if (isset($url['query'])) {
    $path = strtr($path, array('?' . $url['query'] => ''));
    $options['query'] = $url['query'];
  }
  if (isset($url['fragment'])) {
    $path = strtr($path, array('#' . $url['fragment'] => ''));
    // If the path is empty we want to have a fragment for the current site.
    if ($path == '') {
      $options['external'] = TRUE;
    }
    $options['fragment'] = $url['fragment'];
  }

  $alt = strtr($alter['alt'], $tokens);
  // Set the title attribute of the link only if it improves accessibility
  if ($alt && $alt != $text) {
    $options['attributes']['title'] = html_entity_decode($alt, ENT_QUOTES);
  }

  $class = strtr($alter['link_class'], $tokens);
  if ($class) {
    $options['attributes']['class'] = $class;
  }

  if (!empty($alter['rel']) && $rel = strtr($alter['rel'], $tokens)) {
    $options['attributes']['rel'] = $rel;
  }

  $target = check_plain(trim(strtr($alter['target'], $tokens)));
  if (!empty($target)) {
    $options['attributes']['target'] = $target;
  }

  // Allow the addition of arbitrary attributes to links. Additional attributes
  // currently can only be altered in preprocessors and not within the UI.
  if (isset($alter['link_attributes']) && is_array($alter['link_attributes'])) {
    foreach ($alter['link_attributes'] as $key => $attribute) {
      if (!isset($options['attributes'][$key])) {
        $options['attributes'][$key] = strtr($attribute, $tokens);
      }
    }
  }

  // If the query and fragment were programatically assigned overwrite any
  // parsed values.
  if (isset($alter['query'])) {
    $options['query'] = strtr($alter['query'], $tokens);
  }
  if (isset($alter['alias'])) {
    // Alias is a boolean field, so no token.
    $options['alias'] = $alter['alias'];
  }
  if (isset($alter['fragment'])) {
    $options['fragment'] = strtr($alter['fragment'], $tokens);
  }
  if (isset($this->options['alter']['language'])) {
    $options['language'] = $this->options['alter']['language'];
  }

  $value .= l($text, $path, $options);

  if (!empty($alter['suffix'])) {
    $value .= filter_xss_admin(strtr($alter['suffix'], $tokens));
  }

  return $value;
}