function datetime_field_load

Implements hook_field_load().

The function generates a Date object for each field early so that it is cached in the field cache. This avoids the need to generate the object later. The date will be retrieved in UTC, the local timezone adjustment must be made in real time, based on the preferences of the site and user.

File

drupal/core/modules/datetime/datetime.module, line 236
Field hooks to implement a simple datetime field.

Code

function datetime_field_load($entity_type, $entities, $field, $instances, $langcode, &$items) {
  foreach ($entities as $id => $entity) {
    foreach ($items[$id] as $delta => $item) {
      $items[$id][$delta]['date'] = NULL;
      $value = isset($item['value']) ? $item['value'] : NULL;
      if (!empty($value)) {
        $storage_format = $field['settings']['datetime_type'] == 'date' ? DATETIME_DATE_STORAGE_FORMAT : DATETIME_DATETIME_STORAGE_FORMAT;
        $date = new DrupalDateTime($value, DATETIME_STORAGE_TIMEZONE, $storage_format);
        if ($date instanceof DrupalDateTime && !$date
          ->hasErrors()) {
          $items[$id][$delta]['date'] = $date;
        }
      }
    }
  }
}