date_elements.inc 28 KB

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