You are here

function url_field_presave in URL field 8

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

Implements hook_field_presave().

File

./url.module, line 88
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.
    if (!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'],
      ));
      $item['title'] = url_fetch_title($url);
    }

    // Trim any spaces around the URL title.
    $item['title'] = trim($item['title']);

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