date_api_elements.inc

  1. cis7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc
  2. cle7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc
  3. ecd7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc
  4. elmsmedia7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc
  5. harmony7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc
  6. icor7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc
  7. meedjum_blog7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc
  8. mooc7 sites/all/modules/ulmus/date/date_api/date_api_elements.inc

Date API elements themes and validation. This file is only included during the edit process to reduce memory usage.

Functions

Namesort descending Description
date_default_date Create a date object from a datetime string value.
date_parts_element Creates form elements for one or more date parts.
date_select_element_process Flexible date/time drop-down selector.
date_select_element_value_callback Element value callback for date_select element.
date_select_input_date Helper function for creating a date object out of user input.
date_select_validate Validation function for date selector.
date_text_element_process Text date input form.
date_text_element_value_callback Element value callback for date_text element.
date_text_input_date Helper function for creating a date object out of user input.
date_text_validate Validation for text input.
date_timezone_element_process Creates a timezone form element.
date_timezone_element_value_callback Element value callback for date_timezone element.
date_timezone_validate Validation for timezone input
date_year_range_element_process Process callback which creates a date_year_range form element.
date_year_range_element_value_callback Element value callback for the date_year_range form element.
date_year_range_validate Element validation function for the date_year_range form element.
_date_api_element_info Implements hook_element_info().

File

