date_admin.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. <?php
  2. /**
  3. * @file
  4. * Date administration code.
  5. */
  6. /**
  7. * Settings for the default formatter.
  8. */
  9. function date_default_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  10. $display = $instance['display'][$view_mode];
  11. $settings = $display['settings'];
  12. $formatter = $display['type'];
  13. $form = array();
  14. $date_formats = date_format_type_options();
  15. $form['format_type'] = array(
  16. '#title' => t('Choose how users view dates and times:'),
  17. '#type' => 'select',
  18. '#options' => $date_formats + array('custom' => t('Custom')),
  19. '#default_value' => $settings['format_type'],
  20. '#description' => t('To add or edit options, visit <a href="@date-time-page">Date and time settings</a>.', array('@date-time-page' => url('admin/config/regional/date-time'))),
  21. '#weight' => 0,
  22. );
  23. $form['custom_date_format'] = array(
  24. '#type' => 'textfield',
  25. '#title' => t('Custom date format'),
  26. '#description' => t('If "Custom", see the <a href="@url" target="_blank">PHP manual</a> for date formats. Otherwise, enter the number of different time units to display, which defaults to 2.', array('@url' => 'http://php.net/manual/function.date.php')),
  27. '#default_value' => isset($settings['custom_date_format']) ? $settings['custom_date_format'] : '',
  28. '#dependency' => array('edit-options-settings-format-type' => array('custom')),
  29. );
  30. $form['fromto'] = array(
  31. '#title' => t('Display:'),
  32. '#type' => 'select',
  33. '#options' => array(
  34. 'both' => t('Both Start and End dates'),
  35. 'value' => t('Start date only'),
  36. 'value2' => t('End date only'),
  37. ),
  38. '#access' => $field['settings']['todate'],
  39. '#default_value' => $settings['fromto'],
  40. '#weight' => 1,
  41. );
  42. // Make the string translatable by keeping it as a whole rather than
  43. // translating prefix and suffix separately.
  44. list($prefix, $suffix) = explode('@count', t('Show @count value(s)'));
  45. $form['multiple_number'] = array(
  46. '#type' => 'textfield',
  47. '#title' => t('Multiple values:'),
  48. '#size' => 5,
  49. '#field_prefix' => $prefix,
  50. '#field_suffix' => $suffix,
  51. '#default_value' => $settings['multiple_number'],
  52. '#weight' => 2,
  53. '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
  54. '#description' => t('Identify a specific number of values to display, or leave blank to show all values.'),
  55. );
  56. list($prefix, $suffix) = explode('@isodate', t('starting from @isodate'));
  57. $form['multiple_from'] = array(
  58. '#type' => 'textfield',
  59. '#size' => 15,
  60. '#field_prefix' => $prefix,
  61. '#field_suffix' => $suffix,
  62. '#default_value' => $settings['multiple_from'],
  63. '#weight' => 3,
  64. '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
  65. );
  66. list($prefix, $suffix) = explode('@isodate', t('ending with @isodate'));
  67. $form['multiple_to'] = array(
  68. '#type' => 'textfield',
  69. '#size' => 15,
  70. '#field_prefix' => $prefix,
  71. '#field_suffix' => $suffix,
  72. '#default_value' => $settings['multiple_to'],
  73. '#weight' => 4,
  74. '#access' => ($field['cardinality'] == FIELD_CARDINALITY_UNLIMITED) || ($field['cardinality'] > 1),
  75. '#description' => t('Identify specific start and/or end dates in the format YYYY-MM-DDTHH:MM:SS, or leave blank for all available dates.'),
  76. );
  77. $form['show_remaining_days'] = array(
  78. '#title' => t('Show remaining days'),
  79. '#type' => 'checkbox',
  80. '#default_value' => $settings['show_remaining_days'],
  81. '#weight' => 0,
  82. );
  83. return $form;
  84. }
  85. /**
  86. * Settings for the interval formatter.
  87. */
  88. function date_interval_formatter_settings_form($field, $instance, $view_mode, $form, &$form_state) {
  89. $display = $instance['display'][$view_mode];
  90. $settings = $display['settings'];
  91. $form = array();
  92. $form['interval'] = array(
  93. '#title' => t('Interval'),
  94. '#description' => t("How many time units should be shown in the 'time ago' string."),
  95. '#type' => 'select',
  96. '#options' => drupal_map_assoc(range(1, 6)),
  97. '#default_value' => $settings['interval'],
  98. '#weight' => 0,
  99. );
  100. // Uses the same options used by Views format_interval.
  101. $options = array(
  102. 'raw time ago' => t('Time ago'),
  103. 'time ago' => t('Time ago (with "ago" appended)'),
  104. 'raw time hence' => t('Time hence'),
  105. 'time hence' => t('Time hence (with "hence" appended)'),
  106. 'raw time span' => t('Time span (future dates have "-" prepended)'),
  107. 'inverse time span' => t('Time span (past dates have "-" prepended)'),
  108. 'time span' => t('Time span (with "ago/hence" appended)'),
  109. );
  110. $form['interval_display'] = array(
  111. '#title' => t('Display'),
  112. '#description' => t("How to display the time ago or time hence for this field."),
  113. '#type' => 'select',
  114. '#options' => $options,
  115. '#default_value' => $settings['interval_display'],
  116. '#weight' => 0,
  117. );
  118. if (!empty($field['settings']['todate'])) {
  119. $form['use_end_date'] = array(
  120. '#title' => t('Use End date'),
  121. '#description' => 'Use the End date, instead of the start date',
  122. '#type' => 'checkbox',
  123. '#default_value' => $settings['use_end_date'],
  124. );
  125. }
  126. return $form;
  127. }
  128. /**
  129. * Settings summary for the default formatter.
  130. */
  131. function date_default_formatter_settings_summary($field, $instance, $view_mode) {
  132. $display = $instance['display'][$view_mode];
  133. $settings = $display['settings'];
  134. $formatter = $display['type'];
  135. $format_types = date_format_type_options();
  136. $summary = array();
  137. $format = FALSE;
  138. switch ($formatter) {
  139. case 'date_plain':
  140. $format = t('Plain');
  141. break;
  142. case 'format_interval':
  143. $format = t('Interval');
  144. break;
  145. default:
  146. if (!empty($format_types[$settings['format_type']])) {
  147. $format = $format_types[$settings['format_type']];
  148. }
  149. }
  150. if ($format) {
  151. $summary[] = t('Display dates using the @format format', array('@format' => $format));
  152. }
  153. else {
  154. $summary[] = t('Display dates using the default format because the specified format (@format) is not defined', array('@format' => $settings['format_type']));
  155. }
  156. if (array_key_exists('fromto', $settings) && $field['settings']['todate']) {
  157. $options = array(
  158. 'both' => t('Display both Start and End dates'),
  159. 'value' => t('Display Start date only'),
  160. 'value2' => t('Display End date only'),
  161. );
  162. if (isset($options[$settings['fromto']])) {
  163. $summary[] = $options[$settings['fromto']];
  164. }
  165. }
  166. if (array_key_exists('multiple_number', $settings) && !empty($field['cardinality'])) {
  167. $summary[] = t('Show @count value(s) starting with @date1, ending with @date2', array(
  168. '@count' => !empty($settings['multiple_number']) ? $settings['multiple_number'] : t('all'),
  169. '@date1' => !empty($settings['multiple_from']) ? $settings['multiple_from'] : t('earliest'),
  170. '@date2' => !empty($settings['multiple_to']) ? $settings['multiple_to'] : t('latest'),
  171. ));
  172. }
  173. if (array_key_exists('show_remaining_days', $settings)) {
  174. $summary[] = t('Show remaining days: @value', array('@value' => ($settings['show_remaining_days'] ? 'yes' : 'no')));
  175. }
  176. return $summary;
  177. }
  178. /**
  179. * Settings summary for the interval formatter.
  180. *
  181. * @TODO Add settings later.
  182. */
  183. function date_interval_formatter_settings_summary($field, $instance, $view_mode) {
  184. $summary = array();
  185. $display = $instance['display'][$view_mode];
  186. $settings = $display['settings'];
  187. $formatter = $display['type'];
  188. $field = ($settings['use_end_date'] == 1) ? 'End' : 'Start';
  189. $summary[] = t('Display time ago, showing @interval units. Using @field Date',
  190. array('@interval' => $settings['interval'], '@field' => $field));
  191. return $summary;
  192. }
  193. /**
  194. * Helper function for date_field_instance_settings_form().
  195. *
  196. * @see date_field_instance_settings_form_validate()
  197. */
  198. function _date_field_instance_settings_form($field, $instance) {
  199. $widget = $instance['widget'];
  200. $settings = $instance['settings'];
  201. $widget_settings = $instance['widget']['settings'];
  202. $form['default_value'] = array(
  203. '#type' => 'select',
  204. '#title' => t('Default date'),
  205. '#default_value' => $settings['default_value'],
  206. '#options' => array(
  207. 'blank' => t('No default value'),
  208. 'now' => t('Now'),
  209. 'strtotime' => t('Relative'),
  210. ),
  211. '#weight' => 1,
  212. '#fieldset' => 'default_values',
  213. );
  214. $description = t("Describe a time by reference to the current day, like '+90 days' (90 days from the day the field is created) or '+1 Saturday' (the next Saturday). See !strtotime for more details.", array('!strtotime' => l(t('strtotime'), 'http://www.php.net/manual/en/function.strtotime.php')));
  215. $form['default_value_code'] = array(
  216. '#type' => 'textfield',
  217. '#title' => t('Relative default value'),
  218. '#description' => $description,
  219. '#default_value' => $settings['default_value_code'],
  220. '#states' => array(
  221. 'visible' => array(
  222. ':input[name="instance[settings][default_value]"]' => array(
  223. 'value' => 'strtotime',
  224. ),
  225. ),
  226. ),
  227. '#weight' => 1.1,
  228. '#fieldset' => 'default_values',
  229. );
  230. $form['default_value2'] = array(
  231. '#type' => !empty($field['settings']['todate']) ? 'select' : 'hidden',
  232. '#title' => t('Default end date'),
  233. '#default_value' => $settings['default_value2'],
  234. '#options' => array(
  235. 'same' => t('Same as Default date'),
  236. 'blank' => t('No default value'),
  237. 'now' => t('Now'),
  238. 'strtotime' => t('Relative'),
  239. ),
  240. '#weight' => 2,
  241. '#fieldset' => 'default_values',
  242. );
  243. $form['default_value_code2'] = array(
  244. '#type' => !empty($field['settings']['todate']) ? 'textfield' : 'hidden',
  245. '#title' => t('Relative default value for end date'),
  246. '#description' => $description,
  247. '#default_value' => $settings['default_value_code2'],
  248. '#states' => array(
  249. 'visible' => array(
  250. ':input[name="instance[settings][default_value2]"]' => array(
  251. 'value' => 'strtotime',
  252. ),
  253. ),
  254. ),
  255. '#weight' => 2.1,
  256. '#fieldset' => 'default_values',
  257. );
  258. $form['#element_validate'] = array('date_field_instance_settings_form_validate');
  259. $context = array(
  260. 'field' => $field,
  261. 'instance' => $instance,
  262. );
  263. drupal_alter('date_field_instance_settings_form', $form, $context);
  264. return $form;
  265. }
  266. /**
  267. * Form validation handler for _date_field_instance_settings_form().
  268. */
  269. function _date_field_instance_settings_form_validate(&$form, &$form_state) {
  270. $settings = $form_state['values']['instance']['settings'];
  271. if ($settings['default_value'] == 'strtotime') {
  272. $is_strtotime = @strtotime($settings['default_value_code']);
  273. if (!$is_strtotime) {
  274. form_set_error('instance][settings][default_value_code', t('The Strtotime default value is invalid.'));
  275. }
  276. }
  277. if (isset($settings['default_value2']) && $settings['default_value2'] == 'strtotime') {
  278. $is_strtotime = @strtotime($settings['default_value_code2']);
  279. if (!$is_strtotime) {
  280. form_set_error('instance][settings][default_value_code2', t('The Strtotime default value for the End Date is invalid.'));
  281. }
  282. }
  283. }
  284. /**
  285. * Helper function for date_field_widget_settings_form().
  286. *
  287. * @see date_field_widget_settings_form_validate()
  288. */
  289. function _date_field_widget_settings_form($field, $instance) {
  290. $widget = $instance['widget'];
  291. $settings = $widget['settings'];
  292. $form = array(
  293. '#element_validate' => array('date_field_widget_settings_form_validate'),
  294. );
  295. $options = array();
  296. if ($widget['type'] == 'date_popup' && module_exists('date_popup')) {
  297. $formats = date_popup_formats();
  298. }
  299. else {
  300. // Example input formats must show all possible date parts, so add seconds.
  301. $formats = str_replace('i', 'i:s', array_keys(system_get_date_formats('short')));
  302. $formats = drupal_map_assoc($formats);
  303. }
  304. $now = date_example_date();
  305. $options['site-wide'] = t('Short date format: @date', array('@date' => date_format_date($now, 'short')));
  306. foreach ($formats as $f) {
  307. $options[$f] = date_format_date($now, 'custom', $f);
  308. }
  309. $form['input_format'] = array(
  310. '#type' => 'select',
  311. '#title' => t('Date entry options'),
  312. '#default_value' => $settings['input_format'],
  313. '#options' => $options,
  314. '#description' => t('Control the order and format of the options users see.'),
  315. '#weight' => 3,
  316. '#fieldset' => 'date_format',
  317. );
  318. // Only a limited set of formats is available for the Date Popup module.
  319. if ($widget['type'] != 'date_popup') {
  320. $form['input_format']['#options']['custom'] = t('Custom format');
  321. $form['input_format_custom'] = array(
  322. '#type' => 'textfield',
  323. '#title' => t('Custom input format'),
  324. '#default_value' => $settings['input_format_custom'],
  325. '#description' => t("Override the input format selected above. Define a php date format string like 'm-d-Y H:i' (see <a href=\"@link\">http://php.net/date</a> for more details).", array('@link' => 'http://php.net/date')),
  326. '#weight' => 5,
  327. '#fieldset' => 'date_format',
  328. '#attributes' => array('class' => array('indent')),
  329. '#states' => array(
  330. 'visible' => array(
  331. ':input[name="instance[widget][settings][input_format]"]' => array('value' => 'custom'),
  332. ),
  333. ),
  334. );
  335. }
  336. else {
  337. $form['input_format_custom'] = array(
  338. '#type' => 'hidden',
  339. '#value' => '',
  340. );
  341. }
  342. if (in_array($widget['type'], array('date_select', 'date_popup'))) {
  343. $form['year_range'] = array(
  344. '#type' => 'date_year_range',
  345. '#default_value' => $settings['year_range'],
  346. '#fieldset' => 'date_format',
  347. '#weight' => 6,
  348. );
  349. $form['increment'] = array(
  350. '#type' => 'select', '#title' => t('Time increments'),
  351. '#default_value' => $settings['increment'],
  352. '#options' => array(
  353. 1 => t('1 minute'),
  354. 5 => t('5 minute'),
  355. 10 => t('10 minute'),
  356. 15 => t('15 minute'),
  357. 30 => t('30 minute')),
  358. '#weight' => 7,
  359. '#fieldset' => 'date_format',
  360. );
  361. }
  362. else {
  363. $form['year_range'] = array(
  364. '#type' => 'hidden',
  365. '#value' => $settings['year_range'],
  366. );
  367. $form['increment'] = array(
  368. '#type' => 'hidden',
  369. '#value' => $settings['increment'],
  370. );
  371. }
  372. $form['label_position'] = array(
  373. '#type' => 'value',
  374. '#value' => $settings['label_position'],
  375. );
  376. $form['text_parts'] = array(
  377. '#type' => 'value',
  378. '#value' => $settings['text_parts'],
  379. );
  380. $form['advanced'] = array(
  381. '#type' => 'fieldset',
  382. '#title' => t('Advanced settings'),
  383. '#collapsible' => TRUE,
  384. '#collapsed' => TRUE,
  385. '#fieldset' => 'date_format',
  386. '#weight' => 9,
  387. );
  388. if (in_array($widget['type'], array('date_select'))) {
  389. $options = array(
  390. 'above' => t('Above'),
  391. 'within' => t('Within'),
  392. 'none' => t('None'),
  393. );
  394. $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'Within' inserts the label as the first option in the select list and in blank textfields. 'None' doesn't visually label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
  395. }
  396. else {
  397. $options = array(
  398. 'above' => t('Above'),
  399. 'none' => t('None'),
  400. );
  401. $description = t("The location of date part labels, like 'Year', 'Month', or 'Day' . 'Above' displays the label as titles above each date part. 'None' doesn't visually label any of the date parts. Theme functions like 'date_part_label_year' and 'date_part_label_month' control label text.");
  402. }
  403. $form['advanced']['label_position'] = array(
  404. '#type' => 'radios',
  405. '#options' => $options,
  406. '#default_value' => $settings['label_position'],
  407. '#title' => t('Position of date part labels'),
  408. '#description' => $description,
  409. );
  410. $form['advanced']['text_parts'] = array(
  411. '#theme' => $widget['type'] == 'date_select' ? 'date_text_parts' : '',
  412. );
  413. $text_parts = (array) $settings['text_parts'];
  414. foreach (date_granularity_names() as $key => $value) {
  415. if ($widget['type'] == 'date_select') {
  416. $form['advanced']['text_parts'][$key] = array(
  417. '#type' => 'radios',
  418. '#default_value' => in_array($key, $text_parts) ? 1 : 0,
  419. '#options' => array(0 => '', 1 => ''),
  420. );
  421. }
  422. else {
  423. $form['advanced']['text_parts'][$key] = array(
  424. '#type' => 'value',
  425. '#value' => (int) in_array($key, (array) $settings['text_parts']),
  426. );
  427. }
  428. }
  429. $form['advanced']['no_fieldset'] = array(
  430. '#type' => 'checkbox',
  431. '#title' => t('Render as a regular field'),
  432. '#default_value' => !empty($settings['no_fieldset']),
  433. '#description' => t('Whether to render this field as a regular field instead of a fieldset. The date field elements are wrapped in a fieldset by default, and may not display well without it.'),
  434. );
  435. $context = array(
  436. 'field' => $field,
  437. 'instance' => $instance,
  438. );
  439. drupal_alter('date_field_widget_settings_form', $form, $context);
  440. return $form;
  441. }
  442. /**
  443. * Form validation handler for _date_field_widget_settings_form().
  444. */
  445. function _date_field_widget_settings_form_validate(&$form, &$form_state) {
  446. // The widget settings are in the wrong place in the form because of #tree on
  447. // the top level.
  448. $settings = $form_state['values']['instance']['widget']['settings'];
  449. $settings = array_merge($settings, $settings['advanced']);
  450. unset($settings['advanced']);
  451. form_set_value(array('#parents' => array('instance', 'widget', 'settings')), $settings, $form_state);
  452. $widget = &$form_state['values']['instance']['widget'];
  453. // Munge the table display for text parts back into an array of text parts.
  454. if (is_array($widget['settings']['text_parts'])) {
  455. form_set_value($form['text_parts'], array_keys(array_filter($widget['settings']['text_parts'])), $form_state);
  456. }
  457. if ($widget['settings']['input_format'] === 'custom' && empty($widget['settings']['input_format_custom'])) {
  458. form_set_error('instance][widget][settings][input_format_custom', t('Please enter a custom date format, or choose one of the preset formats.'));
  459. }
  460. }
  461. /**
  462. * Helper function for date_field_settings_form().
  463. *
  464. * @see date_field_settings_validate()
  465. */
  466. function _date_field_settings_form($field, $instance, $has_data) {
  467. $settings = $field['settings'];
  468. $form = array(
  469. '#element_validate' => array('date_field_settings_validate'),
  470. );
  471. // Make sure granularity is in the right format and has no empty values.
  472. if (!empty($settings['granularity']) && is_array($settings['granularity'])) {
  473. $granularity = array_filter($settings['granularity']);
  474. }
  475. $tz_handling = $settings['tz_handling'];
  476. $description = t('Select the date attributes to collect and store.');
  477. if ($has_data) {
  478. $description .= ' ' . t('Changes to date attributes only effects new or updated content.');
  479. }
  480. $options = date_granularity_names();
  481. $checkbox_year = array(
  482. '#type' => 'checkbox',
  483. '#title' => check_plain($options['year']),
  484. '#value' => 'year',
  485. '#return_value' => 'year',
  486. '#disabled' => TRUE,
  487. );
  488. unset($options['year']);
  489. $form['granularity'] = array(
  490. '#type' => 'checkboxes',
  491. '#title' => t('Date attributes to collect'),
  492. '#default_value' => $granularity,
  493. '#options' => $options,
  494. '#attributes' => array(
  495. 'class' => array('container-inline'),
  496. ),
  497. '#description' => $description,
  498. 'year' => $checkbox_year,
  499. );
  500. $description = t('End dates are used to collect duration. E.g., allow an event to start on September 15, and end on September 16.');
  501. $form['enddate_get'] = array(
  502. '#type' => 'checkbox',
  503. '#title' => t('Collect an end date'),
  504. '#description' => $description,
  505. '#default_value' => (empty($settings['todate']) ? FALSE : TRUE),
  506. '#disabled' => $has_data,
  507. );
  508. $form['enddate_required'] = array(
  509. '#type' => 'checkbox',
  510. '#title' => t('Required'),
  511. '#default_value' => ((isset($settings['todate']) && $settings['todate'] === 'required') ? TRUE : FALSE),
  512. '#disabled' => $has_data,
  513. '#states' => array(
  514. 'invisible' => array(
  515. 'input[name="field[settings][enddate_get]"]' => array('checked' => FALSE),
  516. ),
  517. ),
  518. );
  519. $description = t('Select the timezone handling method for this date field.');
  520. $form['tz_handling'] = array(
  521. '#type' => 'select',
  522. '#title' => t('Time zone handling'),
  523. '#default_value' => $tz_handling,
  524. '#options' => date_timezone_handling_options(),
  525. '#description' => $description,
  526. '#attached' => array(
  527. 'js' => array(drupal_get_path('module', 'date') . '/date_admin.js'),
  528. ),
  529. );
  530. // Force this value to hidden because we don't want to allow it to be changed
  531. // right now, but allow it to be a variable if needed.
  532. $form['timezone_db'] = array(
  533. '#type' => 'hidden',
  534. '#value' => date_get_timezone_db($tz_handling),
  535. );
  536. $form['cache_enabled'] = array(
  537. '#type' => 'checkbox',
  538. '#title' => t('Cache dates'),
  539. '#description' => t('Date objects can be created and cached as date fields are loaded, rather than when they are displayed, to improve performance.'),
  540. '#default_value' => !empty($settings['cache_enabled']),
  541. '#weight' => 10,
  542. );
  543. $form['cache_count'] = array(
  544. '#type' => 'textfield',
  545. '#title' => t('Maximum dates per field'),
  546. '#default_value' => (isset($settings['cache_count'])) ? $settings['cache_count'] : 4,
  547. '#description' => t("If set to '0', all date values on every entity will be cached. Note that caching every date on fields that may have a large number of multiple or repeating values may create a significant performance penalty when the cache is cleared. The suggested setting for multiple value and repeating fields is no more than 4 values per field."),
  548. '#size' => 3,
  549. '#weight' => 11,
  550. '#states' => array(
  551. 'visible' => array(
  552. 'input[name="field[settings][cache_enabled]"]' => array(
  553. 'checked' => TRUE,
  554. ),
  555. ),
  556. ),
  557. );
  558. $context = array(
  559. 'field' => $field,
  560. 'instance' => $instance,
  561. 'has_data' => $has_data,
  562. );
  563. drupal_alter('date_field_settings_form', $form, $context);
  564. return $form;
  565. }
  566. /**
  567. * Form validation handler for _date_field_settings_form().
  568. */
  569. function _date_field_settings_validate(&$form, &$form_state) {
  570. $field = &$form_state['values']['field'];
  571. if ($field['settings']['tz_handling'] == 'none') {
  572. form_set_value($form['timezone_db'], '', $form_state);
  573. }
  574. else {
  575. form_set_value($form['timezone_db'], date_get_timezone_db($field['settings']['tz_handling']), $form_state);
  576. }
  577. if ($field['settings']['tz_handling'] != 'none' && !in_array('hour', array_filter($field['settings']['granularity']))) {
  578. form_set_error('field[settings][tz_handling]', t('Dates without hours granularity must not use any timezone handling.'));
  579. }
  580. // Extract the correct 'todate' value out of the two end date checkboxes.
  581. if ($field['settings']['enddate_get']) {
  582. if ($field['settings']['enddate_required']) {
  583. $field['settings']['todate'] = 'required';
  584. }
  585. else {
  586. $field['settings']['todate'] = 'optional';
  587. }
  588. }
  589. else {
  590. $field['settings']['todate'] = '';
  591. }
  592. // Don't save the pseudo values created in the UI.
  593. unset($field['settings']['enddate_get'], $field['settings']['enddate_required']);
  594. if (!empty($field['settings']['cache_enabled'])) {
  595. if (!is_numeric($field['settings']['cache_count'])) {
  596. form_set_error('field[settings][cache_count]', t('The number of cache values must be a number.'));
  597. }
  598. elseif ($field['settings']['cache_count'] < 0) {
  599. form_set_error('field[settings][cache_count]', t('The number of cache values must be a number 0 or greater.'));
  600. }
  601. }
  602. }
  603. /**
  604. * Timezone handling options.
  605. *
  606. * The 'none' option will do no timezone conversions and will store and display
  607. * dates exactly as entered useful in locales or situations where timezone
  608. * conversions are not working reliably, for dates with no times, for historical
  609. * dates where timezones are irrelevant, or anytime conversion is unnecessary or
  610. * undesirable.
  611. */
  612. function date_timezone_handling_options() {
  613. return array(
  614. 'site' => t("Site's time zone"),
  615. 'date' => t("Date's time zone"),
  616. 'user' => t("User's time zone"),
  617. 'utc' => 'UTC',
  618. 'none' => t('No time zone conversion'),
  619. );
  620. }