You are here

function date_formatter_process in Date 8

Same name and namespace in other branches
  1. 5.2 date/date.module \date_formatter_process()
  2. 6.2 date/date.module \date_formatter_process()
  3. 6 date/date.module \date_formatter_process()
  4. 7.3 date.module \date_formatter_process()
  5. 7 date.module \date_formatter_process()
  6. 7.2 date.module \date_formatter_process()

Helper function for creating formatted date arrays from a formatter.

Use the Date API to get an object representation of a date field.

Parameters

string $formatter: The date formatter.

string $entity_type: The entity_type for the instance

object $entity: The entity object.

array $field: The field info array.

array $instance: The field instance array.

string $langcode: The language code used by this field.

array $item: An entity field item, like $entity->myfield[0].

array $display: The instance display settings.

Return value

array An array that holds the Start and End date objects. Each date object looks like: date [value] => array ( [db] => array ( // the value stored in the database [object] => the datetime object [datetime] => 2007-02-15 20:00:00 ) [local] => array ( // the local representation of that value [object] => the datetime object [datetime] => 2007-02-15 14:00:00 [timezone] => US/Central [offset] => -21600 ) )

4 calls to date_formatter_process()
DateDefaultFormatter::viewElements in lib/Drupal/date/Plugin/field/formatter/DateDefaultFormatter.php
Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
DateFieldDefaultFormatter::viewElements in date_field/lib/Drupal/date_field/Plugin/field/formatter/DateFieldDefaultFormatter.php
Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
DateFieldFormatIntervalFormatter::viewElements in date_field/lib/Drupal/date_field/Plugin/field/formatter/DateFieldFormatIntervalFormatter.php
Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().
FormatIntervalFormatter::viewElements in lib/Drupal/date/Plugin/field/formatter/FormatIntervalFormatter.php
Implements Drupal\field\Plugin\Type\Formatter\FormatterInterface::viewElements().

File

./date.module, line 209
Defines date/time field types.

Code

function date_formatter_process($formatter, $entity, $field, $instance, $langcode, $item, $settings) {
  $dates = array();
  $timezone = drupal_get_user_timezone();
  if (empty($timezone)) {
    return $dates;
  }
  $granularity = date_granularity($field);
  $field_name = $field['field_name'];
  $format = date_formatter_format($formatter, $settings, $granularity, $langcode);
  $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  $timezone = date_get_timezone($field['settings']['tz_handling'], $timezone);
  $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  $db_format = date_type_format($field['type']);
  $keys = date_available_values($field);
  foreach ($keys as $key) {
    if (empty($item[$key])) {
      $dates[$key] = NULL;
    }
    else {

      // Create a date object with a GMT timezone from the database value.
      $dates[$key] = array();

      // Check to see if this date was already created by date_field_load().
      if (isset($item['db'][$key])) {
        $date = $item['db'][$key];
      }
      else {
        $date = new DrupalDateTime($item[$key], $timezone_db, $db_format);
        $date->granularity = $granularity;
      }
      $dates[$key]['db']['object'] = $date;
      $dates[$key]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
      date_timezone_set($date, timezone_open($timezone));
      $dates[$key]['local']['object'] = $date;
      $dates[$key]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
      $dates[$key]['local']['timezone'] = $timezone;
      $dates[$key]['local']['offset'] = date_offset_get($date);

      // Format the date, special casing the 'interval' format which doesn't
      // need to be processed.
      $dates[$key]['formatted'] = '';
      $dates[$key]['formatted_iso'] = $date
        ->format('c');
      if ($date instanceof DrupalDateTime) {
        if ($format == 'format_interval') {
          $dates[$key]['interval'] = date_format_interval($date);
        }
        elseif ($format == 'format_calendar_day') {
          $dates[$key]['calendar_day'] = date_format_calendar_day($date);
        }
        elseif ($format == 'U') {
          $dates[$key]['formatted'] = $date
            ->format($format);
          $dates[$key]['formatted_date'] = $date
            ->format($format);
          $dates[$key]['formatted_time'] = '';
          $dates[$key]['formatted_timezone'] = '';
        }
        elseif (!empty($format)) {
          $dates[$key]['formatted'] = $date
            ->format($format);
          $dates[$key]['formatted_date'] = $date
            ->format(DateGranularity::limitFormat($format, DateGranularity::$date_parts));
          $dates[$key]['formatted_time'] = $date
            ->format(DateGranularity::limitFormat($format, DateGranularity::$time_parts));
          $dates[$key]['formatted_timezone'] = $date
            ->format(DateGranularity::limitFormat($format, array(
            'timezone',
          )));
        }
      }
    }
  }
  if (empty($dates['value2'])) {
    $dates['value2'] = $dates['value'];
  }

  // Allow other modules to alter the date values.
  $context = array(
    'field' => $field,
    'instance' => $instance,
    'format' => $format,
    'entity' => $entity,
    'langcode' => $langcode,
    'item' => $item,
    'settings' => $settings,
  );
  drupal_alter('date_formatter_dates', $dates, $context);
  $dates['format'] = $format;
  return $dates;
}