You are here

function url_field_presave in URL field 7

Same name and namespace in other branches
  1. 8 url.module \url_field_presave()

Implements hook_field_presave().

1 call to url_field_presave()
_url_fix_field_default_value in ./url.install
Helper function to fix incorrect default values of URL field instances.

File

./url.module, line 86
Provides a URL field type that stores external links with optional titles.

Code

function url_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  $settings = $instance['settings'];
  foreach ($items as $delta => &$item) {

    // If the link title is empty, and auto-fetching is enabled, then attempt
    // to extract the title from a request to the URL.
    // Skip this process if $entity is empty (field default value form)
    if (!empty($entity) && !empty($settings['title_fetch']) && !empty($item['value']) && empty($item['title'])) {

      // Parse the URL into a form ready for url().
      $parsed = drupal_parse_url($item['value']);
      $url = url($parsed['path'], array(
        'query' => $parsed['query'],
        'fragment' => $parsed['fragment'],
        'absolute' => TRUE,
      ));
      $item['title'] = url_fetch_title($url);
    }

    // Trim any spaces around the URL title.
    $item['title'] = isset($item['title']) && is_string($item['title']) ? trim($item['title']) : '';

    // Serialize the attributes array.
    $item['attributes'] = !empty($item['attributes']) && is_array($item['attributes']) ? serialize($item['attributes']) : NULL;
  }
}