datetime_range.module 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. /**
  3. * @file
  4. * Field hooks to implement a datetime field that stores a start and end date.
  5. */
  6. use Drupal\Core\Routing\RouteMatchInterface;
  7. use Drupal\views\Views;
  8. use Drupal\views\ViewEntityInterface;
  9. /**
  10. * Implements hook_help().
  11. */
  12. function datetime_range_help($route_name, RouteMatchInterface $route_match) {
  13. switch ($route_name) {
  14. case 'help.page.datetime_range':
  15. $output = '';
  16. $output .= '<h3>' . t('About') . '</h3>';
  17. $output .= '<p>' . t('The Datetime Range module provides a Date field that stores start dates and times, as well as end dates and times. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI module help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":datetime_do">online documentation for the Datetime Range module</a>.', [':field' => \Drupal::url('help.page', ['name' => 'field']), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#', ':datetime_do' => 'https://www.drupal.org/documentation/modules/datetime_range']) . '</p>';
  18. $output .= '<h3>' . t('Uses') . '</h3>';
  19. $output .= '<dl>';
  20. $output .= '<dt>' . t('Managing and displaying date fields') . '</dt>';
  21. $output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the Date field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? \Drupal::url('help.page', ['name' => 'field_ui']) : '#']) . '</dd>';
  22. $output .= '<dt>' . t('Displaying dates') . '</dt>';
  23. $output .= '<dd>' . t('Dates can be displayed using the <em>Plain</em> or the <em>Default</em> formatter. The <em>Plain</em> formatter displays the date in the <a href="http://en.wikipedia.org/wiki/ISO_8601">ISO 8601</a> format. If you choose the <em>Default</em> formatter, you can choose a format from a predefined list that can be managed on the <a href=":date_format_list">Date and time formats</a> page.', [':date_format_list' => \Drupal::url('entity.date_format.collection')]) . '</dd>';
  24. $output .= '</dl>';
  25. return $output;
  26. }
  27. }
  28. /**
  29. * Implements hook_view_presave().
  30. *
  31. * When a view is saved using the old string or standard plugin format for
  32. * Datetime Range filters or sorts, they will automatically be updated to
  33. * Datetime filters or sorts. Old plugins usage must to be considered
  34. * deprecated and must be converted before 9.0.0, when this updating layer will
  35. * be removed.
  36. *
  37. * @deprecated in Drupal 8.5.x and will be removed before 9.0.0.
  38. *
  39. * @see https://www.drupal.org/node/2857691
  40. */
  41. function datetime_range_view_presave(ViewEntityInterface $view) {
  42. $config_factory = \Drupal::configFactory();
  43. $displays = $view->get('display');
  44. $changed = FALSE;
  45. foreach ($displays as $display_name => &$display) {
  46. // Update datetime_range filters.
  47. if (isset($display['display_options']['filters'])) {
  48. foreach ($display['display_options']['filters'] as $field_name => &$filter) {
  49. if ($filter['plugin_id'] === 'string') {
  50. $table_data = Views::viewsData()->get($filter['table']);
  51. if (!$table_data) {
  52. continue;
  53. }
  54. // Get field config.
  55. $filter_views_data = $table_data[$filter['field']]['filter'];
  56. if (!isset($filter_views_data['entity_type']) || !isset($filter_views_data['field_name'])) {
  57. continue;
  58. }
  59. $field_storage_name = 'field.storage.' . $filter_views_data['entity_type'] . '.' . $filter_views_data['field_name'];
  60. $field_configuration = $config_factory->get($field_storage_name);
  61. if ($field_configuration->get('type') === 'daterange') {
  62. // Set entity_type if missing.
  63. if (!isset($filter['entity_type'])) {
  64. $filter['entity_type'] = $filter_views_data['entity_type'];
  65. }
  66. // Set datetime plugin_id.
  67. $filter['plugin_id'] = 'datetime';
  68. // Create datetime value array.
  69. $datetime_value = [
  70. 'min' => '',
  71. 'max' => '',
  72. 'value' => $filter['value'],
  73. 'type' => 'date',
  74. ];
  75. // Map string operator/value to numeric equivalent.
  76. switch ($filter['operator']) {
  77. case '=':
  78. case 'empty':
  79. case 'not empty':
  80. $operator = $filter['operator'];
  81. break;
  82. case '!=':
  83. case 'not':
  84. $operator = '!=';
  85. break;
  86. case 'starts':
  87. $operator = 'regular_expression';
  88. $datetime_value['value'] = '^' . preg_quote($datetime_value['value']);
  89. break;
  90. case 'ends':
  91. $operator = 'regular_expression';
  92. $datetime_value['value'] = preg_quote($datetime_value['value']) . '$';
  93. break;
  94. default:
  95. $operator = 'regular_expression';
  96. // Add .* to prevent blank regexes.
  97. if (empty($datetime_value['value'])) {
  98. $datetime_value['value'] = '.*';
  99. }
  100. else {
  101. $datetime_value['value'] = preg_quote($datetime_value['value']);
  102. }
  103. }
  104. // Set value and operator.
  105. $filter['value'] = $datetime_value;
  106. $filter['operator'] = $operator;
  107. $changed = TRUE;
  108. @trigger_error('Use of string filters for datetime_range fields is deprecated. Use the datetime filters instead. See https://www.drupal.org/node/2857691', E_USER_DEPRECATED);
  109. }
  110. }
  111. }
  112. }
  113. // Update datetime_range sort handlers.
  114. if (isset($display['display_options']['sorts'])) {
  115. foreach ($display['display_options']['sorts'] as $field_name => &$sort) {
  116. if ($sort['plugin_id'] === 'standard') {
  117. $table_data = Views::viewsData()->get($sort['table']);
  118. if (!$table_data) {
  119. continue;
  120. }
  121. // Get field config.
  122. $sort_views_data = $table_data[$sort['field']]['sort'];
  123. if (!isset($sort_views_data['entity_type']) || !isset($sort_views_data['field_name'])) {
  124. continue;
  125. }
  126. $field_storage_name = 'field.storage.' . $sort_views_data['entity_type'] . '.' . $sort_views_data['field_name'];
  127. $field_configuration = $config_factory->get($field_storage_name);
  128. if ($field_configuration->get('type') === 'daterange') {
  129. // Set entity_type if missing.
  130. if (!isset($sort['entity_type'])) {
  131. $sort['entity_type'] = $sort_views_data['entity_type'];
  132. }
  133. // Set datetime plugin_id.
  134. $sort['plugin_id'] = 'datetime';
  135. $changed = TRUE;
  136. @trigger_error('Use of standard sort handlers for datetime_range fields is deprecated. Use the datetime sort handlers instead. See https://www.drupal.org/node/2857691', E_USER_DEPRECATED);
  137. }
  138. }
  139. }
  140. }
  141. }
  142. if ($changed) {
  143. $view->set('display', $displays);
  144. }
  145. }