You are here

function _link_sanitize in Link 6

Same name and namespace in other branches
  1. 6.2 link.inc \_link_sanitize()
  2. 7 link.module \_link_sanitize()

Cleanup user-entered values for a link field according to field settings.

Parameters

$item: A single link item, usually containing url, title, and attributes.

$delta: The delta value if this field is one of multiple fields.

$field: The CCK field definition.

$node: The node containing this link.

1 call to _link_sanitize()
link_field in ./link.module
Implementation of hook_field().

File

./link.module, line 297
Defines simple link field types.

Code

function _link_sanitize(&$item, $delta, &$field, &$node) {

  // Don't try to process empty links.
  if (empty($item['url']) && empty($item['title'])) {
    return;
  }

  // Replace URL tokens.
  if (module_exists('token') && $field['enable_tokens']) {

    // Load the node if necessary for nodes in views.
    $token_node = isset($node->nid) ? node_load($node->nid) : $node;
    $item['url'] = token_replace($item['url'], 'node', $token_node);
  }
  $type = link_validate_url($item['url']);
  $url = link_cleanup_url($item['url']);

  // Separate out the anchor if any.
  if (strpos($url, '#') !== FALSE) {
    $item['fragment'] = substr($url, strpos($url, '#') + 1);
    $url = substr($url, 0, strpos($url, '#'));
  }

  // Separate out the query string if any.
  if (strpos($url, '?') !== FALSE) {
    $item['query'] = substr($url, strpos($url, '?') + 1);
    $url = substr($url, 0, strpos($url, '?'));
  }

  // Save the new URL without the anchor or query.
  $item['url'] = $url;

  // Create a shortened URL for display.
  $display_url = $type == LINK_EMAIL ? str_replace('mailto:', '', $url) : url($url, array(
    'query' => isset($item['query']) ? $item['query'] : NULL,
    'fragment' => isset($item['fragment']) ? $item['fragment'] : NULL,
    'absolute' => TRUE,
  ));
  if ($field['display']['url_cutoff'] && strlen($display_url) > $field['display']['url_cutoff']) {
    $display_url = substr($display_url, 0, $field['display']['url_cutoff']) . "...";
  }
  $item['display_url'] = $display_url;

  // Use the title defined at the field level.
  if ($field['title'] == 'value' && strlen(trim($field['title_value']))) {
    $title = $field['title_value'];
  }
  else {
    $title = $item['title'];
  }

  // Replace tokens.
  if (module_exists('token') && ($field['title'] == 'value' || $field['enable_tokens'])) {

    // Load the node if necessary for nodes in views.
    $token_node = isset($node->nid) ? node_load($node->nid) : $node;
    $title = filter_xss(token_replace($title, 'node', $token_node), array(
      'b',
      'br',
      'code',
      'em',
      'i',
      'img',
      'span',
      'strong',
      'sub',
      'sup',
      'tt',
      'u',
    ));
    $item['html'] = TRUE;
  }
  $item['display_title'] = empty($title) ? $item['display_url'] : $title;

  // Add attributes defined at the widget level
  $attributes = array();
  if (!empty($item['attributes']) && is_array($item['attributes'])) {
    foreach ($item['attributes'] as $attribute => $attbvalue) {
      if (isset($item['attributes'][$attribute]) && $field['attributes'][$attribute] == 'user') {
        $attributes[$attribute] = $attbvalue;
      }
    }
  }

  // Add attributes defined at the field level
  if (is_array($field['attributes'])) {
    foreach ($field['attributes'] as $attribute => $attbvalue) {
      if (!empty($attbvalue) && $attbvalue != 'default' && $attbvalue != 'user') {
        $attributes[$attribute] = $attbvalue;
      }
    }
  }

  // Remove the rel=nofollow for internal links.
  if ($type != LINK_EXTERNAL && isset($attributes['rel']) && strpos($attributes['rel'], 'nofollow') !== FALSE) {
    $attributes['rel'] = str_replace('nofollow', '', $attributes['rel']);
    if (empty($attributes['rel'])) {
      unset($attributes['rel']);
    }
  }
  $item['attributes'] = $attributes;

  // Add the widget label.
  $item['label'] = $field['widget']['label'];
}