sites/all/modules/ulmus/date/date_api/date_api_elements.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Date API elements themes and validation.
  5. * This file is only included during the edit process to reduce memory usage.
  6. */
  7. /**
  8. * Implements hook_element_info().
  9. *
  10. * Parameters for date form elements, designed to have sane defaults so any
  11. * or all can be omitted.
  12. *
  13. * Fill the element #default_value with a date in datetime format,
  14. * (YYYY-MM-DD HH:MM:SS), adjusted to the proper local timezone.
  15. *
  16. * NOTE - Converting a date stored in the database from UTC to the local zone
  17. * and converting it back to UTC before storing it is not handled by this
  18. * element and must be done in pre-form and post-form processing!!
  19. *
  20. * The date_select element will create a collection of form elements, with a
  21. * separate select or textfield for each date part. The whole collection will
  22. * get reformatted back to a date value of the requested type during validation.
  23. *
  24. * The date_text element will create a textfield that can contain a whole
  25. * date or any part of a date as text. The user input value will be re-formatted
  26. * back into a date value of the requested type during validation.
  27. *
  28. * The date_timezone element will create a drop-down selector to pick a
  29. * timezone name.
  30. *
  31. * The date_year_range element will create two textfields (for users with
  32. * JavaScript enabled they will appear as drop-down selectors with an option
  33. * for custom text entry) to pick a range of years that will be passed to form
  34. * submit handlers as a single string (e.g., -3:+3).
  35. *
  36. * #date_timezone
  37. * The local timezone to be used to create this date.
  38. *
  39. * #date_format
  40. * A format string that describes the format and order of date parts to
  41. * display in the edit form for this element. This makes it possible
  42. * to show date parts in a custom order, or to leave some of them out.
  43. * Be sure to add 'A' or 'a' to get an am/pm selector. Defaults to the
  44. * short site default format.
  45. *
  46. * #date_label_position
  47. * Handling option for date part labels, like 'Year', 'Month', and 'Day',
  48. * can be 'above' the date part, 'within' it, or 'none', default is 'above' .
  49. * The 'within' option shows the label as the first option in a select list
  50. * or the default value for an empty textfield, taking up less screen space.
  51. *
  52. * #date_increment
  53. * Increment minutes and seconds by this amount, default is 1.
  54. *
  55. * #date_year_range
  56. * The number of years to go back and forward in a year selector,
  57. * default is -3:+3 (3 back and 3 forward).
  58. *
  59. * #date_text_parts
  60. * Array of date parts that should use textfields instead of selects
  61. * i.e. array('year') will format the year as a textfield and other
  62. * date parts as drop-down selects.
  63. */
  64. function _date_api_element_info() {
  65. $date_base = array(
  66. '#input' => TRUE, '#tree' => TRUE,
  67. '#date_timezone' => date_default_timezone(),
  68. '#date_flexible' => 0,
  69. '#date_format' => variable_get('date_format_short', 'm/d/Y - H:i'),
  70. '#date_text_parts' => array(),
  71. '#date_increment' => 1,
  72. '#date_year_range' => '-3:+3',
  73. '#date_label_position' => 'above',
  74. );
  75. if (module_exists('ctools')) {
  76. $date_base['#pre_render'] = array('ctools_dependent_pre_render');
  77. }
  78. $type['date_select'] = array_merge($date_base, array(
  79. '#process' => array('date_select_element_process'),
  80. '#theme_wrappers' => array('date_select'),
  81. '#value_callback' => 'date_select_element_value_callback',
  82. ));
  83. $type['date_text'] = array_merge($date_base, array(
  84. '#process' => array('date_text_element_process'),
  85. '#theme_wrappers' => array('date_text'),
  86. '#value_callback' => 'date_text_element_value_callback',
  87. ));
  88. $type['date_timezone'] = array(
  89. '#input' => TRUE, '#tree' => TRUE,
  90. '#process' => array('date_timezone_element_process'),
  91. '#theme_wrappers' => array('date_text'),
  92. '#value_callback' => 'date_timezone_element_value_callback',
  93. );
  94. $type['date_year_range'] = array(
  95. '#input' => TRUE,
  96. '#process' => array('date_year_range_element_process'),
  97. '#value_callback' => 'date_year_range_element_value_callback',
  98. '#element_validate' => array('date_year_range_validate'),
  99. );
  100. return $type;
  101. }
  102. /**
  103. * Create a date object from a datetime string value.
  104. */
  105. function date_default_date($element) {
  106. $granularity = date_format_order($element['#date_format']);
  107. $default_value = $element['#default_value'];
  108. $format = DATE_FORMAT_DATETIME;
  109. // The text and popup widgets might return less than a full datetime string.
  110. if (strlen($element['#default_value']) < 19) {
  111. switch (strlen($element['#default_value'])) {
  112. case 16:
  113. $format = 'Y-m-d H:i';
  114. break;
  115. case 13:
  116. $format = 'Y-m-d H';
  117. break;
  118. case 10:
  119. $format = 'Y-m-d';
  120. break;
  121. case 7:
  122. $format = 'Y-m';
  123. break;
  124. case 4:
  125. $format = 'Y';
  126. break;
  127. }
  128. }
  129. $date = new DateObject($default_value, $element['#date_timezone'], $format);
  130. if (is_object($date)) {
  131. $date->limitGranularity($granularity);
  132. if ($date->validGranularity($granularity, $element['#date_flexible'])) {
  133. date_increment_round($date, $element['#date_increment']);
  134. }
  135. return $date;
  136. }
  137. return NULL;
  138. }
  139. /**
  140. * Process callback which creates a date_year_range form element.
  141. */
  142. function date_year_range_element_process($element, &$form_state, $form) {
  143. // Year range is stored in the -3:+3 format, but collected as two separate
  144. // textfields.
  145. $element['years_back'] = array(
  146. '#type' => 'textfield',
  147. '#title' => t('Starting year'),
  148. '#default_value' => $element['#value']['years_back'],
  149. '#size' => 10,
  150. '#maxsize' => 10,
  151. '#attributes' => array('class' => array('select-list-with-custom-option', 'back')),
  152. '#description' => t('Enter a relative value (-9, +9) or an absolute year such as 2015.'),
  153. );
  154. $element['years_forward'] = array(
  155. '#type' => 'textfield',
  156. '#title' => t('Ending year'),
  157. '#default_value' => $element['#value']['years_forward'],
  158. '#size' => 10,
  159. '#maxsize' => 10,
  160. '#attributes' => array('class' => array('select-list-with-custom-option', 'forward')),
  161. '#description' => t('Enter a relative value (-9, +9) or an absolute year such as 2015.'),
  162. );
  163. $element['#tree'] = TRUE;
  164. $element['#attached']['js'][] = drupal_get_path('module', 'date_api') . '/date_year_range.js';
  165. $context = array(
  166. 'form' => $form,
  167. );
  168. drupal_alter('date_year_range_process', $element, $form_state, $context);
  169. return $element;
  170. }
  171. /**
  172. * Element value callback for the date_year_range form element.
  173. */
  174. function date_year_range_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  175. // Convert the element's default value from a string to an array (to match
  176. // what we will get from the two textfields when the form is submitted).
  177. if ($input === FALSE) {
  178. list($years_back, $years_forward) = explode(':', $element['#default_value']);
  179. return array(
  180. 'years_back' => $years_back,
  181. 'years_forward' => $years_forward,
  182. );
  183. }
  184. }
  185. /**
  186. * Element validation function for the date_year_range form element.
  187. */
  188. function date_year_range_validate(&$element, &$form_state) {
  189. // Recombine the two submitted form values into the -3:+3 format we will
  190. // validate and save.
  191. $year_range_submitted = drupal_array_get_nested_value($form_state['values'], $element['#parents']);
  192. $year_range = $year_range_submitted['years_back'] . ':' . $year_range_submitted['years_forward'];
  193. drupal_array_set_nested_value($form_state['values'], $element['#parents'], $year_range);
  194. if (!date_range_valid($year_range)) {
  195. form_error($element['years_back'], t('Starting year must be in the format -9, or an absolute year such as 1980.'));
  196. form_error($element['years_forward'], t('Ending year must be in the format +9, or an absolute year such as 2030.'));
  197. }
  198. }
  199. /**
  200. * Element value callback for date_timezone element.
  201. */
  202. function date_timezone_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  203. $return = '';
  204. if ($input !== FALSE) {
  205. $return = $input;
  206. }
  207. elseif (!empty($element['#default_value'])) {
  208. $return = array('timezone' => $element['#default_value']);
  209. }
  210. return $return;
  211. }
  212. /**
  213. * Creates a timezone form element.
  214. *
  215. * @param array $element
  216. * The timezone form element.
  217. *
  218. * @return array
  219. * the timezone form element
  220. */
  221. function date_timezone_element_process($element, &$form_state, $form) {
  222. if (date_hidden_element($element)) {
  223. return $element;
  224. }
  225. $element['#tree'] = TRUE;
  226. $label = theme('date_part_label_timezone', array('part_type' => 'select', 'element' => $element));
  227. $element['timezone'] = array(
  228. '#type' => 'select',
  229. '#title' => $label,
  230. '#title_display' => $element['#date_label_position'] == 'above' ? 'before' : 'invisible',
  231. '#options' => date_timezone_names($element['#required']),
  232. '#value' => $element['#value'],
  233. '#weight' => $element['#weight'],
  234. '#required' => $element['#required'],
  235. '#theme' => 'date_select_element',
  236. '#theme_wrappers' => array('form_element'),
  237. );
  238. if (isset($element['#element_validate'])) {
  239. array_push($element['#element_validate'], 'date_timezone_validate');
  240. }
  241. else {
  242. $element['#element_validate'] = array('date_timezone_validate');
  243. }
  244. $context = array(
  245. 'form' => $form,
  246. );
  247. drupal_alter('date_timezone_process', $element, $form_state, $context);
  248. return $element;
  249. }
  250. /**
  251. * Validation for timezone input
  252. *
  253. * Move the timezone value from the nested field back to the original field.
  254. */
  255. function date_timezone_validate($element, &$form_state) {
  256. if (date_hidden_element($element)) {
  257. return;
  258. }
  259. form_set_value($element, $element['#value']['timezone'], $form_state);
  260. }
  261. /**
  262. * Element value callback for date_text element.
  263. */
  264. function date_text_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  265. $return = array('date' => '');
  266. $date = NULL;
  267. // Normal input from submitting the form element.
  268. // Check is_array() to skip the string input values created by Views pagers.
  269. // Those string values, if present, should be interpreted as empty input.
  270. if ($input != FALSE && is_array($input)) {
  271. $return = $input;
  272. $date = date_text_input_date($element, $input);
  273. }
  274. // No input? Try the default value.
  275. elseif (!empty($element['#default_value'])) {
  276. $date = date_default_date($element);
  277. }
  278. if (date_is_date($date)) {
  279. $return['date'] = date_format_date($date, 'custom', $element['#date_format']);
  280. }
  281. return $return;
  282. }
  283. /**
  284. * Text date input form.
  285. *
  286. * Display all or part of a date in a single textfield.
  287. *
  288. * The exact parts displayed in the field are those in #date_granularity.
  289. * The display of each part comes from #date_format.
  290. *
  291. */
  292. function date_text_element_process($element, &$form_state, $form) {
  293. if (date_hidden_element($element)) {
  294. return $element;
  295. }
  296. $element['#tree'] = TRUE;
  297. $element['#theme_wrappers'] = array('date_text');
  298. $element['date']['#value'] = $element['#value']['date'];
  299. $element['date']['#type'] = 'textfield';
  300. $element['date']['#weight'] = !empty($element['date']['#weight']) ? $element['date']['#weight'] : $element['#weight'];
  301. $element['date']['#attributes'] = array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-date') : array('date-date'));
  302. $now = date_example_date();
  303. $element['date']['#title'] = t('Date');
  304. $element['date']['#title_display'] = 'invisible';
  305. $element['date']['#description'] = ' ' . t('Format: @date', array('@date' => date_format_date(date_example_date(), 'custom', $element['#date_format'])));
  306. $element['date']['#ajax'] = !empty($element['#ajax']) ? $element['#ajax'] : FALSE;
  307. // Keep the system from creating an error message for the sub-element.
  308. // We'll set our own message on the parent element.
  309. // $element['date']['#required'] = $element['#required'];
  310. $element['date']['#theme'] = 'date_textfield_element';
  311. if (isset($element['#element_validate'])) {
  312. array_push($element['#element_validate'], 'date_text_validate');
  313. }
  314. else {
  315. $element['#element_validate'] = array('date_text_validate');
  316. }
  317. if (!empty($element['#force_value'])) {
  318. $element['date']['#value'] = $element['date']['#default_value'];
  319. }
  320. $context = array(
  321. 'form' => $form,
  322. );
  323. drupal_alter('date_text_process', $element, $form_state, $context);
  324. return $element;
  325. }
  326. /**
  327. * Validation for text input.
  328. *
  329. * When used as a Views widget, the validation step always gets triggered,
  330. * even with no form submission. Before form submission $element['#value']
  331. * contains a string, after submission it contains an array.
  332. *
  333. */
  334. function date_text_validate($element, &$form_state) {
  335. if (date_hidden_element($element)) {
  336. return;
  337. }
  338. if (is_string($element['#value'])) {
  339. return;
  340. }
  341. $input_exists = NULL;
  342. $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  343. drupal_alter('date_text_pre_validate', $element, $form_state, $input);
  344. $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  345. $date = date_text_input_date($element, $input);
  346. // If the field has errors, display them.
  347. // If something was input but there is no date, the date is invalid.
  348. // If the field is empty and required, set error message and return.
  349. $error_field = implode('][', $element['#parents']);
  350. if (empty($date) || !empty($date->errors)) {
  351. if (is_object($date) && !empty($date->errors)) {
  352. $message = t('The value input for field %field is invalid:', array('%field' => $label));
  353. $message .= '<br />' . implode('<br />', $date->errors);
  354. form_set_error($error_field, $message);
  355. return;
  356. }
  357. if (!empty($element['#required'])) {
  358. $message = t('A valid date is required for %title.', array('%title' => $label));
  359. form_set_error($error_field, $message);
  360. return;
  361. }
  362. // Fall through, some other error.
  363. if (!empty($input['date'])) {
  364. form_error($element, t('%title is invalid.', array('%title' => $label)));
  365. return;
  366. }
  367. }
  368. $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  369. form_set_value($element, $value, $form_state);
  370. }
  371. /**
  372. * Helper function for creating a date object out of user input.
  373. */
  374. function date_text_input_date($element, $input) {
  375. if (empty($input) || !is_array($input) || !array_key_exists('date', $input) || empty($input['date'])) {
  376. return NULL;
  377. }
  378. $granularity = date_format_order($element['#date_format']);
  379. $date = new DateObject($input['date'], $element['#date_timezone'], $element['#date_format']);
  380. if (is_object($date)) {
  381. $date->limitGranularity($granularity);
  382. if ($date->validGranularity($granularity, $element['#date_flexible'])) {
  383. date_increment_round($date, $element['#date_increment']);
  384. }
  385. return $date;
  386. }
  387. return NULL;
  388. }
  389. /**
  390. * Element value callback for date_select element.
  391. */
  392. function date_select_element_value_callback($element, $input = FALSE, &$form_state = array()) {
  393. $return = array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '');
  394. $date = NULL;
  395. if ($input !== FALSE) {
  396. $return = $input;
  397. $date = date_select_input_date($element, $input);
  398. }
  399. elseif (!empty($element['#default_value'])) {
  400. $date = date_default_date($element);
  401. }
  402. $granularity = date_format_order($element['#date_format']);
  403. $formats = array('year' => 'Y', 'month' => 'n', 'day' => 'j', 'hour' => 'H', 'minute' => 'i', 'second' => 's');
  404. foreach ($granularity as $field) {
  405. if ($field != 'timezone') {
  406. $return[$field] = date_is_date($date) ? $date->format($formats[$field]) : '';
  407. }
  408. }
  409. return $return;
  410. }
  411. /**
  412. * Flexible date/time drop-down selector.
  413. *
  414. * Splits date into a collection of date and time sub-elements, one
  415. * for each date part. Each sub-element can be either a textfield or a
  416. * select, based on the value of ['#date_settings']['text_fields'].
  417. *
  418. * The exact parts displayed in the field are those in #date_granularity.
  419. * The display of each part comes from ['#date_settings']['format'].
  420. *
  421. */
  422. function date_select_element_process($element, &$form_state, $form) {
  423. if (date_hidden_element($element)) {
  424. return $element;
  425. }
  426. $date = NULL;
  427. $granularity = date_format_order($element['#date_format']);
  428. if (is_array($element['#default_value'])) {
  429. $date = date_select_input_date($element, $element['#default_value']);
  430. }
  431. elseif (!empty($element['#default_value'])) {
  432. $date = date_default_date($element);
  433. }
  434. $element['#tree'] = TRUE;
  435. $element['#theme_wrappers'] = array('date_select');
  436. $element += (array) date_parts_element($element, $date, $element['#date_format']);
  437. // Store a hidden value for all date parts not in the current display.
  438. $granularity = date_format_order($element['#date_format']);
  439. $formats = array('year' => 'Y', 'month' => 'n', 'day' => 'j', 'hour' => 'H', 'minute' => 'i', 'second' => 's');
  440. foreach (date_nongranularity($granularity) as $field) {
  441. if ($field != 'timezone') {
  442. $element[$field] = array(
  443. '#type' => 'value',
  444. '#value' => 0,
  445. );
  446. }
  447. }
  448. if (isset($element['#element_validate'])) {
  449. array_push($element['#element_validate'], 'date_select_validate');
  450. }
  451. else {
  452. $element['#element_validate'] = array('date_select_validate');
  453. }
  454. $context = array(
  455. 'form' => $form,
  456. );
  457. drupal_alter('date_select_process', $element, $form_state, $context);
  458. return $element;
  459. }
  460. /**
  461. * Creates form elements for one or more date parts.
  462. *
  463. * Get the order of date elements from the provided format.
  464. * If the format order omits any date parts in the granularity, alter the
  465. * granularity array to match the format, then flip the $order array
  466. * to get the position for each element. Then iterate through the
  467. * elements and create a sub-form for each part.
  468. *
  469. * @param array $element
  470. * The date element.
  471. * @param object $date
  472. * The date object.
  473. * @param string $format
  474. * A date format string.
  475. *
  476. * @return array
  477. * The form array for the submitted date parts.
  478. */
  479. function date_parts_element($element, $date, $format) {
  480. $granularity = date_format_order($format);
  481. $sub_element = array('#granularity' => $granularity);
  482. $order = array_flip($granularity);
  483. $hours_format = strpos(strtolower($element['#date_format']), 'a') ? 'g': 'G';
  484. $month_function = strpos($element['#date_format'], 'F') !== FALSE ? 'date_month_names' : 'date_month_names_abbr';
  485. $count = 0;
  486. $increment = min(intval($element['#date_increment']), 1);
  487. // Allow empty value as option if date is not required or there is no date.
  488. $part_required = (bool) $element['#required'] && is_object($date);
  489. foreach ($granularity as $field) {
  490. $part_type = in_array($field, $element['#date_text_parts']) ? 'textfield' : 'select';
  491. $sub_element[$field] = array(
  492. '#weight' => $order[$field],
  493. '#required' => $part_required,
  494. '#attributes' => array('class' => isset($element['#attributes']['class']) ? $element['#attributes']['class'] += array('date-' . $field) : array('date-' . $field)),
  495. '#ajax' => !empty($element['#ajax']) ? $element['#ajax'] : FALSE,
  496. );
  497. switch ($field) {
  498. case 'year':
  499. $range = date_range_years($element['#date_year_range'], $date);
  500. $min_year = $range[0];
  501. $max_year = $range[1];
  502. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('Y') : '';
  503. if ($part_type == 'select') {
  504. $sub_element[$field]['#options'] = drupal_map_assoc(date_years($min_year, $max_year, $part_required));
  505. }
  506. break;
  507. case 'month':
  508. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('n') : '';
  509. if ($part_type == 'select') {
  510. $sub_element[$field]['#options'] = $month_function($part_required);
  511. }
  512. break;
  513. case 'day':
  514. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('j') : '';
  515. if ($part_type == 'select') {
  516. $sub_element[$field]['#options'] = drupal_map_assoc(date_days($part_required));
  517. }
  518. break;
  519. case 'hour':
  520. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format($hours_format) : '';
  521. if ($part_type == 'select') {
  522. $sub_element[$field]['#options'] = drupal_map_assoc(date_hours($hours_format, $part_required));
  523. }
  524. $sub_element[$field]['#prefix'] = theme('date_part_hour_prefix', $element);
  525. break;
  526. case 'minute':
  527. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('i') : '';
  528. if ($part_type == 'select') {
  529. $sub_element[$field]['#options'] = drupal_map_assoc(date_minutes('i', $part_required, $element['#date_increment']));
  530. }
  531. $sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
  532. break;
  533. case 'second':
  534. $sub_element[$field]['#default_value'] = is_object($date) ? $date->format('s') : '';
  535. if ($part_type == 'select') {
  536. $sub_element[$field]['#options'] = drupal_map_assoc(date_seconds('s', $part_required, $element['#date_increment']));
  537. }
  538. $sub_element[$field]['#prefix'] = theme('date_part_minsec_prefix', $element);
  539. break;
  540. }
  541. // Add handling for the date part label.
  542. $label = theme('date_part_label_' . $field, array('part_type' => $part_type, 'element' => $element));
  543. if (in_array($field, $element['#date_text_parts'])) {
  544. $sub_element[$field]['#type'] = 'textfield';
  545. $sub_element[$field]['#theme'] = 'date_textfield_element';
  546. $sub_element[$field]['#size'] = 7;
  547. $sub_element[$field]['#title'] = $label;
  548. $sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
  549. if ($element['#date_label_position'] == 'within') {
  550. if (!empty($sub_element[$field]['#options']) && is_array($sub_element[$field]['#options'])) {
  551. $sub_element[$field]['#options'] = array(
  552. '-' . $label => '-' . $label) + $sub_element[$field]['#options'];
  553. }
  554. if (empty($sub_element[$field]['#default_value'])) {
  555. $sub_element[$field]['#default_value'] = '-' . $label;
  556. }
  557. }
  558. }
  559. else {
  560. $sub_element[$field]['#type'] = 'select';
  561. $sub_element[$field]['#theme'] = 'date_select_element';
  562. $sub_element[$field]['#title'] = $label;
  563. $sub_element[$field]['#title_display'] = in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before';
  564. if ($element['#date_label_position'] == 'within') {
  565. $sub_element[$field]['#options'] = array(
  566. '' => '-' . $label) + $sub_element[$field]['#options'];
  567. }
  568. }
  569. }
  570. // Views exposed filters are treated as submitted even if not,
  571. // so force the #default value in that case. Make sure we set
  572. // a default that is in the option list.
  573. if (!empty($element['#force_value'])) {
  574. $options = $sub_element[$field]['#options'];
  575. $default = !empty($sub_element[$field]['#default_value']) ? $sub_element[$field]['#default_value'] : array_shift($options);
  576. $sub_element[$field]['#value'] = $default;
  577. }
  578. if (($hours_format == 'g' || $hours_format == 'h') && date_has_time($granularity)) {
  579. $label = theme('date_part_label_ampm', array('part_type' => 'ampm', 'element' => $element));
  580. $sub_element['ampm'] = array(
  581. '#type' => 'select',
  582. '#theme' => 'date_select_element',
  583. '#title' => $label,
  584. '#title_display' => in_array($element['#date_label_position'], array('within', 'none')) ? 'invisible' : 'before',
  585. '#default_value' => is_object($date) ? (date_format($date, 'G') >= 12 ? 'pm' : 'am') : '',
  586. '#options' => drupal_map_assoc(date_ampm($part_required)),
  587. '#required' => $part_required,
  588. '#weight' => 8,
  589. '#attributes' => array('class' => array('date-ampm')),
  590. );
  591. if ($element['#date_label_position'] == 'within') {
  592. $sub_element['ampm']['#options'] = array('' => '-' . $label) + $sub_element['ampm']['#options'];
  593. }
  594. }
  595. return $sub_element;
  596. }
  597. /**
  598. * Validation function for date selector.
  599. *
  600. * When used as a Views widget, the validation step always gets triggered,
  601. * even with no form submission. Before form submission $element['#value']
  602. * contains a string, after submission it contains an array.
  603. */
  604. function date_select_validate($element, &$form_state) {
  605. if (date_hidden_element($element)) {
  606. return;
  607. }
  608. if (is_string($element['#value'])) {
  609. return;
  610. }
  611. $input_exists = NULL;
  612. $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  613. // Strip field labels out of the results.
  614. foreach ($element['#value'] as $field => $field_value) {
  615. if (substr($field_value, 0, 1) == '-') {
  616. $input[$field] = '';
  617. }
  618. }
  619. drupal_alter('date_select_pre_validate', $element, $form_state, $input);
  620. $label = !empty($element['#date_title']) ? $element['#date_title'] : (!empty($element['#title']) ? $element['#title'] : '');
  621. if (isset($input['ampm'])) {
  622. if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
  623. $input['hour'] += 12;
  624. }
  625. elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
  626. $input['hour'] -= 12;
  627. }
  628. }
  629. unset($input['ampm']);
  630. $date = date_select_input_date($element, $input);
  631. // If the field has errors, display them.
  632. $error_field = implode('][', $element['#parents']);
  633. $entered = array_values(array_filter($input));
  634. if (empty($date) || !empty($date->errors)) {
  635. // The input created a date but it has errors.
  636. if (is_object($date) && !empty($date->errors)) {
  637. $message = t('The value input for field %field is invalid:', array('%field' => $label));
  638. $message .= '<br />' . implode('<br />', $date->errors);
  639. form_set_error($error_field, $message);
  640. return;
  641. }
  642. // Nothing was entered but the date is required.
  643. elseif (empty($entered) && $element['#required']) {
  644. $message = t('A valid date is required for %title.', array('%title' => $label));
  645. form_set_error($error_field, $message);
  646. return;
  647. }
  648. // Something was input but it wasn't enough to create a valid date.
  649. elseif (!empty($entered)) {
  650. $message = t('The value input for field %field is invalid.', array('%field' => $label));
  651. form_set_error($error_field, $message);
  652. return;
  653. }
  654. }
  655. $value = !empty($date) ? $date->format(DATE_FORMAT_DATETIME) : NULL;
  656. form_set_value($element, $value, $form_state);
  657. }
  658. /**
  659. * Helper function for creating a date object out of user input.
  660. */
  661. function date_select_input_date($element, $input) {
  662. // Was anything entered? If not, we have no date.
  663. if (!is_array($input)) {
  664. return NULL;
  665. }
  666. else {
  667. $entered = array_values(array_filter($input));
  668. if (empty($entered)) {
  669. return NULL;
  670. }
  671. }
  672. $granularity = date_format_order($element['#date_format']);
  673. if (isset($input['ampm'])) {
  674. if ($input['ampm'] == 'pm' && $input['hour'] < 12) {
  675. $input['hour'] += 12;
  676. }
  677. elseif ($input['ampm'] == 'am' && $input['hour'] == 12) {
  678. $input['hour'] -= 12;
  679. }
  680. }
  681. unset($input['ampm']);
  682. // Make the input match the granularity.
  683. foreach (date_nongranularity($granularity) as $part) {
  684. unset($input[$part]);
  685. }
  686. $date = new DateObject($input, $element['#date_timezone']);
  687. if (is_object($date)) {
  688. $date->limitGranularity($granularity);
  689. if ($date->validGranularity($granularity, $element['#date_flexible'])) {
  690. date_increment_round($date, $element['#date_increment']);
  691. }
  692. return $date;
  693. }
  694. return NULL;
  695. }