date_elements.inc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. <?php
  2. /**
  3. * @file
  4. * Date forms and form themes and validation.
  5. *
  6. * All code used in form editing and processing is in this file,
  7. * included only during form editing.
  8. */
  9. /**
  10. * Private implementation of hook_widget().
  11. *
  12. * The widget builds out a complex date element in the following way:
  13. *
  14. * - A field is pulled out of the database which is comprised of one or
  15. * more collections of start/end dates.
  16. *
  17. * - The dates in this field are all converted from the UTC values stored
  18. * in the database back to the local time. This is done in #process
  19. * to avoid making this change to dates that are not being processed,
  20. * like those hidden with #access.
  21. *
  22. * - If values are empty, the field settings rules are used to determine
  23. * if the default_values should be empty, now, the same, or use strtotime.
  24. *
  25. * - Each start/end combination is created using the date_combo element type
  26. * defined by the date module. If the timezone is date-specific, a
  27. * timezone selector is added to the first combo element.
  28. *
  29. * - The date combo element creates two individual date elements, one each
  30. * for the start and end field, using the appropriate individual Date API
  31. * date elements, like selects, textfields, or popups.
  32. *
  33. * - In the individual element validation, the data supplied by the user is
  34. * used to update the individual date values.
  35. *
  36. * - In the combo date validation, the timezone is updated, if necessary,
  37. * then the user input date values are used with that timezone to create
  38. * date objects, which are used update combo date timezone and offset values.
  39. *
  40. * - In the field's submission processing, the new date values, which are in
  41. * the local timezone, are converted back to their UTC values and stored.
  42. *
  43. */
  44. function date_field_widget_form(&$form, &$form_state, $field, $instance, $langcode, $items, $delta, $base) {
  45. $element = $base;
  46. $field_name = $field['field_name'];
  47. $entity_type = $instance['entity_type'];
  48. // If this is a new entity, populate the field with the right default values.
  49. // This happens early so even fields later hidden with #access get those values.
  50. // We should only add default values to new entities, to avoid over-writing
  51. // a value that has already been set. This means we can't just check to see
  52. // if $items is empty, because it might have been set that way on purpose.
  53. // @see date_field_widget_properties_alter() where we flagged if this is a new entity.
  54. // We check !isset($items[$delta]['value']) because entity translation may create
  55. // a new translation entity for an existing entity and we don't want to clobber
  56. // values that were already set in that case.
  57. // @see http://drupal.org/node/1478848.
  58. $is_default = FALSE;
  59. if (!empty($instance['widget']['is_new']) && !isset($items[$delta]['value'])) {
  60. $items = date_default_value($field, $instance, $langcode);
  61. $is_default = TRUE;
  62. }
  63. // @TODO Repeating dates should probably be made into their own field type and completely separated out.
  64. // That will have to wait for a new branch since it may break other things, including other modules
  65. // that have an expectation of what the date field types are.
  66. // Since repeating dates cannot use the default Add more button, we have to handle our own behaviors here.
  67. // Return only the first multiple value for repeating dates, then clean up the 'Add more' bits in #after_build.
  68. // The repeating values will be re-generated when the repeat widget form is validated.
  69. // At this point we can't tell if this form element is going to be hidden by #access, and we're going to
  70. // lose all but the first value by doing this, so store the original values in case we need to replace them later.
  71. if (!empty($field['settings']['repeat'])) {
  72. if ($delta == 0) {
  73. $form['#after_build'][] = 'date_repeat_after_build';
  74. $form_state['storage']['repeat_fields'][$field_name] = array_merge($form['#parents'], array($field_name));
  75. $form_state['storage']['date_items'][$field_name][$langcode] = $items;
  76. }
  77. else {
  78. return;
  79. }
  80. }
  81. module_load_include('inc', 'date_api', 'date_api_elements');
  82. $timezone = date_get_timezone($field['settings']['tz_handling'], isset($items[0]['timezone']) ? $items[0]['timezone'] : date_default_timezone());
  83. // TODO see if there's a way to keep the timezone element from ever being
  84. // nested as array('timezone' => 'timezone' => value)). After struggling
  85. // with this a while, I can find no way to get it displayed in the form
  86. // correctly and get it to use the timezone element without ending up
  87. // with nesting.
  88. if (is_array($timezone)) {
  89. $timezone = $timezone['timezone'];
  90. }
  91. $element += array(
  92. '#type' => 'date_combo',
  93. '#theme_wrappers' => array('date_combo'),
  94. '#weight' => $delta,
  95. '#default_value' => isset($items[$delta]) ? $items[$delta] : '',
  96. '#date_timezone' => $timezone,
  97. '#element_validate' => array('date_combo_validate'),
  98. '#date_is_default' => $is_default,
  99. // Store the original values, for use with disabled and hidden fields.
  100. '#date_items' => isset($items[$delta]) ? $items[$delta] : '',
  101. );
  102. $element['#title'] = $instance['label'];
  103. if ($field['settings']['tz_handling'] == 'date') {
  104. $element['timezone'] = array(
  105. '#type' => 'date_timezone',
  106. '#theme_wrappers' => array('date_timezone'),
  107. '#delta' => $delta,
  108. '#default_value' => $timezone,
  109. '#weight' => $instance['widget']['weight'] + 1,
  110. '#attributes' => array('class' => array('date-no-float')),
  111. '#date_label_position' => $instance['widget']['settings']['label_position'],
  112. );
  113. }
  114. return $element;
  115. }
  116. /**
  117. * Create local date object.
  118. *
  119. * Create a date object set to local time from the field and
  120. * widget settings and item values. Default values for new entities
  121. * are set by the default value callback, so don't need to be accounted for here.
  122. */
  123. function date_local_date($item, $timezone, $field, $instance, $part = 'value') {
  124. $value = $item[$part];
  125. // If the value is empty, don't try to create a date object because it will
  126. // end up being the current day.
  127. if (empty($value)) {
  128. return NULL;
  129. }
  130. // @TODO Figure out how to replace date_fuzzy_datetime() function.
  131. // Special case for ISO dates to create a valid date object for formatting.
  132. // Is this still needed?
  133. /*
  134. if ($field['type'] == DATE_ISO) {
  135. $value = date_fuzzy_datetime($value);
  136. }
  137. else {
  138. $db_timezone = date_get_timezone_db($field['settings']['tz_handling']);
  139. $value = date_convert($value, $field['type'], DATE_DATETIME, $db_timezone);
  140. }
  141. */
  142. $date = new DateObject($value, date_get_timezone_db($field['settings']['tz_handling']));
  143. $date->limitGranularity($field['settings']['granularity']);
  144. if (empty($date)) {
  145. return NULL;
  146. }
  147. date_timezone_set($date, timezone_open($timezone));
  148. return $date;
  149. }
  150. /**
  151. * The callback for setting a default value for an empty date field.
  152. */
  153. function date_default_value($field, $instance, $langcode) {
  154. $item = array();
  155. $db_format = date_type_format($field['type']);
  156. $date = date_default_value_part($item, $field, $instance, $langcode, 'value');
  157. $item[0]['value'] = is_object($date) ? date_format($date, $db_format) : '';
  158. if (!empty($field['settings']['todate'])) {
  159. $date = date_default_value_part($item, $field, $instance, $langcode, 'value2');
  160. $item[0]['value2'] = is_object($date) ? date_format($date, $db_format) : '';
  161. }
  162. // Make sure the default value has the same construct as a loaded field value
  163. // to avoid errors if the default value is used on a hidden element.
  164. $item[0]['timezone'] = date_get_timezone($field['settings']['tz_handling']);
  165. $item[0]['timezone_db'] = date_get_timezone_db($field['settings']['tz_handling']);
  166. $item[0]['date_type'] = $field['type'];
  167. if (!isset($item[0]['value2'])) {
  168. $item[0]['value2'] = $item[0]['value'];
  169. }
  170. return $item;
  171. }
  172. /**
  173. * Helper function for the date default value callback to set
  174. * either 'value' or 'value2' to its default value.
  175. */
  176. function date_default_value_part($item, $field, $instance, $langcode, $part = 'value') {
  177. $timezone = date_get_timezone($field['settings']['tz_handling']);
  178. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  179. $date = NULL;
  180. if ($part == 'value') {
  181. $default_value = $instance['settings']['default_value'];
  182. $default_value_code = $instance['settings']['default_value_code'];
  183. }
  184. else {
  185. $default_value = $instance['settings']['default_value2'];
  186. $default_value_code = $instance['settings']['default_value_code2'];
  187. }
  188. if (empty($default_value) || $default_value == 'blank') {
  189. return NULL;
  190. }
  191. elseif ($default_value == 'strtotime' && !empty($default_value_code)) {
  192. $date = new DateObject($default_value_code, date_default_timezone());
  193. }
  194. elseif ($part == 'value2' && $default_value == 'same') {
  195. if ($instance['settings']['default_value'] == 'blank' || empty($item[0]['value'])) {
  196. return NULL;
  197. }
  198. else {
  199. // The date stored in 'value' has already been switched to the db timezone.
  200. $date = new DateObject($item[0]['value'], $timezone_db, DATE_FORMAT_DATETIME);
  201. }
  202. }
  203. // Special case for 'now' when using dates with no timezone,
  204. // make sure 'now' isn't adjusted to UTC value of 'now' .
  205. elseif ($field['settings']['tz_handling'] == 'none') {
  206. $date = date_now();
  207. }
  208. else {
  209. $date = date_now($timezone);
  210. }
  211. // The default value needs to be in the database timezone.
  212. date_timezone_set($date, timezone_open($timezone_db));
  213. $date->limitGranularity($field['settings']['granularity']);
  214. return $date;
  215. }
  216. /**
  217. * Process an individual date element.
  218. */
  219. function date_combo_element_process($element, &$form_state, $form) {
  220. if (date_hidden_element($element)) {
  221. // A hidden value for a new entity that had its end date set to blank
  222. // will not get processed later to populate the end date, so set it here.
  223. if (isset($element['#value']['value2']) && empty($element['#value']['value2'])) {
  224. $element['#value']['value2'] = $element['#value']['value'];
  225. }
  226. return $element;
  227. }
  228. $field_name = $element['#field_name'];
  229. $delta = $element['#delta'];
  230. $bundle = $element['#bundle'];
  231. $entity_type = $element['#entity_type'];
  232. $langcode = $element['#language'];
  233. $date_is_default = $element['#date_is_default'];
  234. $field = field_widget_field($element, $form_state);
  235. $instance = field_widget_instance($element, $form_state);
  236. // Figure out how many items are in the form, including new ones added by ajax.
  237. $field_state = field_form_get_state($element['#field_parents'], $field_name, $element['#language'], $form_state);
  238. $items_count = $field_state['items_count'];
  239. $columns = $element['#columns'];
  240. if (isset($columns['rrule'])) {
  241. unset($columns['rrule']);
  242. }
  243. $from_field = 'value';
  244. $to_field = 'value2';
  245. $tz_field = 'timezone';
  246. $offset_field = 'offset';
  247. $offset_field2 = 'offset2';
  248. // Convert UTC dates to their local values in DATETIME format,
  249. // and adjust the default values as specified in the field settings.
  250. // It would seem to make sense to do this conversion when the data
  251. // is loaded instead of when the form is created, but the loaded
  252. // field data is cached and we can't cache dates that have been converted
  253. // to the timezone of an individual user, so we cache the UTC values
  254. // instead and do our conversion to local dates in the form and
  255. // in the formatters.
  256. $process = date_process_values($field, $instance);
  257. foreach ($process as $processed) {
  258. if (!isset($element['#default_value'][$processed])) {
  259. $element['#default_value'][$processed] = '';
  260. }
  261. $date = date_local_date($element['#default_value'], $element['#date_timezone'], $field, $instance, $processed);
  262. $element['#default_value'][$processed] = is_object($date) ? date_format($date, DATE_FORMAT_DATETIME) : '';
  263. }
  264. // Blank out the end date for optional end dates that match the start date,
  265. // except when this is a new node that has default values that should be honored.
  266. if (!$date_is_default && $field['settings']['todate'] != 'required'
  267. && !empty($element['#default_value'][$to_field])
  268. && $element['#default_value'][$to_field] == $element['#default_value'][$from_field]) {
  269. unset($element['#default_value'][$to_field]);
  270. }
  271. $show_todate = !empty($form_state['values']['show_todate']) || !empty($element['#default_value'][$to_field]) || $field['settings']['todate'] == 'required';
  272. $element['show_todate'] = array(
  273. '#title' => t('Show End Date'),
  274. '#type' => 'checkbox',
  275. '#default_value' => $show_todate,
  276. '#weight' => -20,
  277. '#access' => $field['settings']['todate'] == 'optional',
  278. '#prefix' => '<div class="date-float">',
  279. '#suffix' => '</div>',
  280. );
  281. $parents = $element['#parents'];
  282. $first_parent = array_shift($parents);
  283. $show_id = $first_parent . '[' . implode('][', $parents) . '][show_todate]';
  284. $element[$from_field] = array(
  285. '#field' => $field,
  286. '#instance' => $instance,
  287. '#weight' => $instance['widget']['weight'],
  288. '#required' => ($element['#required'] && $delta == 0) ? 1 : 0,
  289. '#default_value' => isset($element['#default_value'][$from_field]) ? $element['#default_value'][$from_field] : '',
  290. '#delta' => $delta,
  291. '#date_timezone' => $element['#date_timezone'],
  292. '#date_format' => date_limit_format(date_input_format($element, $field, $instance), $field['settings']['granularity']),
  293. '#date_text_parts' => (array) $instance['widget']['settings']['text_parts'],
  294. '#date_increment' => $instance['widget']['settings']['increment'],
  295. '#date_year_range' => $instance['widget']['settings']['year_range'],
  296. '#date_label_position' => $instance['widget']['settings']['label_position'],
  297. );
  298. $description = !empty($element['#description']) ? t($element['#description']) : '';
  299. unset($element['#description']);
  300. // Give this element the right type, using a Date API
  301. // or a Date Popup element type.
  302. $element[$from_field]['#attributes'] = array('class' => array('date-clear'));
  303. $element[$from_field]['#wrapper_attributes'] = array('class' => array());
  304. $element[$from_field]['#wrapper_attributes']['class'][] = 'date-no-float';
  305. switch ($instance['widget']['type']) {
  306. case 'date_select':
  307. $element[$from_field]['#type'] = 'date_select';
  308. $element[$from_field]['#theme_wrappers'] = array('date_select');
  309. $element['#attached']['js'][] = drupal_get_path('module', 'date') . '/date.js';
  310. $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  311. break;
  312. case 'date_popup':
  313. $element[$from_field]['#type'] = 'date_popup';
  314. $element[$from_field]['#theme_wrappers'] = array('date_popup');
  315. $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  316. break;
  317. default:
  318. $element[$from_field]['#type'] = 'date_text';
  319. $element[$from_field]['#theme_wrappers'] = array('date_text');
  320. $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  321. break;
  322. }
  323. // If this field uses the 'End', add matching element
  324. // for the 'End' date, and adapt titles to make it clear which
  325. // is the 'Start' and which is the 'End' .
  326. if (!empty($field['settings']['todate'])) {
  327. $element[$to_field] = $element[$from_field];
  328. $element[$from_field]['#title_display'] = 'none';
  329. $element[$to_field]['#title'] = t('to:');
  330. $element[$from_field]['#wrapper_attributes']['class'][] = 'start-date-wrapper';
  331. $element[$to_field]['#wrapper_attributes']['class'][] = 'end-date-wrapper';
  332. $element[$to_field]['#default_value'] = isset($element['#default_value'][$to_field]) ? $element['#default_value'][$to_field] : '';
  333. $element[$to_field]['#required'] = ($element[$from_field]['#required'] && $field['settings']['todate'] == 'required');
  334. $element[$to_field]['#weight'] += .2;
  335. $element[$to_field]['#prefix'] = '';
  336. // Users with JS enabled will never see initially blank values for the end
  337. // date (see Drupal.date.EndDateHandler()), so hide the message for them.
  338. $description .= '<span class="js-hide"> ' . t("Empty 'End date' values will use the 'Start date' values.") . '</span>';
  339. $element['#fieldset_description'] = $description;
  340. if ($field['settings']['todate'] == 'optional') {
  341. $element[$to_field]['#states'] = array(
  342. 'visible' => array(
  343. 'input[name="' . $show_id . '"]' => array('checked' => TRUE),
  344. ));
  345. }
  346. }
  347. else {
  348. $element[$from_field]['#description'] = $description;
  349. }
  350. // Create label for error messages that make sense in multiple values
  351. // and when the title field is left blank.
  352. if ($field['cardinality'] <> 1 && empty($field['settings']['repeat'])) {
  353. $element[$from_field]['#date_title'] = t('@field_name Start date value #@delta', array('@field_name' => $instance['label'], '@delta' => $delta + 1));
  354. if (!empty($field['settings']['todate'])) {
  355. $element[$to_field]['#date_title'] = t('@field_name End date value #@delta', array('@field_name' => $instance['label'], '@delta' => $delta + 1));
  356. }
  357. }
  358. elseif (!empty($field['settings']['todate'])) {
  359. $element[$from_field]['#date_title'] = t('@field_name Start date', array('@field_name' => $instance['label']));
  360. $element[$to_field]['#date_title'] = t('@field_name End date', array('@field_name' => $instance['label']));
  361. }
  362. else {
  363. $element[$from_field]['#date_title'] = t('@field_name', array('@field_name' => $instance['label']));
  364. }
  365. $context = array(
  366. 'field' => $field,
  367. 'instance' => $instance,
  368. 'form' => $form,
  369. );
  370. drupal_alter('date_combo_process', $element, $form_state, $context);
  371. return $element;
  372. }
  373. function date_element_empty($element, &$form_state) {
  374. $item = array();
  375. $item['value'] = NULL;
  376. $item['value2'] = NULL;
  377. $item['timezone'] = NULL;
  378. $item['offset'] = NULL;
  379. $item['offset2'] = NULL;
  380. $item['rrule'] = NULL;
  381. form_set_value($element, $item, $form_state);
  382. return $item;
  383. }
  384. /**
  385. * Validate and update a combo element.
  386. * Don't try this if there were errors before reaching this point.
  387. */
  388. function date_combo_validate($element, &$form_state) {
  389. // Disabled and hidden elements won't have any input and don't need validation,
  390. // we just need to re-save the original values, from before they were processed into
  391. // widget arrays and timezone-adjusted.
  392. if (date_hidden_element($element) || !empty($element['#disabled'])) {
  393. form_set_value($element, $element['#date_items'], $form_state);
  394. return;
  395. }
  396. $field_name = $element['#field_name'];
  397. $delta = $element['#delta'];
  398. $langcode = $element['#language'];
  399. $form_values = drupal_array_get_nested_value($form_state['values'], $element['#field_parents']);
  400. $form_input = drupal_array_get_nested_value($form_state['input'], $element['#field_parents']);
  401. // If the whole field is empty and that's OK, stop now.
  402. if (empty($form_input[$field_name]) && !$element['#required']) {
  403. return;
  404. }
  405. $item = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  406. $posted = drupal_array_get_nested_value($form_state['input'], $element['#parents']);
  407. $field = field_widget_field($element, $form_state);
  408. $instance = field_widget_instance($element, $form_state);
  409. $context = array(
  410. 'field' => $field,
  411. 'instance' => $instance,
  412. 'item' => $item,
  413. );
  414. drupal_alter('date_combo_pre_validate', $element, $form_state, $context);
  415. $from_field = 'value';
  416. $to_field = 'value2';
  417. $tz_field = 'timezone';
  418. $offset_field = 'offset';
  419. $offset_field2 = 'offset2';
  420. // Check for empty 'Start date', which could either be an empty
  421. // value or an array of empty values, depending on the widget.
  422. $empty = TRUE;
  423. if (!empty($item[$from_field])) {
  424. if (!is_array($item[$from_field])) {
  425. $empty = FALSE;
  426. }
  427. else {
  428. foreach ($item[$from_field] as $key => $value) {
  429. if (!empty($value)) {
  430. $empty = FALSE;
  431. break;
  432. }
  433. }
  434. }
  435. }
  436. // An 'End' date without a 'Start' date is a validation error.
  437. if ($empty && !empty($item[$to_field])) {
  438. if (!is_array($item[$to_field])) {
  439. form_error($element, t("A 'Start date' date is required if an 'end date' is supplied for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])));
  440. $empty = FALSE;
  441. }
  442. else {
  443. foreach ($item[$to_field] as $key => $value) {
  444. if (!empty($value)) {
  445. form_error($element, t("A 'Start date' date is required if an 'End date' is supplied for field %field #%delta.", array('%delta' => $field['cardinality'] ? intval($delta + 1) : '', '%field' => $instance['label'])));
  446. $empty = FALSE;
  447. break;
  448. }
  449. }
  450. }
  451. }
  452. // If the user chose the option to not show the end date, just swap in the
  453. // start date as that value so the start and end dates are the same.
  454. if ($field['settings']['todate'] == 'optional' && empty($item['show_todate'])) {
  455. $item[$to_field] = $item[$from_field];
  456. $posted[$to_field] = $posted[$from_field];
  457. }
  458. if ($empty) {
  459. $item = date_element_empty($element, $form_state);
  460. if (!$element['#required']) {
  461. return;
  462. }
  463. }
  464. // Don't look for further errors if errors are already flagged
  465. // because otherwise we'll show errors on the nested elements
  466. // more than once.
  467. elseif (!form_get_errors()) {
  468. $timezone = !empty($item[$tz_field]) ? $item[$tz_field] : $element['#date_timezone'];
  469. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  470. $element[$from_field]['#date_timezone'] = $timezone;
  471. $from_date = date_input_date($field, $instance, $element[$from_field], $posted[$from_field]);
  472. if (!empty($field['settings']['todate'])) {
  473. $element[$to_field]['#date_timezone'] = $timezone;
  474. $to_date = date_input_date($field, $instance, $element[$to_field], $posted[$to_field]);
  475. }
  476. else {
  477. $to_date = $from_date;
  478. }
  479. // Neither the start date nor the end date should be empty at this point
  480. // unless they held values that couldn't be evaluated.
  481. if (!$instance['required'] && (!date_is_date($from_date) || !date_is_date($to_date))) {
  482. $item = date_element_empty($element, $form_state);
  483. $errors[] = t('The dates are invalid.');
  484. }
  485. elseif (!empty($field['settings']['todate']) && $from_date > $to_date) {
  486. form_set_value($element[$to_field], $to_date, $form_state);
  487. $errors[] = t('The End date must be greater than the Start date.');
  488. }
  489. else {
  490. // Convert input dates back to their UTC values and re-format to ISO
  491. // or UNIX instead of the DATETIME format used in element processing.
  492. $item[$tz_field] = $timezone;
  493. // Update the context for changes in the $item, and allow other modules to
  494. // alter the computed local dates.
  495. $context['item'] = $item;
  496. // We can only pass two additional values to drupal_alter, so $element
  497. // needs to be included in $context.
  498. $context['element'] = $element;
  499. drupal_alter('date_combo_validate_date_start', $from_date, $form_state, $context);
  500. drupal_alter('date_combo_validate_date_end', $to_date, $form_state, $context);
  501. $item[$offset_field] = date_offset_get($from_date);
  502. $test_from = date_format($from_date, 'r');
  503. $test_to = date_format($to_date, 'r');
  504. $item[$offset_field2] = date_offset_get($to_date);
  505. date_timezone_set($from_date, timezone_open($timezone_db));
  506. date_timezone_set($to_date, timezone_open($timezone_db));
  507. $item[$from_field] = date_format($from_date, date_type_format($field['type']));
  508. $item[$to_field] = date_format($to_date, date_type_format($field['type']));
  509. if (isset($form_values[$field_name]['rrule'])) {
  510. $item['rrule'] = $form_values[$field['field_name']]['rrule'];
  511. }
  512. // If the db timezone is not the same as the display timezone
  513. // and we are using a date with time granularity,
  514. // test a roundtrip back to the original timezone to catch
  515. // invalid dates, like 2AM on the day that spring daylight savings
  516. // time begins in the US.
  517. $granularity = date_format_order($element[$from_field]['#date_format']);
  518. if ($timezone != $timezone_db && date_has_time($granularity)) {
  519. date_timezone_set($from_date, timezone_open($timezone));
  520. date_timezone_set($to_date, timezone_open($timezone));
  521. if ($test_from != date_format($from_date, 'r')) {
  522. $errors[] = t('The Start date is invalid.');
  523. }
  524. if ($test_to != date_format($to_date, 'r')) {
  525. $errors[] = t('The End date is invalid.');
  526. }
  527. }
  528. if (empty($errors)) {
  529. form_set_value($element, $item, $form_state);
  530. }
  531. }
  532. }
  533. if (!empty($errors)) {
  534. if ($field['cardinality']) {
  535. form_error($element, t('There are errors in @field_name value #@delta:', array('@field_name' => $instance['label'], '@delta' => $delta + 1)) . theme('item_list', array('items' => $errors)));
  536. }
  537. else {
  538. form_error($element, t('There are errors in @field_name:', array('@field_name' => $instance['label'])) . theme('item_list', array('items' => $errors)));
  539. }
  540. }
  541. }
  542. /**
  543. * Determine the input format for this element.
  544. */
  545. function date_input_format($element, $field, $instance) {
  546. if (!empty($instance['widget']['settings']['input_format_custom'])) {
  547. return $instance['widget']['settings']['input_format_custom'];
  548. }
  549. elseif (!empty($instance['widget']['settings']['input_format']) && $instance['widget']['settings']['input_format'] != 'site-wide') {
  550. return $instance['widget']['settings']['input_format'];
  551. }
  552. return variable_get('date_format_short', 'm/d/Y - H:i');
  553. }
  554. /**
  555. * Implements hook_date_select_pre_validate_alter().
  556. */
  557. function date_date_select_pre_validate_alter(&$element, &$form_state, &$input) {
  558. date_empty_end_date($element, $form_state, $input);
  559. }
  560. /**
  561. * Implements hook_date_text_pre_validate_alter().
  562. */
  563. function date_date_text_pre_validate_alter(&$element, &$form_state, &$input) {
  564. date_empty_end_date($element, $form_state, $input);
  565. }
  566. /**
  567. * Implements hook_date_popup_pre_validate_alter().
  568. */
  569. function date_date_popup_pre_validate_alter(&$element, &$form_state, &$input) {
  570. date_empty_end_date($element, $form_state, $input);
  571. }
  572. /**
  573. * Helper function to clear out end date when not being used.
  574. */
  575. function date_empty_end_date(&$element, &$form_state, &$input) {
  576. // If this is the end date and the option to show an end date has not been selected,
  577. // empty the end date to surpress validation errors and stop further processing.
  578. $parents = $element['#parents'];
  579. $parent = array_pop($parents);
  580. if ($parent == 'value2') {
  581. $parent_values = drupal_array_get_nested_value($form_state['values'], $parents);
  582. if (isset($parent_values['show_todate']) && $parent_values['show_todate'] != 1) {
  583. $input = array();
  584. form_set_value($element, NULL, $form_state);
  585. }
  586. }
  587. }