date.field.inc 19 KB

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