date.field.inc 19 KB

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