date.field.inc 19 KB

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