1.20.x date.module | date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) |
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 ) )
File
- modules/
date/ date.module, line 301 - Defines date/time field types.
Code
function date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
$dates = array();
$timezone = date_default_timezone();
if (empty($timezone)) {
return $dates;
}
$granularity = date_granularity($field);
$settings = $display['settings'];
$field_name = $field['field_name'];
$format = date_formatter_format($formatter, $settings, $granularity, $langcode);
if (!isset($field['settings']['tz_handling']) || $field['settings']['tz_handling'] !== 'utc') {
$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']);
$process = date_process_values($field);
foreach ($process as $processed) {
if (empty($item[$processed])) {
$dates[$processed] = NULL;
}
else {
// Create a date object with a GMT timezone from the database value.
$dates[$processed] = array();
// Check to see if this date was already created by date_field_load().
if (isset($item['db'][$processed])) {
$date = $item['db'][$processed];
}
else {
$date = new BackdropDateTime($item[$processed], $timezone_db, $db_format);
$date->limitGranularity($field['settings']['granularity']);
}
$dates[$processed]['db']['object'] = $date;
$dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
date_timezone_set($date, timezone_open($timezone));
$dates[$processed]['local']['object'] = $date;
$dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
$dates[$processed]['local']['timezone'] = $timezone;
$dates[$processed]['local']['offset'] = date_offset_get($date);
// Format the date, special casing the 'interval' format which doesn't
// need to be processed.
$dates[$processed]['formatted'] = '';
$dates[$processed]['formatted_iso'] = date_format_date($date, 'custom', 'c');
if (is_object($date)) {
if ($format == 'format_interval') {
$dates[$processed]['interval'] = date_format_interval($date);
}
elseif ($format == 'format_calendar_day') {
$dates[$processed]['calendar_day'] = date_format_calendar_day($date);
}
elseif ($format == 'U' || $format == 'r' || $format == 'c') {
$dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
$dates[$processed]['formatted_date'] = date_format_date($date, 'custom', $format);
$dates[$processed]['formatted_time'] = '';
$dates[$processed]['formatted_timezone'] = '';
}
elseif (!empty($format)) {
$formats = _get_custom_date_format($date, $format);
$dates[$processed]['formatted'] = $formats['formatted'];
$dates[$processed]['formatted_date'] = $formats['date'];
$dates[$processed]['formatted_time'] = $formats['time'];
$dates[$processed]['formatted_timezone'] = $formats['zone'];
}
}
}
}
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_type' => $entity_type,
'entity' => $entity,
'langcode' => $langcode,
'item' => $item,
'display' => $display,
);
backdrop_alter('date_formatter_dates', $dates, $context);
$dates['format'] = $format;
return $dates;
}