1 link.module _link_sanitize(array &$item, $delta, array $field, $instance, $entity)

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

Parameters

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

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

array $field: The field definition.

object $entity: The entity containing this link.

File

core/modules/link/link.module, line 459
Defines simple link field types.

Code

function _link_sanitize(array &$item, $delta, array $field, $instance, $entity) {
  // Don't try to process empty links.
  if (empty($item['url']) && empty($item['title'])) {
    return;
  }
  if (empty($item['html'])) {
    $item['html'] = FALSE;
  }

  // Replace URL tokens.
  $entity_type = $instance['entity_type'];
  $entity_info = entity_get_info($entity_type);
  $property_id = $entity_info['entity keys']['id'];
  $entity_token_type = !empty($entity_info['token type']) ? $entity_info['token type'] : 'unknown';
  if (isset($instance['settings']['enable_tokens']) && $instance['settings']['enable_tokens']) {
    $text_tokens = token_scan($item['url']);
    if (!empty($text_tokens)) {
      // Load the entity if necessary for entities in views.
      if (isset($entity->{$property_id})) {
        $entity_loaded = entity_load($entity_type, $entity->{$property_id});
      }
      else {
        $entity_loaded = $entity;
      }
      $item['url'] = token_replace($item['url'], array($entity_token_type => $entity_loaded));
    }
  }

  $type = link_validate_url($item['url']);
  // If the type of the URL cannot be determined and URL validation is disabled,
  // then assume LINK_EXTERNAL for later processing.
  if ($type == FALSE && $instance['settings']['validate_url'] === 0) {
    $type = LINK_EXTERNAL;
  }
  $url = link_cleanup_url($item['url']);
  $url_parts = _link_parse_url($url);

  if (!empty($url_parts['url'])) {
    $item['url'] = url($url_parts['url'], 
    array(
      'query' => isset($url_parts['query']) ? $url_parts['query'] : NULL,
      'fragment' => isset($url_parts['fragment']) ? $url_parts['fragment'] : NULL,
      'html' => TRUE,
    )
    );
  }

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

  // Use the title defined at the instance level.
  if ($instance['settings']['title'] == 'value' && strlen(trim($instance['settings']['title_value']))) {
    $title = $instance['settings']['title_value'];
    if (function_exists('i18n_string_translate')) {
      $i18n_string_name = "field:{$instance['field_name']}:{$instance['bundle']}:title_value";
      $title = i18n_string_translate($i18n_string_name, $title);
    }
  }
  // Use the title defined by the user at the widget level.
  elseif (isset($item['title'])) {
    $title = $item['title'];
  }
  else {
    $title = '';
  }

  // Replace title tokens.
  if ($title && ($instance['settings']['title'] == 'value' || $instance['settings']['enable_tokens'])) {
    $text_tokens = token_scan($title);
    if (!empty($text_tokens)) {
      // Load the entity if necessary for entities in views.
      if (isset($entity->{$property_id})) {
        $entity_loaded = entity_load($entity_type, $entity->{$property_id});
      }
      else {
        $entity_loaded = $entity;
      }
      $title = token_replace($title, array($entity_token_type => $entity_loaded));
    }
    $title = filter_xss($title, array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
    $item['html'] = TRUE;
  }
  $item['title'] = empty($title) ? $item['display_url'] : $title;

  if (!isset($item['attributes'])) {
    $item['attributes'] = array();
  }

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

  // Merge item attributes with attributes defined at the field level.
  $item['attributes'] += $instance['settings']['attributes'];

  // If user is not allowed to choose target attribute, use default defined at
  // field level.
  if ($instance['settings']['attributes']['target'] != LINK_TARGET_USER) {
    $item['attributes']['target'] = $instance['settings']['attributes']['target'];
  }
  elseif ($item['attributes']['target'] == LINK_TARGET_USER) {
    $item['attributes']['target'] = LINK_TARGET_DEFAULT;
  }

  // Remove the target attribute if the default (no target) is selected.
  if (empty($item['attributes']) || (isset($item['attributes']['target']) && $item['attributes']['target'] == LINK_TARGET_DEFAULT)) {
    unset($item['attributes']['target']);
  }

  // Remove rel attribute for internal or external links if selected.
  if (isset($item['attributes']['rel']) && isset($instance['settings']['rel_remove']) && $instance['settings']['rel_remove'] != 'default') {
    if (($instance['settings']['rel_remove'] != 'rel_remove_internal' && $type != LINK_INTERNAL) || 
      ($instance['settings']['rel_remove'] != 'rel_remove_external' && $type != LINK_EXTERNAL)) {
      unset($item['attributes']['rel']);
    }
  }

  // Handle "title" link attribute.
  if (!empty($item['attributes']['title']) && module_exists('token')) {
    $text_tokens = token_scan($item['attributes']['title']);
    if (!empty($text_tokens)) {
      // Load the entity (necessary for entities in views).
      if (isset($entity->{$property_id})) {
        $entity_loaded = entity_load($entity_type, $entity->{$property_id});
      }
      else {
        $entity_loaded = $entity;
      }
      $item['attributes']['title'] = token_replace($item['attributes']['title'], array($entity_token_type => $entity_loaded));
    }
    $item['attributes']['title'] = filter_xss($item['attributes']['title'], array('b', 'br', 'code', 'em', 'i', 'img', 'span', 'strong', 'sub', 'sup', 'tt', 'u'));
  }
  // Handle attribute classes.
  $classes = explode(' ', $item['attributes']['class']);
  $classes = array_filter($classes);
  foreach ($classes as $n => $class) {
    $classes[$n] = backdrop_clean_css_identifier($class);
  }
  $item['attributes']['class'] = implode(' ', $classes);
  unset($item['attributes']['configurable_class']);

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

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