date.module 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. <?php
  2. /**
  3. * @file
  4. * Defines date/time field types.
  5. */
  6. module_load_include('theme', 'date', 'date');
  7. module_load_include('inc', 'date', 'date.field');
  8. module_load_include('inc', 'date', 'date_elements');
  9. /**
  10. * Helper function to figure out the bundle name for an entity.
  11. */
  12. function date_get_entity_bundle($entity_type, $entity) {
  13. switch ($entity_type) {
  14. case 'field_collection_item':
  15. $bundle = $entity->field_name;
  16. break;
  17. default:
  18. $bundle = field_extract_bundle($entity_type, $entity);
  19. break;
  20. }
  21. // If there is no bundle name, field_info() uses the entity name as the bundle
  22. // name in its arrays.
  23. if (empty($bundle)) {
  24. $bundle = $entity_type;
  25. }
  26. return $bundle;
  27. }
  28. /**
  29. * Gets the default date format for the given field widget.
  30. */
  31. function date_default_format($type) {
  32. // Example input formats must show all possible date parts, so add seconds.
  33. $default_format = str_replace('i', 'i:s', variable_get('date_format_short', 'm/d/Y - H:i'));
  34. return $default_format;
  35. }
  36. /**
  37. * Wrapper function around each of the widget types for creating a date object.
  38. */
  39. function date_input_date($field, $instance, $element, $input) {
  40. switch ($instance['widget']['type']) {
  41. case 'date_text':
  42. $function = 'date_text_input_date';
  43. break;
  44. case 'date_popup':
  45. $function = 'date_popup_input_date';
  46. break;
  47. default:
  48. $function = 'date_select_input_date';
  49. }
  50. return $function($element, $input);
  51. }
  52. /**
  53. * Implements hook_theme().
  54. */
  55. function date_theme() {
  56. $path = drupal_get_path('module', 'date');
  57. module_load_include('theme', 'date', 'date');
  58. $base = array(
  59. 'file' => 'date.theme',
  60. 'path' => "$path",
  61. );
  62. $themes = array(
  63. 'date_combo' => $base + array('render element' => 'element'),
  64. 'date_text_parts' => $base + array('render element' => 'element'),
  65. 'date' => $base + array('render element' => 'element'),
  66. 'date_display_single' => $base + array(
  67. 'variables' => array(
  68. 'date' => NULL,
  69. 'timezone' => NULL,
  70. 'dates' => NULL,
  71. 'attributes' => array(),
  72. 'rdf_mapping' => NULL,
  73. 'add_rdf' => NULL,
  74. ),
  75. ),
  76. 'date_display_range' => $base + array(
  77. 'variables' => array(
  78. 'date1' => NULL,
  79. 'date2' => NULL,
  80. 'timezone' => NULL,
  81. 'dates' => NULL,
  82. // HTML attributes that will be applied to both the start and end dates
  83. // (unless overridden).
  84. 'attributes' => array(),
  85. // HTML attributes that will be applied to the start date only.
  86. 'attributes_start' => array(),
  87. // HTML attributes that will be applied to the end date only.
  88. 'attributes_end' => array(),
  89. 'rdf_mapping' => NULL,
  90. 'add_rdf' => NULL,
  91. )),
  92. 'date_display_combination' => $base + array(
  93. 'variables' => array(
  94. 'entity_type' => NULL,
  95. 'entity' => NULL,
  96. 'field' => NULL,
  97. 'instance' => NULL,
  98. 'langcode' => NULL,
  99. 'item' => NULL,
  100. 'delta' => NULL,
  101. 'display' => NULL,
  102. 'dates' => NULL,
  103. 'attributes' => array(),
  104. 'rdf_mapping' => NULL,
  105. 'add_rdf' => NULL,
  106. ),
  107. ),
  108. 'date_display_interval' => $base + array(
  109. 'variables' => array(
  110. 'entity_type' => NULL,
  111. 'entity' => NULL,
  112. 'field' => NULL,
  113. 'instance' => NULL,
  114. 'langcode' => NULL,
  115. 'item' => NULL,
  116. 'delta' => NULL,
  117. 'display' => NULL,
  118. 'dates' => NULL,
  119. 'attributes' => array(),
  120. 'rdf_mapping' => NULL,
  121. 'add_rdf' => NULL,
  122. ),
  123. ),
  124. );
  125. return $themes;
  126. }
  127. /**
  128. * Implements hook_element_info().
  129. *
  130. * date_combo will create a 'start' and optional 'end' date, along with
  131. * an optional 'timezone' column for date-specific timezones. Each
  132. * 'start' and 'end' date will be constructed from date_select or date_text.
  133. */
  134. function date_element_info() {
  135. $type = array();
  136. $type['date_combo'] = array(
  137. '#input' => TRUE,
  138. '#delta' => 0,
  139. '#columns' => array('value', 'value2', 'timezone', 'offset', 'offset2'),
  140. '#process' => array('date_combo_element_process'),
  141. '#element_validate' => array('date_combo_validate'),
  142. '#theme_wrappers' => array('date_combo'),
  143. );
  144. if (module_exists('ctools')) {
  145. $type['date_combo']['#pre_render'] = array('ctools_dependent_pre_render');
  146. }
  147. return $type;
  148. }
  149. /**
  150. * Helper function for creating formatted date arrays from a formatter.
  151. *
  152. * Use the Date API to get an object representation of a date field.
  153. *
  154. * @param string $formatter
  155. * The date formatter.
  156. * @param string $entity_type
  157. * The entity_type for the instance
  158. * @param object $entity
  159. * The entity object.
  160. * @param array $field
  161. * The field info array.
  162. * @param array $instance
  163. * The field instance array.
  164. * @param string $langcode
  165. * The language code used by this field.
  166. * @param array $item
  167. * An entity field item, like $entity->myfield[0].
  168. * @param array $display
  169. * The instance display settings.
  170. *
  171. * @return array
  172. * An array that holds the Start and End date objects.
  173. * Each date object looks like:
  174. * date [value] => array (
  175. * [db] => array ( // the value stored in the database
  176. * [object] => the datetime object
  177. * [datetime] => 2007-02-15 20:00:00
  178. * )
  179. * [local] => array ( // the local representation of that value
  180. * [object] => the datetime object
  181. * [datetime] => 2007-02-15 14:00:00
  182. * [timezone] => US/Central
  183. * [offset] => -21600
  184. * )
  185. * )
  186. */
  187. function date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
  188. $dates = array();
  189. $timezone = date_default_timezone();
  190. if (empty($timezone)) {
  191. return $dates;
  192. }
  193. $granularity = date_granularity($field);
  194. $settings = $display['settings'];
  195. $field_name = $field['field_name'];
  196. $format = date_formatter_format($formatter, $settings, $granularity, $langcode);
  197. $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  198. $timezone = date_get_timezone($field['settings']['tz_handling'], $timezone);
  199. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  200. $db_format = date_type_format($field['type']);
  201. $process = date_process_values($field);
  202. foreach ($process as $processed) {
  203. if (empty($item[$processed])) {
  204. $dates[$processed] = NULL;
  205. }
  206. else {
  207. // Create a date object with a GMT timezone from the database value.
  208. $dates[$processed] = array();
  209. // Check to see if this date was already created by date_field_load().
  210. if (isset($item['db'][$processed])) {
  211. $date = $item['db'][$processed];
  212. }
  213. else {
  214. $date = new DateObject($item[$processed], $timezone_db, $db_format);
  215. $date->limitGranularity($field['settings']['granularity']);
  216. }
  217. $dates[$processed]['db']['object'] = $date;
  218. $dates[$processed]['db']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
  219. date_timezone_set($date, timezone_open($timezone));
  220. $dates[$processed]['local']['object'] = $date;
  221. $dates[$processed]['local']['datetime'] = date_format($date, DATE_FORMAT_DATETIME);
  222. $dates[$processed]['local']['timezone'] = $timezone;
  223. $dates[$processed]['local']['offset'] = date_offset_get($date);
  224. // Format the date, special casing the 'interval' format which doesn't
  225. // need to be processed.
  226. $dates[$processed]['formatted'] = '';
  227. $dates[$processed]['formatted_iso'] = date_format_date($date, 'custom', 'c');
  228. if (is_object($date)) {
  229. if ($format == 'format_interval') {
  230. $dates[$processed]['interval'] = date_format_interval($date);
  231. }
  232. elseif ($format == 'format_calendar_day') {
  233. $dates[$processed]['calendar_day'] = date_format_calendar_day($date);
  234. }
  235. elseif ($format == 'U') {
  236. $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
  237. $dates[$processed]['formatted_date'] = date_format_date($date, 'custom', $format);
  238. $dates[$processed]['formatted_time'] = '';
  239. $dates[$processed]['formatted_timezone'] = '';
  240. }
  241. elseif (!empty($format)) {
  242. $dates[$processed]['formatted'] = date_format_date($date, 'custom', $format);
  243. $dates[$processed]['formatted_date'] = date_format_date($date, 'custom', date_limit_format($format, array('year', 'month', 'day')));
  244. $dates[$processed]['formatted_time'] = date_format_date($date, 'custom', date_limit_format($format, array('hour', 'minute', 'second')));
  245. $dates[$processed]['formatted_timezone'] = date_format_date($date, 'custom', date_limit_format($format, array('timezone')));
  246. }
  247. }
  248. }
  249. }
  250. if (empty($dates['value2'])) {
  251. $dates['value2'] = $dates['value'];
  252. }
  253. // Allow other modules to alter the date values.
  254. $context = array(
  255. 'field' => $field,
  256. 'instance' => $instance,
  257. 'format' => $format,
  258. 'entity_type' => $entity_type,
  259. 'entity' => $entity,
  260. 'langcode' => $langcode,
  261. 'item' => $item,
  262. 'display' => $display,
  263. );
  264. drupal_alter('date_formatter_dates', $dates, $context);
  265. $dates['format'] = $format;
  266. return $dates;
  267. }
  268. /**
  269. * Retrieves the granularity for a field.
  270. *
  271. * $field['settings']['granularity'] will contain an array like
  272. * ('hour' => 'hour', 'month' => 0) where the values turned on return their own
  273. * names and the values turned off return a zero need to reconfigure this into
  274. * simple array of the turned on values
  275. *
  276. * @param array $field
  277. * The field array.
  278. */
  279. function date_granularity($field) {
  280. if (!is_array($field) || !is_array($field['settings']['granularity'])) {
  281. $field['settings']['granularity'] = drupal_map_assoc(array('year', 'month', 'day'));
  282. }
  283. return array_values(array_filter($field['settings']['granularity']));
  284. }
  285. /**
  286. * Helper function to create an array of the date values in a
  287. * field that need to be processed.
  288. */
  289. function date_process_values($field) {
  290. return $field['settings']['todate'] ? array('value', 'value2') : array('value');
  291. }
  292. /**
  293. * Implements hook_form_FORM_ID_alter() for field_ui_field_edit_form().
  294. */
  295. function date_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
  296. $field = $form['#field'];
  297. $instance = $form['#instance'];
  298. if (!in_array($field['type'], array('date', 'datetime', 'datestamp'))) {
  299. return;
  300. }
  301. // Reorganize the instance settings and widget settings sections into a more
  302. // intuitive combined fieldset.
  303. $form['instance']['defaults'] = array(
  304. '#type' => 'fieldset',
  305. '#title' => t('More settings and values'),
  306. '#collapsible' => TRUE,
  307. '#collapsed' => TRUE,
  308. );
  309. $form['instance']['date_format'] = array(
  310. '#type' => 'fieldset',
  311. '#title' => t('Date entry'),
  312. '#collapsible' => TRUE,
  313. '#collapsed' => FALSE,
  314. );
  315. $form['instance']['default_values'] = array(
  316. '#type' => 'fieldset',
  317. '#title' => t('Default values'),
  318. '#collapsible' => TRUE,
  319. '#collapsed' => FALSE,
  320. );
  321. $form['instance']['years_back_and_forward'] = array(
  322. '#type' => 'fieldset',
  323. '#title' => t('Starting and ending year'),
  324. '#collapsible' => TRUE,
  325. '#collapsed' => FALSE,
  326. );
  327. $form['instance']['#pre_render'][] = 'date_field_ui_field_edit_form_pre_render';
  328. }
  329. /**
  330. * Rearrange form elements into fieldsets for presentation only.
  331. */
  332. function date_field_ui_field_edit_form_pre_render($form) {
  333. foreach ($form as $name => $element) {
  334. if (is_array($element) && isset($element['#fieldset'])) {
  335. $fieldset = $element['#fieldset'];
  336. $form[$fieldset][$name] = $element;
  337. unset($form[$name]);
  338. }
  339. }
  340. foreach (array('date_format', 'default_values', 'years_back_and_forward') as $name) {
  341. if (element_children($form[$name])) {
  342. // Force the items in the fieldset to be resorted now that the instance
  343. // and widget settings are combined.
  344. $form[$name]['#sorted'] = FALSE;
  345. $form['defaults'][$name] = $form[$name];
  346. }
  347. unset($form[$name]);
  348. }
  349. return $form;
  350. }
  351. /**
  352. * Implements hook_field_widget_error().
  353. */
  354. function date_field_widget_error($element, $error, $form, &$form_state) {
  355. form_error($element[$error['error']], $error['message']);
  356. }
  357. /**
  358. * Retrieve a date format string from formatter settings.
  359. */
  360. function date_formatter_format($formatter, $settings, $granularity = array(), $langcode = NULL) {
  361. $format_type = !empty($settings['format_type']) ? $settings['format_type'] : 'format_interval';
  362. switch ($formatter) {
  363. case 'format_interval':
  364. return 'format_interval';
  365. break;
  366. case 'date_plain':
  367. return 'date_plain';
  368. break;
  369. default:
  370. $format = date_format_type_format($format_type, $langcode);
  371. break;
  372. }
  373. // A selected format might include timezone information.
  374. array_push($granularity, 'timezone');
  375. return date_limit_format($format, $granularity);
  376. }
  377. /**
  378. * Helper function to get the right format for a format type.
  379. * Checks for locale-based format first.
  380. */
  381. function date_format_type_format($format_type, $langcode = NULL) {
  382. $static = &drupal_static(__FUNCTION__);
  383. if (!isset($static[$langcode][$format_type])) {
  384. $format = system_date_format_locale($langcode, $format_type);
  385. // If locale enabled and $format_type inexistent in {date_format_locale}
  386. // we receive (due to inconsistency of core api) an array of all (other)
  387. // formats available for $langcode in locale table.
  388. // However there's no guarantee that the key $format_type exists.
  389. // See http://drupal.org/node/1302358.
  390. if (!is_string($format)) {
  391. // If the configuration page at admin/config/regional/date-time was
  392. // never saved, the default core date format variables
  393. // ('date_format_short', 'date_format_medium', and 'date_format_long')
  394. // will not be stored in the database, so we have to define their
  395. // expected defaults here.
  396. switch ($format_type) {
  397. case 'short':
  398. $default = 'm/d/Y - H:i';
  399. break;
  400. case 'long':
  401. $default = 'l, F j, Y - H:i';
  402. break;
  403. // If it's not one of the core date types and isn't stored in the
  404. // database, we'll fall back on using the same default format as the
  405. // 'medium' type.
  406. case 'medium':
  407. default:
  408. // @todo: If a non-core module provides a date type and does not
  409. // variable_set() a default for it, the default assumed here may
  410. // not be correct (since the default format used by 'medium' may
  411. // not even be one of the allowed formats for the date type in
  412. // question). To fix this properly, we should really call
  413. // system_get_date_formats($format_type) and take the first
  414. // format from that list as the default. However, this function
  415. // is called often (on many different page requests), so calling
  416. // system_get_date_formats() from here would be a performance hit
  417. // since that function writes several records to the database
  418. // during each page request that calls it.
  419. $default = 'D, m/d/Y - H:i';
  420. break;
  421. }
  422. $format = variable_get('date_format_' . $format_type, $default);
  423. }
  424. $static[$langcode][$format_type] = $format;
  425. }
  426. return $static[$langcode][$format_type];
  427. }
  428. /**
  429. * Helper function to adapt entity date fields to formatter settings.
  430. */
  431. function date_prepare_entity($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display) {
  432. // If there are options to limit multiple values,
  433. // alter the entity values to match.
  434. $field_name = $field['field_name'];
  435. $options = $display['settings'];
  436. $max_count = $options['multiple_number'];
  437. // If no results should be shown, empty the values and return.
  438. if (is_numeric($max_count) && $max_count == 0) {
  439. $entity->{$field_name} = array();
  440. return $entity;
  441. }
  442. // Otherwise removed values that should not be displayed.
  443. if (!empty($options['multiple_from']) || !empty($options['multiple_to']) || !empty($max_count)) {
  444. $format = date_type_format($field['type']);
  445. include_once drupal_get_path('module', 'date_api') . '/date_api_sql.inc';
  446. $date_handler = new date_sql_handler($field);
  447. $arg0 = !empty($options['multiple_from']) ? $date_handler->arg_replace($options['multiple_from']) : variable_get('date_min_year', 100) . '-01-01T00:00:00';
  448. $arg1 = !empty($options['multiple_to']) ? $date_handler->arg_replace($options['multiple_to']) : variable_get('date_max_year', 4000) . '-12-31T23:59:59';
  449. if (!empty($arg0) && !empty($arg1)) {
  450. $arg = $arg0 . '--' . $arg1;
  451. }
  452. elseif (!empty($arg0)) {
  453. $arg = $arg0;
  454. }
  455. elseif (!empty($arg1)) {
  456. $arg = $arg1;
  457. }
  458. if (!empty($arg)) {
  459. $range = $date_handler->arg_range($arg);
  460. $start = date_format($range[0], $format);
  461. $end = date_format($range[1], $format);
  462. // Empty out values we don't want to see.
  463. $count = 0;
  464. foreach ($entity->{$field_name}[$langcode] as $delta => $value) {
  465. if (!empty($entity->date_repeat_show_all)) {
  466. break;
  467. }
  468. elseif ((!empty($max_count) && is_numeric($max_count) && $count >= $max_count) ||
  469. (!empty($value['value']) && $value['value'] < $start) ||
  470. (!empty($value['value2']) && $value['value2'] > $end)) {
  471. unset($entity->{$field_name}[$langcode][$delta]);
  472. }
  473. else {
  474. $count++;
  475. }
  476. }
  477. }
  478. }
  479. return $entity;
  480. }
  481. /**
  482. * Callback to alter the property info of date fields.
  483. *
  484. * @see date_field_info()
  485. */
  486. function date_entity_metadata_property_info_alter(&$info, $entity_type, $field, $instance, $field_type) {
  487. $name = $field['field_name'];
  488. $property = &$info[$entity_type]['bundles'][$instance['bundle']]['properties'][$name];
  489. if ($field['type'] != 'datestamp' || $field['settings']['timezone_db'] != 'UTC') {
  490. // Add a getter callback to convert the date into the right format.
  491. $property['getter callback'] = 'date_entity_metadata_field_getter';
  492. $property['setter callback'] = 'date_entity_metadata_field_setter';
  493. unset($property['query callback']);
  494. }
  495. if (!empty($field['settings']['todate'])) {
  496. // Define a simple data structure containing both dates.
  497. $property['type'] = ($field['cardinality'] != 1) ? 'list<struct>' : 'struct';
  498. $property['getter callback'] = 'entity_metadata_field_verbatim_get';
  499. $property['setter callback'] = 'entity_metadata_field_verbatim_set';
  500. $property['property info'] = array(
  501. 'value' => array(
  502. 'type' => 'date',
  503. 'label' => t('Start date'),
  504. 'getter callback' => 'date_entity_metadata_struct_getter',
  505. 'setter callback' => 'date_entity_metadata_struct_setter',
  506. // The getter and setter callbacks for 'value' and 'value2'
  507. // will not provide the field name as $name, we'll add it to $info.
  508. 'field_name' => $field['field_name'],
  509. ),
  510. 'value2' => array(
  511. 'type' => 'date',
  512. 'label' => t('End date'),
  513. 'getter callback' => 'date_entity_metadata_struct_getter',
  514. 'setter callback' => 'date_entity_metadata_struct_setter',
  515. // The getter and setter callbacks for 'value' and 'value2'
  516. // will not provide the field name as $name, we'll add it to $info.
  517. 'field_name' => $field['field_name'],
  518. ),
  519. 'duration' => array(
  520. 'type' => 'duration',
  521. 'label' => t('Duration'),
  522. 'desription' => t('The duration of the time period given by the dates.'),
  523. 'getter callback' => 'date_entity_metadata_duration_getter',
  524. // No setter callback for duration.
  525. // The getter callback for duration will not provide the field name
  526. // as $name, we'll add it to $info.
  527. 'field_name' => $field['field_name'],
  528. ),
  529. );
  530. unset($property['query callback']);
  531. }
  532. }
  533. /**
  534. * Getter callback to return date values as datestamp in UTC from the field.
  535. */
  536. function date_entity_metadata_field_getter($entity, array $options, $name, $entity_type, &$context) {
  537. $return = entity_metadata_field_verbatim_get($entity, $options, $name, $entity_type, $context);
  538. $items = ($context['field']['cardinality'] == 1) ? array($return) : $return;
  539. foreach ($items as $key => $item) {
  540. $items[$key] = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $context);
  541. }
  542. return ($context['field']['cardinality'] == 1) ? $items[0] : $items;
  543. }
  544. /**
  545. * Getter callback to return date values as datestamp in UTC.
  546. */
  547. function date_entity_metadata_struct_getter($item, array $options, $name, $type, $info) {
  548. $value = trim($item[$name]);
  549. if (empty($value)) {
  550. return NULL;
  551. }
  552. $timezone_db = !empty($item['timezone_db']) ? $item['timezone_db'] : 'UTC';
  553. $date = new DateObject($value, $timezone_db);
  554. return !empty($date) ? date_format_date($date, 'custom', 'U') : NULL;
  555. }
  556. /**
  557. * Getter callback to return the duration of the time period given by the dates.
  558. */
  559. function date_entity_metadata_duration_getter($item, array $options, $name, $type, $info) {
  560. $value = date_entity_metadata_struct_getter($item, $options, 'value', 'struct', $info);
  561. $value2 = date_entity_metadata_struct_getter($item, $options, 'value2', 'struct', $info);
  562. if ($value && $value2) {
  563. return $value2 - $value;
  564. }
  565. }
  566. /**
  567. * Callback for setting field property values.
  568. *
  569. * Based on entity_metadata_field_property_set(), the original property setter,
  570. * adapted to transform non-timestamp date values to timestamps.
  571. */
  572. function date_entity_metadata_field_setter(&$entity, $name, $value, $langcode, $entity_type, $info) {
  573. $field = field_info_field($name);
  574. if (!isset($langcode)) {
  575. // Try to figure out the default language used by the entity.
  576. // @todo: Update once http://drupal.org/node/1260640 has been fixed.
  577. $langcode = isset($entity->language) ? $entity->language : LANGUAGE_NONE;
  578. }
  579. $values = $field['cardinality'] == 1 ? array($value) : (array) $value;
  580. $items = array();
  581. foreach ($values as $delta => $value) {
  582. // Make use of the struct setter to convert the date back to a timestamp.
  583. $info['field_name'] = $name;
  584. date_entity_metadata_struct_setter($items[$delta], 'value', $value, $langcode, 'struct', $info);
  585. }
  586. $entity->{$name}[$langcode] = $items;
  587. // Empty the static field language cache, so the field system picks up any
  588. // possible new languages.
  589. drupal_static_reset('field_language');
  590. }
  591. /**
  592. * Callback for setting an individual field value if a to-date may be there too.
  593. * Based on entity_property_verbatim_set().
  594. *
  595. * The passed in unix timestamp (UTC) is converted to the right value and
  596. * format dependent on the field.
  597. *
  598. * $name is either 'value' or 'value2'.
  599. */
  600. function date_entity_metadata_struct_setter(&$item, $name, $value, $langcode, $type, $info) {
  601. if (!isset($value)) {
  602. $item[$name] = NULL;
  603. }
  604. else {
  605. $field = field_info_field($info['field_name']);
  606. $format = date_type_format($field['type']);
  607. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  608. $date = new DateObject($value, 'UTC');
  609. if ($timezone_db != 'UTC') {
  610. date_timezone_set($date, timezone_open($timezone_db));
  611. }
  612. $item[$name] = $date->format($format);
  613. }
  614. }
  615. /**
  616. * Duplicate functionality of what is now date_all_day_field() in
  617. * the Date All Day module. Copy left here to avoid breaking other
  618. * modules that use this function.
  619. *
  620. * DEPRECATED!, will be removed at some time in the future.
  621. */
  622. function date_field_all_day($field, $instance, $date1, $date2 = NULL) {
  623. if (empty($date1) || !is_object($date1)) {
  624. return FALSE;
  625. }
  626. elseif (!date_has_time($field['settings']['granularity'])) {
  627. return TRUE;
  628. }
  629. if (empty($date2)) {
  630. $date2 = $date1;
  631. }
  632. $granularity = date_granularity_precision($field['settings']['granularity']);
  633. $increment = isset($instance['widget']['settings']['increment']) ? $instance['widget']['settings']['increment'] : 1;
  634. return date_is_all_day(date_format($date1, DATE_FORMAT_DATETIME), date_format($date2, DATE_FORMAT_DATETIME), $granularity, $increment);
  635. }
  636. /**
  637. * Generates a Date API SQL handler for the given date field.
  638. *
  639. * The handler will be set up to make the correct timezone adjustments
  640. * for the field settings.
  641. *
  642. * @param array $field
  643. * The $field array.
  644. * @param string $compare_tz
  645. * The timezone used for comparison values in the SQL.
  646. *
  647. * DEPRECATED!, will be removed at some time in the future.
  648. */
  649. function date_field_get_sql_handler($field, $compare_tz = NULL) {
  650. module_load_include('inc', 'date_api', 'date_api_sql');
  651. $db_info = date_api_database_info($field);
  652. // Create a DateAPI SQL handler class for this field type.
  653. $handler = new date_sql_handler($field['type']);
  654. // If this date field stores a timezone in the DB, tell the handler about it.
  655. if ($field['settings']['tz_handling'] == 'date') {
  656. $handler->db_timezone_field = $db_info['columns']['timezone']['column'];
  657. }
  658. else {
  659. $handler->db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
  660. }
  661. if (empty($compare_tz)) {
  662. $compare_tz = date_get_timezone($field['settings']['tz_handling']);
  663. }
  664. $handler->local_timezone = $compare_tz;
  665. // Now that the handler is properly initialized, force the DB
  666. // to use UTC so no timezone conversions get added to things like
  667. // NOW() or FROM_UNIXTIME().
  668. $handler->set_db_timezone();
  669. return $handler;
  670. }
  671. /**
  672. * Implements hook_field_widget_properties_alter().
  673. *
  674. * Alters the widget properties of a field instance before it gets displayed.
  675. * Used here to flag new entities so we can later tell if they need default values.
  676. */
  677. function date_field_widget_properties_alter(&$widget, $context) {
  678. if (in_array($widget['type'], array('date_select', 'date_text', 'date_popup'))) {
  679. $entity_type = $context['entity_type'];
  680. $entity = $context['entity'];
  681. $info = entity_get_info($entity_type);
  682. $id = $info['entity keys']['id'];
  683. $widget['is_new']= FALSE;
  684. if (empty($entity->$id)) {
  685. $widget['is_new'] = TRUE;
  686. }
  687. }
  688. }