date.field.inc 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. <?php
  2. /**
  3. * @file
  4. * Field hooks to implement a date field.
  5. */
  6. /**
  7. * Implements hook_field_formatter_info().
  8. */
  9. function date_field_formatter_info() {
  10. $formatters = array(
  11. 'date_default' => array(
  12. 'label' => t('Date and time'),
  13. 'field types' => array('date', 'datestamp', 'datetime'),
  14. 'settings' => array(
  15. 'format_type' => 'long',
  16. 'multiple_number' => '',
  17. 'multiple_from' => '',
  18. 'multiple_to' => '',
  19. 'fromto' => 'both',
  20. 'show_remaining_days' => FALSE,
  21. ),
  22. ),
  23. 'format_interval' => array(
  24. 'label' => t('Time ago'),
  25. 'field types' => array('date', 'datestamp', 'datetime'),
  26. 'settings' => array(
  27. 'interval' => 2,
  28. 'interval_display' => 'time ago',
  29. 'use_end_date' => false,
  30. ),
  31. ),
  32. 'date_plain' => array(
  33. 'label' => t('Plain'),
  34. 'field types' => array('date', 'datestamp', 'datetime'),
  35. ),
  36. );
  37. return $formatters;
  38. }
  39. /**
  40. * Implements hook_field_formatter_settings_form().
  41. */
  42. function date_field_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  43. $display = $instance['display'][$view_mode];
  44. $formatter = $display['type'];
  45. module_load_include('inc', 'date', 'date_admin');
  46. switch ($formatter) {
  47. case 'format_interval':
  48. $form = date_interval_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
  49. break;
  50. default:
  51. $form = date_default_formatter_settings_form($field, $instance, $view_mode, $form, $form_state);
  52. break;
  53. }
  54. $context = array(
  55. 'field' => $field,
  56. 'instance' => $instance,
  57. 'view_mode' => $view_mode,
  58. );
  59. drupal_alter('date_field_formatter_settings_form', $form, $form_state, $context);
  60. return $form;
  61. }
  62. /**
  63. * Implements hook_field_formatter_settings_summary().
  64. */
  65. function date_field_formatter_settings_summary($field, $instance, $view_mode) {
  66. $display = $instance['display'][$view_mode];
  67. $formatter = $display['type'];
  68. module_load_include('inc', 'date', 'date_admin');
  69. switch ($formatter) {
  70. case 'format_interval':
  71. $summary = date_interval_formatter_settings_summary($field, $instance, $view_mode);
  72. break;
  73. default:
  74. $summary = date_default_formatter_settings_summary($field, $instance, $view_mode);
  75. break;
  76. }
  77. $context = array(
  78. 'field' => $field,
  79. 'instance' => $instance,
  80. 'view_mode' => $view_mode,
  81. );
  82. drupal_alter('date_field_formatter_settings_summary', $summary, $context);
  83. return implode('<br />', $summary);
  84. }
  85. /**
  86. * Implements hook_field_formatter_view().
  87. *
  88. * Useful values:
  89. *
  90. * $entity->date_id
  91. * If set, this will show only an individual date on a field with
  92. * multiple dates. The value should be a string that contains
  93. * the following values, separated with periods:
  94. * - module name of the module adding the item
  95. * - node nid
  96. * - field name
  97. * - delta value of the field to be displayed
  98. * - other information the module's custom theme might need
  99. *
  100. * Used by the calendar module and available for other uses.
  101. * example: 'date:217:field_date:3:test'
  102. *
  103. * $entity->date_repeat_show
  104. * If true, tells the theme to show all the computed values
  105. * of a repeating date. If not true or not set, only the
  106. * start date and the repeat rule will be displayed.
  107. */
  108. function date_field_formatter_view($entity_type, $entity, $field, $instance, $langcode, $items, $display) {
  109. $element = array();
  110. $settings = $display['settings'];
  111. $formatter = $display['type'];
  112. $variables = array(
  113. 'entity' => $entity,
  114. 'entity_type' => $entity_type,
  115. 'field' => $field,
  116. 'instance' => $instance,
  117. 'langcode' => $langcode,
  118. 'items' => $items,
  119. 'display' => $display,
  120. 'dates' => array(),
  121. 'attributes' => array(),
  122. 'rdf_mapping' => array(),
  123. 'add_rdf' => module_exists('rdf'),
  124. 'microdata' => array(),
  125. 'add_microdata' => module_exists('microdata'),
  126. );
  127. // If the microdata module is enabled, the microdata mapping will have been
  128. // passed in via the entity.
  129. if ($variables['add_microdata'] && isset($entity->microdata[$field['field_name']])) {
  130. $variables['microdata'] = $entity->microdata[$field['field_name']];
  131. }
  132. // If there is an RDf mapping for this date field, pass it down to the theme.
  133. $rdf_mapping = array();
  134. if (!empty($entity->rdf_mapping) && function_exists('rdf_rdfa_attributes')) {
  135. if (!empty($entity->rdf_mapping[$field['field_name']])) {
  136. $variables['rdf_mapping'] = $rdf_mapping = $entity->rdf_mapping[$field['field_name']];
  137. }
  138. }
  139. // Give other modules a chance to prepare the entity before formatting it.
  140. drupal_alter('date_formatter_pre_view', $entity, $variables);
  141. // See if we are only supposed to display a selected
  142. // item from multiple value date fields.
  143. $selected_deltas = array();
  144. if (!empty($entity->date_id)) {
  145. foreach ((array) $entity->date_id as $key => $id) {
  146. list($module, $nid, $field_name, $selected_delta, $other) = explode('.', $id . '.');
  147. if ($field_name == $field['field_name']) {
  148. $selected_deltas[] = $selected_delta;
  149. }
  150. }
  151. }
  152. switch ($display['type']) {
  153. case 'date_plain':
  154. foreach ($items as $delta => $item) {
  155. if (!empty($entity->date_id) && !in_array($delta, $selected_deltas)) {
  156. continue;
  157. }
  158. else {
  159. if (empty($item['value2']) || $item['value'] == $item['value2']) {
  160. $element[$delta] = array('#markup' => $item['value']);
  161. }
  162. else {
  163. $element[$delta] = array(
  164. '#markup' => t('!start-date to !end-date', array(
  165. '!start-date' => $item['value'],
  166. '!end-date' => $item['value2']
  167. )));
  168. }
  169. }
  170. }
  171. break;
  172. case 'format_interval':
  173. foreach ($items as $delta => $item) {
  174. if (!empty($entity->date_id) && !in_array($delta, $selected_deltas)) {
  175. continue;
  176. }
  177. else {
  178. $variables['delta'] = $delta;
  179. $variables['item'] = $item;
  180. $variables['dates'] = date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
  181. $variables['attributes'] = !empty($rdf_mapping) ? rdf_rdfa_attributes($rdf_mapping, $item['value']) : array();
  182. $element[$delta] = array('#markup' => theme('date_display_interval', $variables));
  183. }
  184. }
  185. break;
  186. default:
  187. foreach ($items as $delta => $item) {
  188. if (!empty($entity->date_id) && !in_array($delta, $selected_deltas)) {
  189. continue;
  190. }
  191. else {
  192. $variables['delta'] = $delta;
  193. $variables['item'] = $item;
  194. $variables['dates'] = date_formatter_process($formatter, $entity_type, $entity, $field, $instance, $langcode, $item, $display);
  195. $variables['attributes'] = !empty($rdf_mapping) ? rdf_rdfa_attributes($rdf_mapping, $item['value']) : array();
  196. $variables['show_remaining_days'] = isset($display['settings']['show_remaining_days']) ? $display['settings']['show_remaining_days'] : FALSE;
  197. $output = theme('date_display_combination', $variables);
  198. if (!empty($output)) {
  199. $element[$delta] = array('#markup' => $output);
  200. }
  201. }
  202. }
  203. break;
  204. }
  205. return $element;
  206. }
  207. /**
  208. * Implements hook_field_is_empty().
  209. */
  210. function date_field_is_empty($item, $field) {
  211. // Sometimes a $item is a date object.
  212. // Coming from repeating dates. Why??
  213. if (!is_array($item)) {
  214. return FALSE;
  215. }
  216. if (empty($item['value'])) {
  217. return TRUE;
  218. }
  219. elseif ($field['settings']['todate'] == 'required' && empty($item['value2'])) {
  220. return TRUE;
  221. }
  222. return FALSE;
  223. }
  224. /**
  225. * Implements hook_field_info().
  226. */
  227. function date_field_info() {
  228. $granularity = array('year', 'month', 'day', 'hour', 'minute');
  229. $settings = array(
  230. 'settings' => array(
  231. 'todate' => '',
  232. 'granularity' => drupal_map_assoc($granularity),
  233. 'tz_handling' => 'site',
  234. 'timezone_db' => 'UTC',
  235. ),
  236. 'instance_settings' => array(
  237. 'default_value' => 'now',
  238. 'default_value_code' => '',
  239. 'default_value2' => 'same',
  240. 'default_value_code2' => '',
  241. ),
  242. // Integrate with the Entity Metadata module.
  243. 'property_type' => 'date',
  244. 'property_callbacks' => array('date_entity_metadata_property_info_alter'),
  245. );
  246. return array(
  247. 'datetime' => array(
  248. 'label' => t('Date'),
  249. 'description' => t('Store a date in the database as a datetime field, recommended for complete dates and times that may need timezone conversion.'),
  250. 'default_widget' => 'date_select',
  251. 'default_formatter' => 'date_default',
  252. 'default_token_formatter' => 'date_plain',
  253. ) + $settings,
  254. 'date' => array(
  255. 'label' => t('Date (ISO format)'),
  256. 'description' => t('Store a date in the database as an ISO date, recommended for historical or partial dates.'),
  257. 'default_widget' => 'date_select',
  258. 'default_formatter' => 'date_default',
  259. 'default_token_formatter' => 'date_plain',
  260. ) + $settings,
  261. 'datestamp' => array(
  262. 'label' => t('Date (Unix timestamp)'),
  263. 'description' => t('Store a date in the database as a timestamp, deprecated format to support legacy data.'),
  264. 'default_widget' => 'date_select',
  265. 'default_formatter' => 'date_default',
  266. 'default_token_formatter' => 'date_plain',
  267. ) + $settings,
  268. );
  269. }
  270. /**
  271. * Implements hook_field_widget_info().
  272. */
  273. function date_field_widget_info() {
  274. $settings = array(
  275. 'settings' => array(
  276. 'input_format' => date_default_format('date_select'),
  277. 'input_format_custom' => '',
  278. 'increment' => 15,
  279. 'text_parts' => array(),
  280. 'year_range' => '-3:+3',
  281. 'label_position' => 'above',
  282. ),
  283. 'behaviors' => array(
  284. 'multiple values' => FIELD_BEHAVIOR_DEFAULT,
  285. 'default value' => FIELD_BEHAVIOR_NONE,
  286. ),
  287. );
  288. $info = array(
  289. 'date_select' => array(
  290. 'label' => t('Select list'),
  291. 'field types' => array('date', 'datestamp', 'datetime'),
  292. ) + $settings,
  293. 'date_text' => array(
  294. 'label' => t('Text field'),
  295. 'field types' => array('date', 'datestamp', 'datetime'),
  296. ) + $settings,
  297. );
  298. if (module_exists('date_popup')) {
  299. $info['date_popup'] = array(
  300. 'label' => t('Pop-up calendar'),
  301. 'field types' => array('date', 'datestamp', 'datetime'),
  302. ) + $settings;
  303. }
  304. // The date text widget should use an increment of 1.
  305. $info['date_text']['settings']['increment'] = 1;
  306. return $info;
  307. }
  308. /**
  309. * Implements hook_field_load().
  310. */
  311. function date_field_load($entity_type, $entities, $field, $instances, $langcode, &$items, $age) {
  312. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  313. $db_format = date_type_format($field['type']);
  314. $process = date_process_values($field);
  315. foreach ($entities as $id => $entity) {
  316. foreach ($items[$id] as $delta => &$item) {
  317. // If the file does not exist, mark the entire item as empty.
  318. if (is_array($item)) {
  319. $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  320. $item['timezone'] = date_get_timezone($field['settings']['tz_handling'], $timezone);
  321. $item['timezone_db'] = $timezone_db;
  322. $item['date_type'] = $field['type'];
  323. if (!empty($field['settings']['cache_enabled']) && ($delta < $field['settings']['cache_count'] || $field['settings']['cache_count'] == 0)) {
  324. foreach ($process as $processed) {
  325. if (!empty($item[$processed])) {
  326. $date = new DateObject($item[$processed], $item['timezone_db'], $db_format);
  327. $date->limitGranularity($field['settings']['granularity']);
  328. $item['db'][$processed] = $date;
  329. }
  330. }
  331. if (!empty($item['db']['value']) && empty($item['db']['value2'])) {
  332. $item['db']['value2'] = $item['db']['value'];
  333. }
  334. }
  335. }
  336. }
  337. }
  338. }
  339. /**
  340. * Implements hook_field_validate().
  341. */
  342. function date_field_validate($entity_type, $entity, $field, $instance, $langcode, $items, &$errors) {
  343. $field_name = $field['field_name'];
  344. $flexible = 0;
  345. // Don't try to validate if there were any errors before this point
  346. // since the element won't have been munged back into a date.
  347. if (!form_get_errors()) {
  348. foreach ($items as $delta => $item) {
  349. if (is_array($item) && isset($item['value'])) {
  350. $process = date_process_values($field, $instance);
  351. $date1 = new DateObject($item['value'], $item['timezone'], date_type_format($field['type']));
  352. if (count($process) == 1 || (empty($item['value2']) && $item['value2'] !== 0)) {
  353. $date2 = clone($date1);
  354. }
  355. else {
  356. $date2 = new DateObject($item['value2'], $item['timezone'], date_type_format($field['type']));
  357. }
  358. $valid1 = $date1->validGranularity($field['settings']['granularity'], $flexible);
  359. $valid2 = $date2->validGranularity($field['settings']['granularity'], $flexible);
  360. foreach ($process as $processed) {
  361. if ($processed == 'value' && $field['settings']['todate'] && !$valid1 && $valid2) {
  362. $errors[$field['field_name']][$langcode][$delta][] = array(
  363. 'error' => 'value',
  364. 'message' => t("A 'Start date' date is required for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])),
  365. );
  366. }
  367. if ($processed == 'value2' && $field['settings']['todate'] == 'required' && ($instance['required'] && $valid1 && !$valid2)) {
  368. $errors[$field['field_name']][$langcode][$delta][] = array(
  369. 'error' => 'value2',
  370. 'message' => t("An 'End date' is required for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])),
  371. );
  372. }
  373. }
  374. }
  375. }
  376. }
  377. }
  378. /**
  379. * Implements hook_field_insert().
  380. */
  381. function date_field_presave($entity_type, $entity, $field, $instance, $langcode, &$items) {
  382. $field_name = $field['field_name'];
  383. if (empty($items)) {
  384. return;
  385. }
  386. // Add some information needed to interpret token values.
  387. $values = $items;
  388. foreach ($values as $delta => $item) {
  389. $timezone = isset($item['timezone']) ? $item['timezone'] : '';
  390. if (is_array($item)) {
  391. $items[$delta]['timezone'] = date_get_timezone($field['settings']['tz_handling'], $timezone);
  392. $items[$delta]['timezone_db'] = date_get_timezone_db($field['settings']['tz_handling']);
  393. $items[$delta]['date_type'] = $field['type'];
  394. }
  395. }
  396. $entity->{$field['field_name']}[$langcode] = $items;
  397. }
  398. /**
  399. * Implements hook_field_insert().
  400. */
  401. function date_field_insert($entity_type, $entity, $field, $instance, $langcode, &$items) {
  402. $context = array(
  403. 'entity_type' => $entity_type,
  404. 'entity' => $entity,
  405. 'field' => $field,
  406. 'instance' => $instance,
  407. 'langcode' => $langcode,
  408. );
  409. drupal_alter('date_field_insert', $items, $context);
  410. }
  411. /**
  412. * Implements hook_field_update().
  413. */
  414. function date_field_update($entity_type, $entity, $field, $instance, $langcode, &$items) {
  415. $context = array(
  416. 'entity_type' => $entity_type,
  417. 'entity' => $entity,
  418. 'field' => $field,
  419. 'instance' => $instance,
  420. 'langcode' => $langcode,
  421. );
  422. drupal_alter('date_field_update', $items, $context);
  423. }
  424. /**
  425. * Implements hook_field_instance_settings_form().
  426. *
  427. * Wrapper functions for date administration, included only when processing
  428. * field settings.
  429. */
  430. function date_field_instance_settings_form($field, $instance) {
  431. module_load_include('inc', 'date', 'date_admin');
  432. return _date_field_instance_settings_form($field, $instance);
  433. }
  434. /**
  435. * Form validation handler for _date_field_instance_settings_form().
  436. */
  437. function date_field_instance_settings_form_validate(&$form, &$form_state) {
  438. module_load_include('inc', 'date', 'date_admin');
  439. return _date_field_instance_settings_form_validate($form, $form_state);
  440. }
  441. /**
  442. * Implements hook_field_widget_settings_form().
  443. */
  444. function date_field_widget_settings_form($field, $instance) {
  445. module_load_include('inc', 'date', 'date_admin');
  446. return _date_field_widget_settings_form($field, $instance);
  447. }
  448. /**
  449. * Form validation handler for _date_field_widget_settings_form().
  450. */
  451. function date_field_widget_settings_form_validate(&$form, &$form_state) {
  452. module_load_include('inc', 'date', 'date_admin');
  453. return _date_field_widget_settings_form_validate($form, $form_state);
  454. }
  455. /**
  456. * Implements hook_field_settings_form().
  457. */
  458. function date_field_settings_form($field, $instance, $has_data) {
  459. module_load_include('inc', 'date', 'date_admin');
  460. return _date_field_settings_form($field, $instance, $has_data);
  461. }
  462. /**
  463. * Form validation handler for _date_field_settings_form().
  464. */
  465. function date_field_settings_validate(&$form, &$form_state) {
  466. module_load_include('inc', 'date', 'date_admin');
  467. return _date_field_settings_validate($form, $form_state);
  468. }
  469. /**
  470. * Implements hook_content_migrate_field_alter().
  471. *
  472. * Use this to tweak the conversion of field settings from the D6 style to the
  473. * D7 style for specific situations not handled by basic conversion, as when
  474. * field types or settings are changed.
  475. *
  476. * $field_value['widget_type'] is available to see what widget type was
  477. * originally used.
  478. */
  479. function date_content_migrate_field_alter(&$field_value, $instance_value) {
  480. switch ($field_value['module']) {
  481. case 'date':
  482. // Those settings do not exist anymore, or have been moved to the instance
  483. // level.
  484. unset($field_value['settings']['default_format']);
  485. unset($field_value['settings']['repeat_collapsed']);
  486. break;
  487. }
  488. }
  489. /**
  490. * Implements hook_content_migrate_instance_alter().
  491. *
  492. * Use this to tweak the conversion of instance or widget settings from the D6
  493. * style to the D7 style for specific situations not handled by basic
  494. * conversion, as when formatter or widget names or settings are changed.
  495. */
  496. function date_content_migrate_instance_alter(&$instance_value, $field_value) {
  497. switch ($instance_value['widget']['module']) {
  498. case 'date':
  499. // Some settings have been moved from field to instance.
  500. $instance_value['widget']['settings']['repeat_collapsed'] = $field_value['settings']['repeat_collapsed'];
  501. // Some settings were moved from widget settings to instance settings.
  502. $instance_value['settings']['default_value'] = $instance_value['default_value'];
  503. unset($instance_value['default_value']);
  504. $instance_value['settings']['default_value_code'] = $instance_value['widget']['settings']['default_value_code'];
  505. unset($instance_value['widget']['settings']['default_value_code']);
  506. $instance_value['settings']['default_value2'] = $instance_value['widget']['settings']['default_value2'];
  507. unset($instance_value['widget']['settings']['default_value2']);
  508. $instance_value['settings']['default_value_code2'] = $instance_value['widget']['settings']['default_value_code2'];
  509. unset($instance_value['widget']['settings']['default_value_code2']);
  510. // We need to retrieve formatter settings from the variables and store
  511. // them in the instance.
  512. foreach ($instance_value['display'] as $context => &$display) {
  513. if ($display['type'] != 'format_interval') {
  514. $old_settings = date_old_formatter_get_settings($instance_value['field_name'], $instance_value['bundle'], $context);
  515. $display['settings'] = array_merge($display['settings'], $old_settings);
  516. // If the formatter was the 'default', then use the old
  517. // 'default_format' field property.
  518. $format = ($display['type'] == 'default') ? $field_value['settings']['default_format'] : $display['type'];
  519. $display['settings']['format_type'] = $format;
  520. $display['type'] = 'date_default';
  521. }
  522. }
  523. break;
  524. }
  525. }
  526. /**
  527. * Constructs an array of old formatter settings.
  528. */
  529. function date_old_formatter_get_settings($field_name, $type_name, $context) {
  530. $options = array();
  531. $value = 'date:' . $type_name . ':' . $context . ':' . $field_name;
  532. $options['show_repeat_rule'] = variable_get($value . '_show_repeat_rule', 'show');
  533. $options['multiple_number'] = variable_get($value . '_multiple_number', '');
  534. $options['multiple_from'] = variable_get($value . '_multiple_from', '');
  535. $options['multiple_to'] = variable_get($value . '_multiple_to', '');
  536. $options['fromto'] = variable_get($value . '_fromto', 'both');
  537. return $options;
  538. }