You are here

function _link_sanitize in Link 6.2

Same name and namespace in other branches
  1. 6 link.module \_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.inc, line 78
Helper functions for Link field, widget and form elements.

Code

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

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

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

    // 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']);

  // If we can't determine the type of url, and we've been told not to validate it,
  // then we assume it's a LINK_EXTERNAL type for later processing. #357604
  if ($type == FALSE && isset($field['validate_url']) && $field['validate_url'] === 0) {
    $type = LINK_EXTERNAL;
  }
  $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.
  if (isset($field['validate_url']) && $field['validate_url'] === 0) {
    $item['url'] = check_plain($url);
  }
  else {
    $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 (is_array($field['display']) && !empty($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. - originally we only did it for value titles.
  if (($field['title'] == 'value' || $field['enable_tokens']) && module_exists('token')) {

    // 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;
  }
  elseif ($field['title'] == 'value') {
    $title = filter_xss($title, 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;
  if (!isset($item['attributes'])) {
    $item['attributes'] = array();
  }

  // Unserialize attributtes array if it has not been unserialized yet.
  if (!is_array($item['attributes'])) {
    $item['attributes'] = (array) unserialize($item['attributes']);
  }

  // Add default attributes.  Make sure that $field['attributes'] is an array. #626932
  if (!is_array($field['attributes'])) {
    $field['attributes'] = array();
  }
  $field['attributes'] += _link_default_attributes();

  // Merge item attributes with attributes defined at the field level.
  $item['attributes'] = array_filter($item['attributes']);
  $item['attributes'] += $field['attributes'];
  if (empty($item['attributes'])) {
    unset($item['attributes']['target']);
  }
  switch ($field['attributes']['target']) {
    case LINK_TARGET_DEFAULT:
      unset($item['attributes']['target']);
      break;
    case LINK_TARGET_USER:

      // '_blank' is the only authorized value for target in this version.
      if ($item['attributes']['target'] != '_blank') {
        unset($item['attributes']['target']);
      }
      break;
    default:

      // LINK_TARGET_NEW_WINDOW and LINK_TARGET_TOP
      $item['attributes']['target'] = $field['attributes']['target'];
      break;
  }

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

  // Some old field data may have $item['#item']['attributes']['rel'] as an array, which will cause errors:
  if (isset($item['#item']['attributes']['rel']) && is_array($item['#item']['attributes']['rel'])) {
    unset($item['#item']['attributes']['rel']);
  }

  // Handle "title" link attribute
  if (!empty($item['attributes']['title']) && module_exists('token')) {

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

  // Remove title attribute if it's equal to link text.
  if ($item['attributes']['title'] == $item['display_title']) {
    unset($item['attributes']['title']);
  }

  // Hide the display title the URL is empty and the "Hide title if URL is
  // empty" has been checked.
  if (empty($item['url']) && isset($field['title_value_visibility']) && $field['title_value_visibility'] == 1) {
    unset($item['display_title']);
  }

  // Remove empty attributes.
  $item['attributes'] = array_filter($item['attributes']);

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