date_elements.inc 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  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'] = array('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' => ($instance['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($instance['description']) ? t($instance['description']) : '';
  299. // Give this element the right type, using a Date API
  300. // or a Date Popup element type.
  301. $element[$from_field]['#attributes'] = array('class' => array('date-clear'));
  302. $element[$from_field]['#wrapper_attributes'] = array('class' => array());
  303. $element[$from_field]['#wrapper_attributes']['class'][] = 'date-no-float';
  304. switch ($instance['widget']['type']) {
  305. case 'date_select':
  306. $element[$from_field]['#type'] = 'date_select';
  307. $element[$from_field]['#theme_wrappers'] = array('date_select');
  308. $element['#attached']['js'][] = drupal_get_path('module', 'date') . '/date.js';
  309. $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  310. break;
  311. case 'date_popup':
  312. $element[$from_field]['#type'] = 'date_popup';
  313. $element[$from_field]['#theme_wrappers'] = array('date_popup');
  314. $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  315. break;
  316. default:
  317. $element[$from_field]['#type'] = 'date_text';
  318. $element[$from_field]['#theme_wrappers'] = array('date_text');
  319. $element[$from_field]['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  320. break;
  321. }
  322. // If this field uses the 'End', add matching element
  323. // for the 'End' date, and adapt titles to make it clear which
  324. // is the 'Start' and which is the 'End' .
  325. if (!empty($field['settings']['todate'])) {
  326. $element[$from_field]['#title'] = '';
  327. $element[$to_field] = $element[$from_field];
  328. $element[$to_field]['#title'] = t('to:');
  329. $element[$from_field]['#wrapper_attributes']['class'][] = 'start-date-wrapper';
  330. $element[$to_field]['#wrapper_attributes']['class'][] = 'end-date-wrapper';
  331. $element[$to_field]['#default_value'] = isset($element['#default_value'][$to_field]) ? $element['#default_value'][$to_field] : '';
  332. $element[$to_field]['#required'] = ($element[$from_field]['#required'] && $field['settings']['todate'] == 'required');
  333. $element[$to_field]['#weight'] += .2;
  334. $element[$to_field]['#prefix'] = '';
  335. // Users with JS enabled will never see initially blank values for the end
  336. // date (see Drupal.date.EndDateHandler()), so hide the message for them.
  337. $description .= '<span class="js-hide"> ' . t("Empty 'End date' values will use the 'Start date' values.") . '</span>';
  338. $element['#fieldset_description'] = $description;
  339. if ($field['settings']['todate'] == 'optional') {
  340. $element[$to_field]['#states'] = array(
  341. 'visible' => array(
  342. 'input[name="' . $show_id . '"]' => array('checked' => TRUE),
  343. ));
  344. }
  345. }
  346. else {
  347. $element[$from_field]['#description'] = $description;
  348. }
  349. // Create label for error messages that make sense in multiple values
  350. // and when the title field is left blank.
  351. if ($field['cardinality'] <> 1 && empty($field['settings']['repeat'])) {
  352. $element[$from_field]['#date_title'] = t('@field_name Start date value #@delta', array('@field_name' => $instance['label'], '@delta' => $delta + 1));
  353. if (!empty($field['settings']['todate'])) {
  354. $element[$to_field]['#date_title'] = t('@field_name End date value #@delta', array('@field_name' => $instance['label'], '@delta' => $delta + 1));
  355. }
  356. }
  357. elseif (!empty($field['settings']['todate'])) {
  358. $element[$from_field]['#date_title'] = t('@field_name Start date', array('@field_name' => $instance['label']));
  359. $element[$to_field]['#date_title'] = t('@field_name End date', array('@field_name' => $instance['label']));
  360. }
  361. else {
  362. $element[$from_field]['#date_title'] = $instance['label'];
  363. }
  364. $context = array(
  365. 'field' => $field,
  366. 'instance' => $instance,
  367. 'form' => $form,
  368. );
  369. drupal_alter('date_combo_process', $element, $form_state, $context);
  370. return $element;
  371. }
  372. function date_element_empty($element, &$form_state) {
  373. $item = array();
  374. $item['value'] = NULL;
  375. $item['value2'] = NULL;
  376. $item['timezone'] = NULL;
  377. $item['offset'] = NULL;
  378. $item['offset2'] = NULL;
  379. $item['rrule'] = NULL;
  380. form_set_value($element, $item, $form_state);
  381. return $item;
  382. }
  383. /**
  384. * Validate and update a combo element.
  385. * Don't try this if there were errors before reaching this point.
  386. */
  387. function date_combo_validate($element, &$form_state) {
  388. // Disabled and hidden elements won't have any input and don't need validation,
  389. // we just need to re-save the original values, from before they were processed into
  390. // widget arrays and timezone-adjusted.
  391. if (date_hidden_element($element) || !empty($element['#disabled'])) {
  392. form_set_value($element, $element['#date_items'], $form_state);
  393. return;
  394. }
  395. $field_name = $element['#field_name'];
  396. $delta = $element['#delta'];
  397. $langcode = $element['#language'];
  398. $form_values = drupal_array_get_nested_value($form_state['values'], $element['#field_parents']);
  399. $form_input = drupal_array_get_nested_value($form_state['input'], $element['#field_parents']);
  400. // If the whole field is empty and that's OK, stop now.
  401. if (empty($form_input[$field_name]) && !$element['#required']) {
  402. return;
  403. }
  404. $item = $form_values[$field_name][$langcode][$delta];
  405. $posted = $form_input[$field_name][$langcode][$delta];
  406. $field = field_widget_field($element, $form_state);
  407. $instance = field_widget_instance($element, $form_state);
  408. $context = array(
  409. 'field' => $field,
  410. 'instance' => $instance,
  411. 'item' => $item,
  412. );
  413. drupal_alter('date_combo_pre_validate', $element, $form_state, $context);
  414. $from_field = 'value';
  415. $to_field = 'value2';
  416. $tz_field = 'timezone';
  417. $offset_field = 'offset';
  418. $offset_field2 = 'offset2';
  419. // Check for empty 'Start date', which could either be an empty
  420. // value or an array of empty values, depending on the widget.
  421. $empty = TRUE;
  422. if (!empty($item[$from_field])) {
  423. if (!is_array($item[$from_field])) {
  424. $empty = FALSE;
  425. }
  426. else {
  427. foreach ($item[$from_field] as $key => $value) {
  428. if (!empty($value)) {
  429. $empty = FALSE;
  430. break;
  431. }
  432. }
  433. }
  434. }
  435. // An 'End' date without a 'Start' date is a validation error.
  436. if ($empty && !empty($item[$to_field])) {
  437. if (!is_array($item[$to_field])) {
  438. 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'])));
  439. $empty = FALSE;
  440. }
  441. else {
  442. foreach ($item[$to_field] as $key => $value) {
  443. if (!empty($value)) {
  444. 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'])));
  445. $empty = FALSE;
  446. break;
  447. }
  448. }
  449. }
  450. }
  451. // If the user chose the option to not show the end date, just swap in the
  452. // start date as that value so the start and end dates are the same.
  453. if ($field['settings']['todate'] == 'optional' && empty($item['show_todate'])) {
  454. $item[$to_field] = $item[$from_field];
  455. $posted[$to_field] = $posted[$from_field];
  456. }
  457. if ($empty) {
  458. $item = date_element_empty($element, $form_state);
  459. if (!$element['#required']) {
  460. return;
  461. }
  462. }
  463. // Don't look for further errors if errors are already flagged
  464. // because otherwise we'll show errors on the nested elements
  465. // more than once.
  466. elseif (!form_get_errors()) {
  467. $timezone = !empty($item[$tz_field]) ? $item[$tz_field] : $element['#date_timezone'];
  468. $timezone_db = date_get_timezone_db($field['settings']['tz_handling']);
  469. $element[$from_field]['#date_timezone'] = $timezone;
  470. $from_date = date_input_date($field, $instance, $element[$from_field], $posted[$from_field]);
  471. if (!empty($field['settings']['todate'])) {
  472. $element[$to_field]['#date_timezone'] = $timezone;
  473. $to_date = date_input_date($field, $instance, $element[$to_field], $posted[$to_field]);
  474. }
  475. else {
  476. $to_date = $from_date;
  477. }
  478. // Neither the start date nor the end date should be empty at this point
  479. // unless they held values that couldn't be evaluated.
  480. if (!$instance['required'] && (!date_is_date($from_date) || !date_is_date($to_date))) {
  481. $item = date_element_empty($element, $form_state);
  482. $errors[] = t('The dates are invalid.');
  483. }
  484. elseif (!empty($field['settings']['todate']) && $from_date > $to_date) {
  485. form_set_value($element[$to_field], $to_date, $form_state);
  486. $errors[] = t('The End date must be greater than the Start date.');
  487. }
  488. else {
  489. // Convert input dates back to their UTC values and re-format to ISO
  490. // or UNIX instead of the DATETIME format used in element processing.
  491. $item[$tz_field] = $timezone;
  492. // Update the context for changes in the $item, and allow other modules to
  493. // alter the computed local dates.
  494. $context['item'] = $item;
  495. // We can only pass two additional values to drupal_alter, so $element
  496. // needs to be included in $context.
  497. $context['element'] = $element;
  498. drupal_alter('date_combo_validate_date_start', $from_date, $form_state, $context);
  499. drupal_alter('date_combo_validate_date_end', $to_date, $form_state, $context);
  500. $item[$offset_field] = date_offset_get($from_date);
  501. $test_from = date_format($from_date, 'r');
  502. $test_to = date_format($to_date, 'r');
  503. $item[$offset_field2] = date_offset_get($to_date);
  504. date_timezone_set($from_date, timezone_open($timezone_db));
  505. date_timezone_set($to_date, timezone_open($timezone_db));
  506. $item[$from_field] = date_format($from_date, date_type_format($field['type']));
  507. $item[$to_field] = date_format($to_date, date_type_format($field['type']));
  508. if (isset($form_values[$field_name]['rrule'])) {
  509. $item['rrule'] = $form_values[$field['field_name']]['rrule'];
  510. }
  511. // If the db timezone is not the same as the display timezone
  512. // and we are using a date with time granularity,
  513. // test a roundtrip back to the original timezone to catch
  514. // invalid dates, like 2AM on the day that spring daylight savings
  515. // time begins in the US.
  516. $granularity = date_format_order($element[$from_field]['#date_format']);
  517. if ($timezone != $timezone_db && date_has_time($granularity)) {
  518. date_timezone_set($from_date, timezone_open($timezone));
  519. date_timezone_set($to_date, timezone_open($timezone));
  520. if ($test_from != date_format($from_date, 'r')) {
  521. $errors[] = t('The Start date is invalid.');
  522. }
  523. if ($test_to != date_format($to_date, 'r')) {
  524. $errors[] = t('The End date is invalid.');
  525. }
  526. }
  527. if (empty($errors)) {
  528. form_set_value($element, $item, $form_state);
  529. }
  530. }
  531. }
  532. if (!empty($errors)) {
  533. if ($field['cardinality']) {
  534. 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)));
  535. }
  536. else {
  537. form_error($element, t('There are errors in @field_name:', array('@field_name' => $instance['label'])) . theme('item_list', array('items' => $errors)));
  538. }
  539. }
  540. }
  541. /**
  542. * Determine the input format for this element.
  543. */
  544. function date_input_format($element, $field, $instance) {
  545. if (!empty($instance['widget']['settings']['input_format_custom'])) {
  546. return $instance['widget']['settings']['input_format_custom'];
  547. }
  548. elseif (!empty($instance['widget']['settings']['input_format']) && $instance['widget']['settings']['input_format'] != 'site-wide') {
  549. return $instance['widget']['settings']['input_format'];
  550. }
  551. return variable_get('date_format_short', 'm/d/Y - H:i');
  552. }
  553. /**
  554. * Implements hook_date_select_pre_validate_alter().
  555. */
  556. function date_date_select_pre_validate_alter(&$element, &$form_state, &$input) {
  557. date_empty_end_date($element, $form_state, $input);
  558. }
  559. /**
  560. * Implements hook_date_text_pre_validate_alter().
  561. */
  562. function date_date_text_pre_validate_alter(&$element, &$form_state, &$input) {
  563. date_empty_end_date($element, $form_state, $input);
  564. }
  565. /**
  566. * Implements hook_date_popup_pre_validate_alter().
  567. */
  568. function date_date_popup_pre_validate_alter(&$element, &$form_state, &$input) {
  569. date_empty_end_date($element, $form_state, $input);
  570. }
  571. /**
  572. * Helper function to clear out end date when not being used.
  573. */
  574. function date_empty_end_date(&$element, &$form_state, &$input) {
  575. // If this is the end date and the option to show an end date has not been selected,
  576. // empty the end date to surpress validation errors and stop further processing.
  577. $parents = $element['#parents'];
  578. $parent = array_pop($parents);
  579. if ($parent == 'value2') {
  580. $parent_values = drupal_array_get_nested_value($form_state['values'], $parents);
  581. if (isset($parent_values['show_todate']) && $parent_values['show_todate'] != 1) {
  582. $input = array();
  583. form_set_value($element, NULL, $form_state);
  584. }
  585. }
  586. }