date.api.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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. '#date_repeat_widget' => str_replace('_repeat', '', $instance['widget']['type']),
  329. '#date_repeat_collapsed' => $instance['widget']['settings']['repeat_collapsed'],
  330. '#date_flexible' => 0,
  331. '#weight' => $instance['widget']['weight'] + .4,
  332. );
  333. }
  334. }
  335. /**
  336. * Alter the date_timezone widget element.
  337. *
  338. * @param array $element
  339. * An associative array containing the properties of the date_select element.
  340. * @param array $form_state
  341. * A keyed array containing the current state of the form.
  342. * @param array $context
  343. * An associative array containing the following keys:
  344. * - form: Nested array of form elements that comprise the form.
  345. *
  346. * @see date_timezone_element_process()
  347. */
  348. function hook_date_timezone_process_alter(&$element, &$form_state, $context) {
  349. // @todo.
  350. }
  351. /**
  352. * Alter the date_year_range widget element.
  353. *
  354. * @param array $element
  355. * An associative array containing the properties of the date_select element.
  356. * @param array $form_state
  357. * A keyed array containing the current state of the form.
  358. * @param array $context
  359. * An associative array containing the following keys:
  360. * - form: Nested array of form elements that comprise the form.
  361. *
  362. * @see date_year_range_element_process()
  363. */
  364. function hook_date_year_range_process_alter(&$element, &$form_state, $context) {
  365. // @todo.
  366. }
  367. /**
  368. * Alter a date field settings form.
  369. *
  370. * @param array $form
  371. * Nested array of form elements that comprise the form.
  372. * @param array $context
  373. * An associative array containing the following keys:
  374. * - field: The $field array.
  375. * - instance: The $instance array.
  376. * - has_data: The value of $has_data.
  377. *
  378. * @see hook_field_settings_form()
  379. */
  380. function hook_date_field_settings_form_alter(&$form, $context) {
  381. $field = $context['field'];
  382. $instance = $context['instance'];
  383. $has_data = $context['has_data'];
  384. $form['repeat'] = array(
  385. '#type' => 'select',
  386. '#title' => t('Repeating date'),
  387. '#default_value' => $field['settings']['repeat'],
  388. '#options' => array(0 => t('No'), 1 => t('Yes')),
  389. '#attributes' => array('class' => array('container-inline')),
  390. '#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."),
  391. '#disabled' => $has_data,
  392. );
  393. }
  394. /**
  395. * Alter a date field instance settings form.
  396. *
  397. * @param array $form
  398. * Nested array of form elements that comprise the form.
  399. * @param array $context
  400. * An associative array containing the following keys:
  401. * - field: The $field array.
  402. * - instance: The $instance array.
  403. *
  404. * @see hook_field_instance_settings_form()
  405. */
  406. function hook_date_field_instance_settings_form_alter(&$form, $context) {
  407. $field = $context['field'];
  408. $instance = $context['instance'];
  409. $form['new_setting'] = array(
  410. '#type' => 'textfield',
  411. '#default_value' => '',
  412. '#title' => t('My new setting'),
  413. );
  414. }
  415. /**
  416. * Alter a date field widget settings form.
  417. *
  418. * @param array $form
  419. * Nested array of form elements that comprise the form.
  420. * @param array $context
  421. * An associative array containing the following keys:
  422. * - field: The $field array.
  423. * - instance: The $instance array.
  424. *
  425. * @see hook_field_widget_settings_form()
  426. */
  427. function hook_date_field_widget_settings_form_alter(&$form, $context) {
  428. $field = $context['field'];
  429. $instance = $context['instance'];
  430. $form['new_setting'] = array(
  431. '#type' => 'textfield',
  432. '#default_value' => '',
  433. '#title' => t('My new setting'),
  434. );
  435. }
  436. /**
  437. * Alter a date field formatter settings form.
  438. *
  439. * @param array $form
  440. * Nested array of form elements that comprise the form.
  441. * @param array $form_state
  442. * A keyed array containing the current state of the form.
  443. * @param array $context
  444. * An associative array containing the following keys:
  445. * - field: The $field array.
  446. * - instance: The $instance array.
  447. * - view_mode: The formatter view mode.
  448. *
  449. * @see hook_field_formatter_settings_form()
  450. */
  451. function hook_date_field_formatter_settings_form_alter(&$form, &$form_state, $context) {
  452. $field = $context['field'];
  453. $instance = $context['instance'];
  454. $view_mode = $context['view_mode'];
  455. $display = $instance['display'][$view_mode];
  456. $formatter = $display['type'];
  457. if ($formatter == 'date_default') {
  458. $form['show_repeat_rule'] = array(
  459. '#title' => t('Repeat rule:'),
  460. '#type' => 'select',
  461. '#options' => array(
  462. 'show' => t('Show repeat rule'),
  463. 'hide' => t('Hide repeat rule')),
  464. '#default_value' => $settings['show_repeat_rule'],
  465. '#access' => $field['settings']['repeat'],
  466. '#weight' => 5,
  467. );
  468. }
  469. }
  470. /**
  471. * Alter a date field formatter settings summary.
  472. *
  473. * @param array $summary
  474. * An array of strings to be concatenated into a short summary of the
  475. * formatter settings.
  476. * @param array $context
  477. * An associative array containing the following keys:
  478. * - field: The $field array.
  479. * - instance: The $instance array.
  480. * - view_mode: The formatter view mode.
  481. *
  482. * @see hook_field_formatter_settings_summary()
  483. */
  484. function hook_date_field_formatter_settings_summary_alter(&$summary, $context) {
  485. $field = $context['field'];
  486. $instance = $context['instance'];
  487. $view_mode = $context['view_mode'];
  488. $display = $instance['display'][$view_mode];
  489. $formatter = $display['type'];
  490. $settings = $display['settings'];
  491. if (isset($settings['show_repeat_rule']) && !empty($field['settings']['repeat'])) {
  492. if ($settings['show_repeat_rule'] == 'show') {
  493. $summary[] = t('Show repeat rule');
  494. }
  495. else {
  496. $summary[] = t('Hide repeat rule');
  497. }
  498. }
  499. }