"Fossies" - the Fresh Open Source Software Archive

Member "drupal-8.9.10/core/modules/datetime_range/datetime_range.module" (26 Nov 2020, 7423 Bytes) of package /linux/www/drupal-8.9.10.tar.gz:


As a special service "Fossies" has tried to format the requested text file into HTML format (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 <?php
    2 
    3 /**
    4  * @file
    5  * Field hooks to implement a datetime field that stores a start and end date.
    6  */
    7 
    8 use Drupal\Core\Url;
    9 use Drupal\Core\Routing\RouteMatchInterface;
   10 use Drupal\views\Views;
   11 use Drupal\views\ViewEntityInterface;
   12 
   13 /**
   14  * Implements hook_help().
   15  */
   16 function datetime_range_help($route_name, RouteMatchInterface $route_match) {
   17   switch ($route_name) {
   18     case 'help.page.datetime_range':
   19       $output = '';
   20       $output .= '<h3>' . t('About') . '</h3>';
   21       $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' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#', ':datetime_do' => 'https://www.drupal.org/documentation/modules/datetime_range']) . '</p>';
   22       $output .= '<h3>' . t('Uses') . '</h3>';
   23       $output .= '<dl>';
   24       $output .= '<dt>' . t('Managing and displaying date fields') . '</dt>';
   25       $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')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#']) . '</dd>';
   26       $output .= '<dt>' . t('Displaying dates') . '</dt>';
   27       $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' => Url::fromRoute('entity.date_format.collection')->toString()]) . '</dd>';
   28       $output .= '</dl>';
   29       return $output;
   30   }
   31 }
   32 
   33 /**
   34  * Implements hook_ENTITY_TYPE_presave().
   35  *
   36  * When a view is saved using the old string or standard plugin format for
   37  * Datetime Range filters or sorts, they will automatically be updated to
   38  * Datetime filters or sorts. Old plugins usage must to be considered
   39  * deprecated and must be converted before 9.0.0, when this updating layer will
   40  * be removed.
   41  *
   42  * @deprecated in drupal:8.5.0 and is removed from drupal:9.0.0.
   43  *
   44  * @see https://www.drupal.org/node/2857691
   45  */
   46 function datetime_range_view_presave(ViewEntityInterface $view) {
   47   $config_factory = \Drupal::configFactory();
   48   $displays = $view->get('display');
   49   $changed = FALSE;
   50 
   51   foreach ($displays as $display_name => &$display) {
   52 
   53     // Update datetime_range filters.
   54     if (isset($display['display_options']['filters'])) {
   55       foreach ($display['display_options']['filters'] as $field_name => &$filter) {
   56         if ($filter['plugin_id'] === 'string') {
   57           $table_data = Views::viewsData()->get($filter['table']);
   58           if (!$table_data) {
   59             continue;
   60           }
   61 
   62           // Get field config.
   63           $filter_views_data = $table_data[$filter['field']]['filter'];
   64           if (!isset($filter_views_data['entity_type']) || !isset($filter_views_data['field_name'])) {
   65             continue;
   66           }
   67           $field_storage_name = 'field.storage.' . $filter_views_data['entity_type'] . '.' . $filter_views_data['field_name'];
   68           $field_configuration = $config_factory->get($field_storage_name);
   69 
   70           if ($field_configuration->get('type') === 'daterange') {
   71 
   72             // Set entity_type if missing.
   73             if (!isset($filter['entity_type'])) {
   74               $filter['entity_type'] = $filter_views_data['entity_type'];
   75             }
   76 
   77             // Set datetime plugin_id.
   78             $filter['plugin_id'] = 'datetime';
   79 
   80             // Create datetime value array.
   81             $datetime_value = [
   82               'min' => '',
   83               'max' => '',
   84               'value' => $filter['value'],
   85               'type' => 'date',
   86             ];
   87 
   88             // Map string operator/value to numeric equivalent.
   89             switch ($filter['operator']) {
   90               case '=':
   91               case 'empty':
   92               case 'not empty':
   93                 $operator = $filter['operator'];
   94                 break;
   95 
   96               case '!=':
   97               case 'not':
   98                 $operator = '!=';
   99                 break;
  100 
  101               case 'starts':
  102                 $operator = 'regular_expression';
  103                 $datetime_value['value'] = '^' . preg_quote($datetime_value['value']);
  104                 break;
  105 
  106               case 'ends':
  107                 $operator = 'regular_expression';
  108                 $datetime_value['value'] = preg_quote($datetime_value['value']) . '$';
  109                 break;
  110 
  111               default:
  112                 $operator = 'regular_expression';
  113                 // Add .* to prevent blank regexes.
  114                 if (empty($datetime_value['value'])) {
  115                   $datetime_value['value'] = '.*';
  116                 }
  117                 else {
  118                   $datetime_value['value'] = preg_quote($datetime_value['value']);
  119                 }
  120             }
  121 
  122             // Set value and operator.
  123             $filter['value'] = $datetime_value;
  124             $filter['operator'] = $operator;
  125             $changed = TRUE;
  126             @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);
  127           }
  128         }
  129       }
  130     }
  131 
  132     // Update datetime_range sort handlers.
  133     if (isset($display['display_options']['sorts'])) {
  134       foreach ($display['display_options']['sorts'] as $field_name => &$sort) {
  135         if ($sort['plugin_id'] === 'standard') {
  136           $table_data = Views::viewsData()->get($sort['table']);
  137           if (!$table_data) {
  138             continue;
  139           }
  140 
  141           // Get field config.
  142           $sort_views_data = $table_data[$sort['field']]['sort'];
  143           if (!isset($sort_views_data['entity_type']) || !isset($sort_views_data['field_name'])) {
  144             continue;
  145           }
  146           $field_storage_name = 'field.storage.' . $sort_views_data['entity_type'] . '.' . $sort_views_data['field_name'];
  147           $field_configuration = $config_factory->get($field_storage_name);
  148 
  149           if ($field_configuration->get('type') === 'daterange') {
  150 
  151             // Set entity_type if missing.
  152             if (!isset($sort['entity_type'])) {
  153               $sort['entity_type'] = $sort_views_data['entity_type'];
  154             }
  155 
  156             // Set datetime plugin_id.
  157             $sort['plugin_id'] = 'datetime';
  158             $changed = TRUE;
  159             @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);
  160           }
  161         }
  162       }
  163     }
  164   }
  165 
  166   if ($changed) {
  167     $view->set('display', $displays);
  168   }
  169 }