date.api.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. <?php
  2. /**
  3. * @file
  4. * Hooks provided by the Date module.
  5. */
  6. /**
  7. * Alter the default value for a date argument.
  8. *
  9. * @param object $argument
  10. * The argument object.
  11. * @param string $value
  12. * The default value created by the argument handler.
  13. */
  14. function hook_date_default_argument_alter(&$argument, &$value) {
  15. $style_options = $style_options = $argument->view->display_handler->get_option('style_options');
  16. if (!empty($style_options['track_date'])) {
  17. $default_date = date_now();
  18. $value = $default_date->format($argument->arg_format);
  19. }
  20. }
  21. /**
  22. * Alter the entity before formatting it.
  23. *
  24. * @param object $entity
  25. * The entity object being viewed.
  26. * @param array $variables
  27. * The variables passed to the formatter.
  28. * - entity: The $entity object.
  29. * - entity_type: The $entity_type.
  30. * - field: The $field array.
  31. * - instance: The $instance array.
  32. * - langcode: The $langcode.
  33. * - items: The $items array.
  34. * - display: The $display array.
  35. * - dates: The processed dates array, empty at this point.
  36. * - attributes: The attributes array, empty at this point.
  37. * - rdf_mapping: The RDF mapping array.
  38. * - add_rdf: If module_exists('rdf').
  39. */
  40. function hook_date_formatter_pre_view_alter(&$entity, array &$variables) {
  41. if (!empty($entity->view)) {
  42. $field = $variables['field'];
  43. $date_id = 'date_id_' . $field['field_name'];
  44. $date_delta = 'date_delta_' . $field['field_name'];
  45. $date_item = $entity->view->result[$entity->view->row_index];
  46. if (!empty($date_item->$date_id)) {
  47. $entity->date_id = 'date.' . $date_item->$date_id . '.' . $field['field_name'] . '.' . $date_item->$date_delta . '.0';
  48. }
  49. }
  50. }
  51. /**
  52. * Alter the dates array created by date_formatter_process().
  53. *
  54. * @param array $dates
  55. * The $dates array created by the Date module.
  56. * @param array $context
  57. * An associative array containing the following keys:
  58. * - field: The $field array.
  59. * - instance: The $instance array.
  60. * - format: The string $format.
  61. * - entity_type: The $entity_type.
  62. * - entity: The $entity object.
  63. * - langcode: The string $langcode.
  64. * - item: The $item array.
  65. * - display: The $display array.
  66. */
  67. function hook_date_formatter_dates_alter(array &$dates, array $context) {
  68. $field = $context['field'];
  69. $instance = $context['instance'];
  70. $format = $context['format'];
  71. $entity_type = $context['entity_type'];
  72. $entity = $context['entity'];
  73. $date1 = $dates['value']['local']['object'];
  74. $date2 = $dates['value2']['local']['object'];
  75. $is_all_day = date_all_day_field($field, $instance, $date1, $date2);
  76. $all_day1 = '';
  77. $all_day2 = '';
  78. if ($format != 'format_interval' && $is_all_day) {
  79. $all_day1 = theme('date_all_day', array(
  80. 'field' => $field,
  81. 'instance' => $instance,
  82. 'which' => 'date1',
  83. 'date1' => $date1,
  84. 'date2' => $date2,
  85. 'format' => $format,
  86. 'entity_type' => $entity_type,
  87. 'entity' => $entity,
  88. ));
  89. $all_day2 = theme('date_all_day', array(
  90. 'field' => $field,
  91. 'instance' => $instance,
  92. 'which' => 'date2',
  93. 'date1' => $date1,
  94. 'date2' => $date2,
  95. 'format' => $format,
  96. 'entity_type' => $entity_type,
  97. 'entity' => $entity,
  98. ));
  99. $dates['value']['formatted_time'] = theme('date_all_day_label');
  100. $dates['value2']['formatted_time'] = theme('date_all_day_label');
  101. $dates['value']['formatted'] = $all_day1;
  102. $dates['value2']['formatted'] = $all_day2;
  103. }
  104. }
  105. /**
  106. * Alter the date_text element before the rest of the validation is run.
  107. *
  108. * @param array $element
  109. * The $element array.
  110. * @param array $form_state
  111. * A keyed array containing the current state of the form.
  112. * @param array $input
  113. * The array of input values to be validated.
  114. */
  115. function hook_date_text_pre_validate_alter(array &$element, array &$form_state, array &$input) {
  116. // Let Date module massage the format for all day values so they will pass
  117. // validation. The All day flag, if used, actually exists on the parent
  118. // element.
  119. date_all_day_value($element, $form_state);
  120. }
  121. /**
  122. * Alter the date_select element before the rest of the validation is run.
  123. *
  124. * @param array $element
  125. * The $element array.
  126. * @param array $form_state
  127. * A keyed array containing the current state of the form.
  128. * @param array $input
  129. * The array of input values to be validated.
  130. */
  131. function hook_date_select_pre_validate_alter(array &$element, array &$form_state, array &$input) {
  132. // Let Date module massage the format for all day values so they will pass
  133. // validation. The All day flag, if used, actually exists on the parent
  134. // element.
  135. date_all_day_value($element, $form_state);
  136. }
  137. /**
  138. * Alter the date_popup element before the rest of the validation is run.
  139. *
  140. * @param array $element
  141. * The $element array.
  142. * @param array $form_state
  143. * A keyed array containing the current state of the form.
  144. * @param array $input
  145. * The array of input values to be validated.
  146. */
  147. function hook_date_popup_pre_validate_alter(array &$element, array &$form_state, array &$input) {
  148. // Let Date module massage the format for all day values so they will pass
  149. // validation. The All day flag, if used, actually exists on the parent
  150. // element.
  151. date_all_day_value($element, $form_state);
  152. }
  153. /**
  154. * Alter the date_combo element before the rest of the validation is run.
  155. *
  156. * @param array $element
  157. * The $element array.
  158. * @param array $form_state
  159. * A keyed array containing the current state of the form.
  160. * @param array $context
  161. * An associative array containing the following keys:
  162. * - field: The $field array.
  163. * - instance: The $instance array.
  164. * - item: The $item array.
  165. *
  166. * @see date_combo_element_process()
  167. */
  168. function hook_date_combo_pre_validate_alter(array &$element, array &$form_state, array $context) {
  169. if (!empty($context['item']['all_day'])) {
  170. $field = $context['field'];
  171. // If we have an all day flag on this date and the time is empty, change the
  172. // format to match the input value so we don't get validation errors.
  173. $element['#date_is_all_day'] = TRUE;
  174. $element['value']['#date_format'] = date_part_format('date', $element['value']['#date_format']);
  175. if (!empty($field['settings']['todate'])) {
  176. $element['value2']['#date_format'] = date_part_format('date', $element['value2']['#date_format']);
  177. }
  178. }
  179. }
  180. /**
  181. * Alter the local start date objects created by the date_combo validation.
  182. *
  183. * This is called before the objects are converted back to the database timezone
  184. * and stored.
  185. *
  186. * @param object $date
  187. * The $date object.
  188. * @param array $form_state
  189. * A keyed array containing the current state of the form.
  190. * @param array $context
  191. * An associative array containing the following keys:
  192. * - field: The $field array.
  193. * - instance: The $instance array.
  194. * - item: The $item array.
  195. * - element: The $element array.
  196. */
  197. function hook_date_combo_validate_date_start_alter(object &$date, array &$form_state, array $context) {
  198. // If this is an 'All day' value, set the time to midnight.
  199. if (!empty($context['element']['#date_is_all_day'])) {
  200. $date->setTime(0, 0, 0);
  201. }
  202. }
  203. /**
  204. * Alter the local end date objects created by the date_combo validation.
  205. *
  206. * This is called before the objects are converted back to the database timezone
  207. * and stored.
  208. *
  209. * @param object $date
  210. * The $date object.
  211. * @param array $form_state
  212. * A keyed array containing the current state of the form.
  213. * @param array $context
  214. * An associative array containing the following keys:
  215. * - field: The $field array.
  216. * - instance: The $instance array.
  217. * - item: The $item array.
  218. * - element: The $element array.
  219. */
  220. function hook_date_combo_validate_date_end_alter(object &$date, array &$form_state, array $context) {
  221. // If this is an 'All day' value, set the time to midnight.
  222. if (!empty($context['element']['#date_is_all_day'])) {
  223. $date->setTime(0, 0, 0);
  224. }
  225. }
  226. /**
  227. * Alter the date_text widget element.
  228. *
  229. * @param array $element
  230. * An associative array containing the properties of the date_text element.
  231. * @param array $form_state
  232. * A keyed array containing the current state of the form.
  233. * @param array $context
  234. * An associative array containing the following keys:
  235. * - form: Nested array of form elements that comprise the form.
  236. *
  237. * @see date_text_element_process()
  238. */
  239. function hook_date_text_process_alter(array &$element, array &$form_state, array $context) {
  240. $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
  241. if ($all_day_id != '') {
  242. // All Day handling on text dates works only if the user leaves the time out
  243. // of the input value. There is no element to hide or show.
  244. }
  245. }
  246. /**
  247. * Alter the date_select widget element.
  248. *
  249. * @param array $element
  250. * An associative array containing the properties of the date_select element.
  251. * @param array $form_state
  252. * A keyed array containing the current state of the form.
  253. * @param array $context
  254. * An associative array containing the following keys:
  255. * - form: Nested array of form elements that comprise the form.
  256. *
  257. * @see date_select_element_process()
  258. */
  259. function hook_date_select_process_alter(array &$element, array &$form_state, array $context) {
  260. // Hide or show the element in reaction to the all_day status for the element.
  261. $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
  262. if ($all_day_id != '') {
  263. foreach (array('hour', 'minute', 'second', 'ampm') as $field) {
  264. if (array_key_exists($field, $element)) {
  265. $element[$field]['#states'] = array(
  266. 'visible' => array(
  267. 'input[name="' . $all_day_id . '"]' => array('checked' => FALSE),
  268. ),
  269. );
  270. }
  271. }
  272. }
  273. }
  274. /**
  275. * Alter the date_popup widget element.
  276. *
  277. * @param array $element
  278. * An associative array containing the properties of the date_popup element.
  279. * @param array $form_state
  280. * A keyed array containing the current state of the form.
  281. * @param array $context
  282. * An associative array containing the following keys:
  283. * - form: Nested array of form elements that comprise the form.
  284. *
  285. * @see date_popup_element_process()
  286. */
  287. function hook_date_popup_process_alter(array &$element, array &$form_state, array $context) {
  288. // Hide or show the element in reaction to the all_day status for the element.
  289. $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
  290. if ($all_day_id != '' && array_key_exists('time', $element)) {
  291. $element['time']['#states'] = array(
  292. 'visible' => array(
  293. 'input[name="' . $all_day_id . '"]' => array('checked' => FALSE),
  294. ),
  295. );
  296. }
  297. }
  298. /**
  299. * Alter the date_combo element after the Date module is finished with it.
  300. *
  301. * @param array $element
  302. * The $element array.
  303. * @param array $form_state
  304. * A keyed array containing the current state of the form.
  305. * @param array $context
  306. * An associative array containing the following keys:
  307. * - field: The $field array.
  308. * - instance: The $instance array.
  309. * - form: Nested array of form elements that comprise the form.
  310. */
  311. function hook_date_combo_process_alter(array &$element, array &$form_state, array $context) {
  312. $field = $context['field'];
  313. $instance = $context['instance'];
  314. $field_name = $element['#field_name'];
  315. $delta = $element['#delta'];
  316. // Add a date repeat form element, if needed.
  317. // We delayed until this point so we don't bother adding it to hidden fields.
  318. if (date_is_repeat_field($field, $instance)) {
  319. $item = $element['#value'];
  320. $element['rrule'] = array(
  321. '#type' => 'date_repeat_rrule',
  322. '#theme_wrappers' => array('date_repeat_rrule'),
  323. '#default_value' => isset($item['rrule']) ? $item['rrule'] : '',
  324. '#date_timezone' => $element['#date_timezone'],
  325. '#date_format' => date_limit_format(date_input_format($element, $field, $instance), $field['settings']['granularity']),
  326. '#date_text_parts' => (array) $instance['widget']['settings']['text_parts'],
  327. '#date_increment' => $instance['widget']['settings']['increment'],
  328. '#date_year_range' => $instance['widget']['settings']['year_range'],
  329. '#date_label_position' => $instance['widget']['settings']['label_position'],
  330. '#date_repeat_widget' => str_replace('_repeat', '', $instance['widget']['type']),
  331. '#date_repeat_collapsed' => $instance['widget']['settings']['repeat_collapsed'],
  332. '#date_flexible' => 0,
  333. '#weight' => $instance['widget']['weight'] + .4,
  334. );
  335. }
  336. }
  337. /**
  338. * Alter the date_timezone widget element.
  339. *
  340. * @param array $element
  341. * An associative array containing the properties of the date_select element.
  342. * @param array $form_state
  343. * A keyed array containing the current state of the form.
  344. * @param array $context
  345. * An associative array containing the following keys:
  346. * - form: Nested array of form elements that comprise the form.
  347. *
  348. * @see date_timezone_element_process()
  349. */
  350. function hook_date_timezone_process_alter(array &$element, array &$form_state, array $context) {
  351. // @todo Document this.
  352. }
  353. /**
  354. * Alter the date_year_range widget element.
  355. *
  356. * @param array $element
  357. * An associative array containing the properties of the date_select element.
  358. * @param array $form_state
  359. * A keyed array containing the current state of the form.
  360. * @param array $context
  361. * An associative array containing the following keys:
  362. * - form: Nested array of form elements that comprise the form.
  363. *
  364. * @see date_year_range_element_process()
  365. */
  366. function hook_date_year_range_process_alter(array &$element, array &$form_state, array $context) {
  367. // @todo Document this.
  368. }
  369. /**
  370. * Alter a date field settings form.
  371. *
  372. * @param array $form
  373. * Nested array of form elements that comprise the form.
  374. * @param array $context
  375. * An associative array containing the following keys:
  376. * - field: The $field array.
  377. * - instance: The $instance array.
  378. * - has_data: The value of $has_data.
  379. *
  380. * @see hook_field_settings_form()
  381. */
  382. function hook_date_field_settings_form_alter(array &$form, array $context) {
  383. $field = $context['field'];
  384. $instance = $context['instance'];
  385. $has_data = $context['has_data'];
  386. $form['repeat'] = array(
  387. '#type' => 'select',
  388. '#title' => t('Repeating date'),
  389. '#default_value' => $field['settings']['repeat'],
  390. '#options' => array(0 => t('No'), 1 => t('Yes')),
  391. '#attributes' => array('class' => array('container-inline')),
  392. '#description' => t("Repeating dates use an 'Unlimited' number of values. Instead of the 'Add more' button, they include a form to select when and how often the date should repeat."),
  393. '#disabled' => $has_data,
  394. );
  395. }
  396. /**
  397. * Alter a date field instance settings form.
  398. *
  399. * @param array $form
  400. * Nested array of form elements that comprise the form.
  401. * @param array $context
  402. * An associative array containing the following keys:
  403. * - field: The $field array.
  404. * - instance: The $instance array.
  405. *
  406. * @see hook_field_instance_settings_form()
  407. */
  408. function hook_date_field_instance_settings_form_alter(array &$form, array $context) {
  409. $field = $context['field'];
  410. $instance = $context['instance'];
  411. $form['new_setting'] = array(
  412. '#type' => 'textfield',
  413. '#default_value' => '',
  414. '#title' => t('My new setting'),
  415. );
  416. }
  417. /**
  418. * Alter a date field widget settings form.
  419. *
  420. * @param array $form
  421. * Nested array of form elements that comprise the form.
  422. * @param array $context
  423. * An associative array containing the following keys:
  424. * - field: The $field array.
  425. * - instance: The $instance array.
  426. *
  427. * @see hook_field_widget_settings_form()
  428. */
  429. function hook_date_field_widget_settings_form_alter(array &$form, array $context) {
  430. $field = $context['field'];
  431. $instance = $context['instance'];
  432. $form['new_setting'] = array(
  433. '#type' => 'textfield',
  434. '#default_value' => '',
  435. '#title' => t('My new setting'),
  436. );
  437. }
  438. /**
  439. * Alter a date field formatter settings form.
  440. *
  441. * @param array $form
  442. * Nested array of form elements that comprise the form.
  443. * @param array $form_state
  444. * A keyed array containing the current state of the form.
  445. * @param array $context
  446. * An associative array containing the following keys:
  447. * - field: The $field array.
  448. * - instance: The $instance array.
  449. * - view_mode: The formatter view mode.
  450. *
  451. * @see hook_field_formatter_settings_form()
  452. */
  453. function hook_date_field_formatter_settings_form_alter(array &$form, array &$form_state, array $context) {
  454. $field = $context['field'];
  455. $instance = $context['instance'];
  456. $view_mode = $context['view_mode'];
  457. $display = $instance['display'][$view_mode];
  458. $formatter = $display['type'];
  459. if ($formatter == 'date_default') {
  460. $form['show_repeat_rule'] = array(
  461. '#title' => t('Repeat rule:'),
  462. '#type' => 'select',
  463. '#options' => array(
  464. 'show' => t('Show repeat rule'),
  465. 'hide' => t('Hide repeat rule'),
  466. ),
  467. '#default_value' => $settings['show_repeat_rule'],
  468. '#access' => $field['settings']['repeat'],
  469. '#weight' => 5,
  470. );
  471. }
  472. }
  473. /**
  474. * Alter a date field formatter settings summary.
  475. *
  476. * @param array $summary
  477. * An array of strings to be concatenated into a short summary of the
  478. * formatter settings.
  479. * @param array $context
  480. * An associative array containing the following keys:
  481. * - field: The $field array.
  482. * - instance: The $instance array.
  483. * - view_mode: The formatter view mode.
  484. *
  485. * @see hook_field_formatter_settings_summary()
  486. */
  487. function hook_date_field_formatter_settings_summary_alter(array &$summary, array $context) {
  488. $field = $context['field'];
  489. $instance = $context['instance'];
  490. $view_mode = $context['view_mode'];
  491. $display = $instance['display'][$view_mode];
  492. $formatter = $display['type'];
  493. $settings = $display['settings'];
  494. if (isset($settings['show_repeat_rule']) && !empty($field['settings']['repeat'])) {
  495. if ($settings['show_repeat_rule'] == 'show') {
  496. $summary[] = t('Show repeat rule');
  497. }
  498. else {
  499. $summary[] = t('Hide repeat rule');
  500. }
  501. }
  502. }