date_popup_authored.module 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @file
  4. * Provides a datepicker for the "Authored on" date field found on node forms.
  5. */
  6. /**
  7. * Implements hook_help().
  8. */
  9. function date_popup_authored_help($section) {
  10. switch ($section) {
  11. case 'admin/help#date_popup_authored':
  12. $readme = '<p>' . t('Date Popup Authored provides a jQuery UI datepicker for the "Authored on" date field found on node forms.') . '</p>';
  13. $readme .= '<p>' . t('Please note that the Drupal 7 branch is experimental and relies on non-stable code. A stable version of Date Popup Authored will be released in conjunction with the stable releases of both Drupal 7 and Date for Drupal 7.') . '</p>';
  14. $readme .= '<p>' . t('For a full description of the module, visit <a href="@url">the project page</a>.',
  15. array(
  16. '@url' => url('http://drupal.org/project/date_popup_authored'),
  17. )) . '</p>';
  18. $readme .= '<p>' . t('To submit bug reports and feature suggestions, or to track changes, visit <a href="@url">the issue queue</a>.',
  19. array(
  20. '@url' => url('http://drupal.org/project/issues/date_popup_authored'),
  21. )) . '</p>';
  22. $readme .= '<h2>' . t('Requirements') . '</h2>';
  23. $readme .= '<ul>';
  24. $readme .= '<li>' . t('Drupal 7') . '</li>';
  25. $readme .= '<li>' . t('<a href="@url">Date</a> 7.x-1.0-alpha2 or later',
  26. array(
  27. '@url' => url('http://http://drupal.org/project/date'),
  28. )) . '</li>';
  29. $readme .= '<li>' . t('<em>Date popup</em>, part of the Date module') . '</li>';
  30. $readme .= '</ul>';
  31. $readme .= '<h2>' . t('Installation and Configuration') . '</h2>';
  32. $readme .= '<p>' . t('Install as usual. See the <a href="@url">handbook page on installing contributed modules</a> for further information.',
  33. array(
  34. '@url' => url('http://drupal.org/node/895232'),
  35. )) . '</p>';
  36. $readme .= '<p>' . t('You can change the behavior of the datepicker by going to <a href="@url">the settings page for each content type</a>.',
  37. array(
  38. '@url' => url('admin/structure/types'),
  39. )) . '</p>';
  40. $readme .= '<h2>' . t('Contact') . '</h2>';
  41. $readme .= '<p>' . t('The current maintainer is Mark Trapp. You can contact him through his <a href="@url">Drupal user page</a>.',
  42. array(
  43. '@url' => url('http://drupal.org/user/212019'),
  44. )) . '</p>';
  45. $readme .= '<h2>' . t('Acknowledgements') . '</h2>';
  46. $readme .= '<p> ' . t('Date Popup Authored was inspired by the hacks provided by <a href="@url-brice">brice</a> and <a href="@url-robloach">Rob Loach</a> in <a href="@url-issue">issue #471942</a>. It contains additional fixes to account for problems found in their solution as well as new configuration options.',
  47. array(
  48. '@url-brice' => url('http://drupal.org/user/446296'),
  49. '@url-robloach' => url('http://drupal.org/user/61114'),
  50. '@url-issue' => url('http://drupal.org/node/471942'),
  51. )) . '</p>';
  52. return $readme;
  53. }
  54. }
  55. /**
  56. * Implements hook_form_FORM_ID_alter().
  57. *
  58. * Adds additional configuration settings to the form for each content type to
  59. * control the behavior of the datepicker.
  60. */
  61. function date_popup_authored_form_node_type_form_alter(&$form, &$form_state, $form_id) {
  62. $form['additional_settings']['#attached']['js'][] = drupal_get_path('module', 'date_popup_authored') . '/date_popup_authored-node-form.js';
  63. $form['date_popup_authored'] = array(
  64. '#type' => 'fieldset',
  65. '#title' => t('Date Popup Authored settings'),
  66. '#collapsible' => TRUE,
  67. '#group' => 'additional_settings',
  68. );
  69. $form['date_popup_authored']['date_popup_authored_enabled'] = array(
  70. '#type' => 'checkbox',
  71. '#title' => t('Enable datepicker for authored on date.'),
  72. '#description' => t('The default Authored On field will be replaced with a JavaScript datepicker.'),
  73. '#default_value' => variable_get('date_popup_authored_enabled_' . $form['#node_type']->type, 1),
  74. );
  75. $form['date_popup_authored']['date_popup_authored_format'] = array(
  76. '#type' => 'select',
  77. '#title' => t('Date format'),
  78. '#description' => t('Custom date formats can be added on the <a href="@url">date and time formats page</a>.', array('@url' => url('admin/config/regional/date-time'))),
  79. '#default_value' => variable_get('date_popup_authored_format_' . $form['#node_type']->type, 'm/d/Y - H:i'),
  80. '#options' => _date_popup_authored_format_options(),
  81. '#states' => array(
  82. 'invisible' => array(
  83. 'input[name="date_popup_authored_enabled"]' => array('checked' => FALSE),
  84. ),
  85. ),
  86. );
  87. $form['date_popup_authored']['date_popup_authored_year_range'] = array(
  88. '#type' => 'textfield',
  89. '#title' => t('Year range'),
  90. '#description' => t('The range of years to provide to the user. Default is &#x00B1;3 years, i.e. 2007-2013'),
  91. '#default_value' => variable_get('date_popup_authored_year_range_' . $form['#node_type']->type, 3),
  92. '#size' => 3,
  93. '#field_prefix' => '&#x00B1;',
  94. '#field_suffix' => 'years',
  95. '#states' => array(
  96. 'invisible' => array(
  97. 'input[name="date_popup_authored_enabled"]' => array('checked' => FALSE),
  98. ),
  99. ),
  100. );
  101. }
  102. /**
  103. * Implements hook_form_BASE_FORM_ID_alter() for node_form.
  104. *
  105. * Replaces default Authored on field with a datepicker on node submission forms.
  106. */
  107. function date_popup_authored_form_node_form_alter(&$form, $form_state, $form_id) {
  108. // Date Popup Authored should modify the node submission form only if the
  109. // user is allowed to modify the authoring information and it's enabled.
  110. if (!($form['author']['#access'] && variable_get('date_popup_authored_enabled_' . $form['type']['#value'], 1))) {
  111. return;
  112. }
  113. $form['author']['date']['#type'] = 'date_popup';
  114. // If there is a date already available, rewrite it to conform to
  115. // Date Popup's expected format.
  116. if (!empty($form['author']['date']['#default_value'])) {
  117. $date = new DateObject($form['author']['date']['#default_value'], NULL, 'Y-m-d H:i:s O');
  118. $form['author']['date']['#default_value'] = $date->format('Y-m-d H:i:s');
  119. }
  120. // Set options specific to date_popup.
  121. $year_range = variable_get('date_popup_authored_year_range_' . $form['type']['#value'], 3);
  122. $form['author']['date']['#date_year_range'] = '-' . $year_range . ':+' . $year_range;
  123. $form['author']['date']['#date_format'] = variable_get('date_popup_authored_format_' . $form['type']['#value'], variable_get('date_format_short', 'm/d/Y - H:i'));
  124. // Unset options that are not relevant to date_popup
  125. unset($form['author']['date']['#maxlength']);
  126. unset($form['author']['date']['#description']);
  127. // Add an additional validate handler after date_popup builds the element.
  128. $form['author']['date']['#after_build'][] = 'date_popup_authored_element_after_build';
  129. // We need to modify date popup's data during submit
  130. // @see http://drupal.org/node/847854
  131. $form['#submit'][] = 'date_popup_authored_node_form_submit';
  132. }
  133. /**
  134. * Implements hook_node_type_delete().
  135. */
  136. function date_popup_authored_node_type_delete($info) {
  137. // Delete format configuration when node types are deleted.
  138. variable_del('date_popup_authored_enabled_' . $info->type);
  139. variable_del('date_popup_authored_format_' . $info->type);
  140. variable_del('date_popup_authored_year_range_' . $info->type);
  141. }
  142. /**
  143. * Form after build handler for the date popup element.
  144. */
  145. function date_popup_authored_element_after_build($element, &$form_state) {
  146. // Add a validate handler after the one that is added by date_popup.
  147. $element['#element_validate'][] = 'date_popup_authored_element_validate';
  148. return $element;
  149. }
  150. /**
  151. * Validate handler for the date popup element.
  152. */
  153. function date_popup_authored_element_validate($element, &$form_state) {
  154. if (date_hidden_element($element) || is_string($element['#value'])) {
  155. return;
  156. }
  157. // If an error occurred in the validation of the date popup field the date
  158. // cannot be correctly rendered as a string. In this case clear the date value
  159. // to avoid subsequent errors when the node is validated.
  160. // @see date_popup_validate()
  161. // @see node_validate()
  162. $input_exists = NULL;
  163. $input = drupal_array_get_nested_value($form_state['values'], $element['#parents'], $input_exists);
  164. $date = date_popup_input_date($element, $input);
  165. if (is_object($date) && !empty($date->errors)) {
  166. $form_state['values']['date'] = NULL;
  167. }
  168. }
  169. /**
  170. * Submits the node data with the proper post date.
  171. *
  172. * @see http://drupal.org/node/847854
  173. */
  174. function date_popup_authored_node_form_submit($form, &$form_state) {
  175. if (isset($form_state['values']['date']) && $form_state['values']['date'] instanceof DateObject) {
  176. $form_state['values']['date'] = $form_state['values']['date']->format('Y-m-d H:i:s O');
  177. }
  178. }
  179. /**
  180. * Provides an options list of date formats for the configuration form.
  181. */
  182. function _date_popup_authored_format_options() {
  183. $options = array();
  184. $date = new DateObject('now', date_default_timezone());
  185. $date_formats = system_get_date_formats();
  186. foreach ($date_formats as $type => $format_info) {
  187. $format_key = ucwords($type) . ' date format';
  188. $options[$format_key] = array();
  189. foreach (array_keys($format_info) as $format) {
  190. $options[$format_key][$format] = $date->format($format);
  191. }
  192. }
  193. return $options;
  194. }