function FieldPluginBase::render_as_link

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

1 call to FieldPluginBase::render_as_link()
FieldPluginBase::render_text in drupal/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php
Perform an advanced text render for the item.

File

drupal/core/modules/views/lib/Drupal/views/Plugin/views/field/FieldPluginBase.php, line 1302
Definition of Drupal\views\Plugin\views\field\FieldPluginBase.

Class

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

Namespace

Drupal\views\Plugin\views\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'];

  // strip_tags() 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(decode_entities(strtr($path, $tokens)));
    if (!empty($alter['path_case']) && $alter['path_case'] != 'none') {
      $path = $this
        ->caseTransform($path, $this->options['alter']['path_case']);
    }
    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'] => '',
    ));
    $query = drupal_get_query_array($url['query']);

    // Remove query parameters that were assigned a query string replacement
    // token for which there is no value available.
    foreach ($query as $param => $val) {
      if ($val == '%' . $param) {
        unset($query[$param]);
      }
    }
    $options['query'] = $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'] = decode_entities($alt);
  }
  $class = strtr($alter['link_class'], $tokens);
  if ($class) {
    $options['attributes']['class'] = array(
      $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'])) {

    // Convert the query to a string, perform token replacement, and then
    // convert back to an array form for l().
    $options['query'] = drupal_http_build_query($alter['query']);
    $options['query'] = strtr($options['query'], $tokens);
    $options['query'] = drupal_get_query_array($options['query']);
  }
  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($alter['language'])) {
    $options['language'] = $alter['language'];
  }

  // If the url came from entity_uri(), pass along the required options.
  if (isset($alter['entity'])) {
    $options['entity'] = $alter['entity'];
  }
  if (isset($alter['entity_type'])) {
    $options['entity_type'] = $alter['entity_type'];
  }
  $value .= l($text, $path, $options);
  if (!empty($alter['suffix'])) {
    $value .= filter_xss_admin(strtr($alter['suffix'], $tokens));
  }
  return $value;
}