date_elements.inc 28 KB

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