function views_handler_field::render_as_link

Same name in other branches
  1. 7.x-3.x handlers/views_handler_field.inc \views_handler_field::render_as_link()

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 925

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 (!empty($alter['replace_spaces'])) {
            $path = str_replace(' ', '-', $path);
        }
    }
    // Parse the URL and move any query and fragment parameters out of the path.
    $url = parse_url($path);
    // Seriously malformed URLs may return FALSE or empty arrays.
    if (empty($url)) {
        return $text;
    }
    // If the path is empty do not build a link around the given text and return
    // it as is.
    // http://www.example.com URLs will not have a $url['path'], so check host as well.
    if (empty($url['path']) && empty($url['host']) && empty($url['fragment'])) {
        return $text;
    }
    // If no scheme is provided in the $path, assign the default 'http://'.
    // This allows a url of 'www.example.com' to be converted to 'http://www.example.com'.
    // Only do this on for external URLs.
    if ($alter['external']) {
        if (!isset($url['scheme'])) {
            // There is no scheme, add the default 'http://' to the $path.
            $path = "http://{$path}";
            // Reset the $url array to include the new scheme.
            $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;
}