date.api.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  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, &$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(&$dates, $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. $all_day2 = theme('date_all_day', array(
  89. 'field' => $field,
  90. 'instance' => $instance,
  91. 'which' => 'date2',
  92. 'date1' => $date1,
  93. 'date2' => $date2,
  94. 'format' => $format,
  95. 'entity_type' => $entity_type,
  96. 'entity' => $entity));
  97. $dates['value']['formatted_time'] = theme('date_all_day_label');
  98. $dates['value2']['formatted_time'] = theme('date_all_day_label');
  99. $dates['value']['formatted'] = $all_day1;
  100. $dates['value2']['formatted'] = $all_day2;
  101. }
  102. }
  103. /**
  104. * Alter the date_text element before the rest of the validation is run.
  105. *
  106. * @param array $element
  107. * The $element array.
  108. * @param array $form_state
  109. * A keyed array containing the current state of the form.
  110. * @param array $input
  111. * The array of input values to be validated.
  112. */
  113. function hook_date_text_pre_validate_alter(&$element, &$form_state, &$input) {
  114. // Let Date module massage the format for all day values so they will pass
  115. // validation. The All day flag, if used, actually exists on the parent
  116. // element.
  117. date_all_day_value($element, $form_state);
  118. }
  119. /**
  120. * Alter the date_select element before the rest of the validation is run.
  121. *
  122. * @param array $element
  123. * The $element array.
  124. * @param array $form_state
  125. * A keyed array containing the current state of the form.
  126. * @param array $input
  127. * The array of input values to be validated.
  128. */
  129. function hook_date_select_pre_validate_alter(&$element, &$form_state, &$input) {
  130. // Let Date module massage the format for all day values so they will pass
  131. // validation. The All day flag, if used, actually exists on the parent
  132. // element.
  133. date_all_day_value($element, $form_state);
  134. }
  135. /**
  136. * Alter the date_popup element before the rest of the validation is run.
  137. *
  138. * @param array $element
  139. * The $element array.
  140. * @param array $form_state
  141. * A keyed array containing the current state of the form.
  142. * @param array $input
  143. * The array of input values to be validated.
  144. */
  145. function hook_date_popup_pre_validate_alter(&$element, &$form_state, &$input) {
  146. // Let Date module massage the format for all day values so they will pass
  147. // validation. The All day flag, if used, actually exists on the parent
  148. // element.
  149. date_all_day_value($element, $form_state);
  150. }
  151. /**
  152. * Alter the date_combo element before the rest of the validation is run.
  153. *
  154. * @param array $element
  155. * The $element array.
  156. * @param array $form_state
  157. * A keyed array containing the current state of the form.
  158. * @param array $context
  159. * An associative array containing the following keys:
  160. * - field: The $field array.
  161. * - instance: The $instance array.
  162. * - item: The $item array.
  163. *
  164. * @see date_combo_element_process()
  165. */
  166. function hook_date_combo_pre_validate_alter(&$element, &$form_state, $context) {
  167. if (!empty($context['item']['all_day'])) {
  168. $field = $context['field'];
  169. // If we have an all day flag on this date and the time is empty, change the
  170. // format to match the input value so we don't get validation errors.
  171. $element['#date_is_all_day'] = TRUE;
  172. $element['value']['#date_format'] = date_part_format('date', $element['value']['#date_format']);
  173. if (!empty($field['settings']['todate'])) {
  174. $element['value2']['#date_format'] = date_part_format('date', $element['value2']['#date_format']);
  175. }
  176. }
  177. }
  178. /**
  179. * Alter the local start date objects created by the date_combo validation.
  180. *
  181. * This is called before the objects are converted back to the database timezone
  182. * and stored.
  183. *
  184. * @param object $date
  185. * The $date object.
  186. * @param array $form_state
  187. * A keyed array containing the current state of the form.
  188. * @param array $context
  189. * An associative array containing the following keys:
  190. * - field: The $field array.
  191. * - instance: The $instance array.
  192. * - item: The $item array.
  193. * - element: The $element array.
  194. */
  195. function hook_date_combo_validate_date_start_alter(&$date, &$form_state, $context) {
  196. // If this is an 'All day' value, set the time to midnight.
  197. if (!empty($context['element']['#date_is_all_day'])) {
  198. $date->setTime(0, 0, 0);
  199. }
  200. }
  201. /**
  202. * Alter the local end date objects created by the date_combo validation.
  203. *
  204. * This is called before the objects are converted back to the database timezone
  205. * and stored.
  206. *
  207. * @param object $date
  208. * The $date object.
  209. * @param array $form_state
  210. * A keyed array containing the current state of the form.
  211. * @param array $context
  212. * An associative array containing the following keys:
  213. * - field: The $field array.
  214. * - instance: The $instance array.
  215. * - item: The $item array.
  216. * - element: The $element array.
  217. */
  218. function hook_date_combo_validate_date_end_alter(&$date, &$form_state, $context) {
  219. // If this is an 'All day' value, set the time to midnight.
  220. if (!empty($context['element']['#date_is_all_day'])) {
  221. $date->setTime(0, 0, 0);
  222. }
  223. }
  224. /**
  225. * Alter the date_text widget element.
  226. *
  227. * @param array $element
  228. * An associative array containing the properties of the date_text element.
  229. * @param array $form_state
  230. * A keyed array containing the current state of the form.
  231. * @param array $context
  232. * An associative array containing the following keys:
  233. * - form: Nested array of form elements that comprise the form.
  234. *
  235. * @see date_text_element_process()
  236. */
  237. function hook_date_text_process_alter(&$element, &$form_state, $context) {
  238. $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
  239. if ($all_day_id != '') {
  240. // All Day handling on text dates works only if the user leaves the time out
  241. // of the input value. There is no element to hide or show.
  242. }
  243. }
  244. /**
  245. * Alter the date_select widget element.
  246. *
  247. * @param array $element
  248. * An associative array containing the properties of the date_select element.
  249. * @param array $form_state
  250. * A keyed array containing the current state of the form.
  251. * @param array $context
  252. * An associative array containing the following keys:
  253. * - form: Nested array of form elements that comprise the form.
  254. *
  255. * @see date_select_element_process()
  256. */
  257. function hook_date_select_process_alter(&$element, &$form_state, $context) {
  258. // Hide or show the element in reaction to the all_day status for the element.
  259. $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
  260. if ($all_day_id != '') {
  261. foreach (array('hour', 'minute', 'second', 'ampm') as $field) {
  262. if (array_key_exists($field, $element)) {
  263. $element[$field]['#states'] = array(
  264. 'visible' => array(
  265. 'input[name="' . $all_day_id . '"]' => array('checked' => FALSE),
  266. ),
  267. );
  268. }
  269. }
  270. }
  271. }
  272. /**
  273. * Alter the date_popup widget element.
  274. *
  275. * @param array $element
  276. * An associative array containing the properties of the date_popup element.
  277. * @param array $form_state
  278. * A keyed array containing the current state of the form.
  279. * @param array $context
  280. * An associative array containing the following keys:
  281. * - form: Nested array of form elements that comprise the form.
  282. *
  283. * @see date_popup_element_process()
  284. */
  285. function hook_date_popup_process_alter(&$element, &$form_state, $context) {
  286. // Hide or show the element in reaction to the all_day status for the element.
  287. $all_day_id = !empty($element['#date_all_day_id']) ? $element['#date_all_day_id'] : '';
  288. if ($all_day_id != '' && array_key_exists('time', $element)) {
  289. $element['time']['#states'] = array(
  290. 'visible' => array(
  291. 'input[name="' . $all_day_id . '"]' => array('checked' => FALSE),
  292. ),
  293. );
  294. }
  295. }
  296. /**
  297. * Alter the date_combo element after the Date module is finished with it.
  298. *
  299. * @param array $element
  300. * The $element array.
  301. * @param array $form_state
  302. * A keyed array containing the current state of the form.
  303. * @param array $context
  304. * An associative array containing the following keys:
  305. * - field: The $field array.
  306. * - instance: The $instance array.
  307. * - form: Nested array of form elements that comprise the form.
  308. */
  309. function hook_date_combo_process_alter(&$element, &$form_state, $context) {
  310. $field = $context['field'];
  311. $instance = $context['instance'];
  312. $field_name = $element['#field_name'];
  313. $delta = $element['#delta'];
  314. // Add a date repeat form element, if needed.
  315. // We delayed until this point so we don't bother adding it to hidden fields.
  316. if (date_is_repeat_field($field, $instance)) {
  317. $item = $element['#value'];
  318. $element['rrule'] = array(
  319. '#type' => 'date_repeat_rrule',
  320. '#theme_wrappers' => array('date_repeat_rrule'),
  321. '#default_value' => isset($item['rrule']) ? $item['rrule'] : '',
  322. '#date_timezone' => $element['#date_timezone'],
  323. '#date_format' => date_limit_format(date_input_format($element, $field, $instance), $field['settings']['granularity']),
  324. '#date_text_parts' => (array) $instance['widget']['settings']['text_parts'],
  325. '#date_increment' => $instance['widget']['settings']['increment'],
  326. '#date_year_range' => $instance['widget']['settings']['year_range'],
  327. '#date_label_position' => $instance['widget']['settings']['label_position'],
  328. '#prev_value' => isset($item['value']) ? $item['value'] : '',
  329. '#prev_value2' => isset($item['value2']) ? $item['value2'] : '',
  330. '#prev_rrule' => isset($item['rrule']) ? $item['rrule'] : '',
  331. '#date_repeat_widget' => str_replace('_repeat', '', $instance['widget']['type']),
  332. '#date_repeat_collapsed' => $instance['widget']['settings']['repeat_collapsed'],
  333. '#date_flexible' => 0,
  334. '#weight' => $instance['widget']['weight'] + .4,
  335. );
  336. }
  337. }
  338. /**
  339. * Alter the date_timezone widget element.
  340. *
  341. * @param array $element
  342. * An associative array containing the properties of the date_select element.
  343. * @param array $form_state
  344. * A keyed array containing the current state of the form.
  345. * @param array $context
  346. * An associative array containing the following keys:
  347. * - form: Nested array of form elements that comprise the form.
  348. *
  349. * @see date_timezone_element_process()
  350. */
  351. function hook_date_timezone_process_alter(&$element, &$form_state, $context) {
  352. // @todo.
  353. }
  354. /**
  355. * Alter the date_year_range widget element.
  356. *
  357. * @param array $element
  358. * An associative array containing the properties of the date_select element.
  359. * @param array $form_state
  360. * A keyed array containing the current state of the form.
  361. * @param array $context
  362. * An associative array containing the following keys:
  363. * - form: Nested array of form elements that comprise the form.
  364. *
  365. * @see date_year_range_element_process()
  366. */
  367. function hook_date_year_range_process_alter(&$element, &$form_state, $context) {
  368. // @todo.
  369. }
  370. /**
  371. * Alter a date field settings form.
  372. *
  373. * @param array $form
  374. * Nested array of form elements that comprise the form.
  375. * @param array $context
  376. * An associative array containing the following keys:
  377. * - field: The $field array.
  378. * - instance: The $instance array.
  379. * - has_data: The value of $has_data.
  380. *
  381. * @see hook_field_settings_form()
  382. */
  383. function hook_date_field_settings_form_alter(&$form, $context) {
  384. $field = $context['field'];
  385. $instance = $context['instance'];
  386. $has_data = $context['has_data'];
  387. $form['repeat'] = array(
  388. '#type' => 'select',
  389. '#title' => t('Repeating date'),
  390. '#default_value' => $field['settings']['repeat'],
  391. '#options' => array(0 => t('No'), 1 => t('Yes')),
  392. '#attributes' => array('class' => array('container-inline')),
  393. '#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."),
  394. '#disabled' => $has_data,
  395. );
  396. }
  397. /**
  398. * Alter a date field instance settings form.
  399. *
  400. * @param array $form
  401. * Nested array of form elements that comprise the form.
  402. * @param array $context
  403. * An associative array containing the following keys:
  404. * - field: The $field array.
  405. * - instance: The $instance array.
  406. *
  407. * @see hook_field_instance_settings_form()
  408. */
  409. function hook_date_field_instance_settings_form_alter(&$form, $context) {
  410. $field = $context['field'];
  411. $instance = $context['instance'];
  412. $form['new_setting'] = array(
  413. '#type' => 'textfield',
  414. '#default_value' => '',
  415. '#title' => t('My new setting'),
  416. );
  417. }
  418. /**
  419. * Alter a date field widget settings form.
  420. *
  421. * @param array $form
  422. * Nested array of form elements that comprise the form.
  423. * @param array $context
  424. * An associative array containing the following keys:
  425. * - field: The $field array.
  426. * - instance: The $instance array.
  427. *
  428. * @see hook_field_widget_settings_form()
  429. */
  430. function hook_date_field_widget_settings_form_alter(&$form, $context) {
  431. $field = $context['field'];
  432. $instance = $context['instance'];
  433. $form['new_setting'] = array(
  434. '#type' => 'textfield',
  435. '#default_value' => '',
  436. '#title' => t('My new setting'),
  437. );
  438. }
  439. /**
  440. * Alter a date field formatter settings form.
  441. *
  442. * @param array $form
  443. * Nested array of form elements that comprise the form.
  444. * @param array $form_state
  445. * A keyed array containing the current state of the form.
  446. * @param array $context
  447. * An associative array containing the following keys:
  448. * - field: The $field array.
  449. * - instance: The $instance array.
  450. * - view_mode: The formatter view mode.
  451. *
  452. * @see hook_field_formatter_settings_form()
  453. */
  454. function hook_date_field_formatter_settings_form_alter(&$form, &$form_state, $context) {
  455. $field = $context['field'];
  456. $instance = $context['instance'];
  457. $view_mode = $context['view_mode'];
  458. $display = $instance['display'][$view_mode];
  459. $formatter = $display['type'];
  460. if ($formatter == 'date_default') {
  461. $form['show_repeat_rule'] = array(
  462. '#title' => t('Repeat rule:'),
  463. '#type' => 'select',
  464. '#options' => array(
  465. 'show' => t('Show repeat rule'),
  466. 'hide' => t('Hide repeat rule')),
  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(&$summary, $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. }