You are here

function _link_process in Link 7

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

Prepares the item attributes and url for storage.

@codingStandardsIgnoreStart

Parameters

array $item: Link field values.

array $delta: The sequence number for current values.

array $field: The field structure array.

object $entity: Entity object.

array $instance: The instance structure for $field on $entity's bundle.

2 calls to _link_process()
link_field_insert in ./link.module
Implements hook_field_insert().
link_field_update in ./link.module
Implements hook_field_update().

File

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

Code

function _link_process(&$item, $delta, $field, $entity, $instance) {

  // @codingStandardsIgnoreEnd
  // Trim whitespace from URL.
  if (!empty($item['url'])) {
    $item['url'] = trim($item['url']);
  }

  // Optionally convert aliases to the system path.
  if (!empty($instance['settings']['convert_aliases'])) {
    global $base_url;

    // Check if either the site's absolute URL or the relative base URL are at
    // the start of the URL, if so remove them.
    $base_paths = array(
      $base_url . base_path(),
      base_path(),
    );

    // Work out the correct base_path to use based on the HTTPS settings.
    if (isset($GLOBALS['base_secure_url'])) {
      $base_paths[] = $GLOBALS['base_secure_url'] . base_path();
    }
    if (isset($GLOBALS['base_insecure_url'])) {
      $base_paths[] = $GLOBALS['base_insecure_url'] . base_path();
    }

    // Add any additional paths.
    if ($extra_paths = variable_get('link_base_urls', array())) {

      // Create versions with and without the base path.
      foreach ($extra_paths as $extra_path) {
        $base_paths[] = $extra_path;
        $base_paths[] = $extra_path . base_path();
      }
    }
    $paths_to_test = array(
      $item['url'],
    );
    foreach ($base_paths as $path) {

      // Verify the path is at the beginning of the URL string.
      if (strpos($item['url'], $path) === 0) {
        $strlen = drupal_strlen($path);
        $paths_to_test[] = drupal_substr($item['url'], $strlen);
      }
    }

    // Check each of the paths to see if one of them is a system path.
    foreach (array_unique($paths_to_test) as $path) {
      $language = NULL;

      // If we have locale enabled attempt to remove the language prefix first.
      if (module_exists('locale')) {
        require_once DRUPAL_ROOT . '/includes/language.inc';
        list($language, $path) = language_url_split_prefix($path, language_list());
      }

      // Attempt to get a system path.
      $normal_path = drupal_get_normal_path($path, $language);

      // If we get back a different path it means Drupal found a system path we
      // can use.
      if ($normal_path != $path) {
        $item['url'] = $normal_path;
        break;
      }
    }
  }

  // If no attributes are set then make sure $item['attributes'] is an empty
  // array, so $field['attributes'] can override it.
  if (empty($item['attributes'])) {
    $item['attributes'] = array();
  }

  // Serialize the attributes array.
  if (!is_string($item['attributes'])) {
    $item['attributes'] = serialize($item['attributes']);
  }

  // Don't save an invalid default value (e.g. 'http://').
  if (isset($field['widget']['default_value'][$delta]['url']) && $item['url'] == $field['widget']['default_value'][$delta]['url'] && is_object($entity)) {
    $langcode = !empty($entity) ? field_language($instance['entity_type'], $entity, $instance['field_name']) : LANGUAGE_NONE;
    if (!link_validate_url($item['url'], $langcode)) {
      unset($item['url']);
    }
  }
}