You are here

public static function CalendarHelper::dateViewFields in Calendar 8

Helper for identifying Date API fields for views.

This is a ported version of date_views_fields() in date_views module in D7.

Parameters

string $base:

Return value

array

1 call to CalendarHelper::dateViewFields()
Calendar::preRender in src/Plugin/views/row/Calendar.php
Allow the style to do stuff before each row is rendered.

File

src/CalendarHelper.php, line 406

Class

CalendarHelper
Defines Gregorian Calendar date values.

Namespace

Drupal\calendar

Code

public static function dateViewFields($base = 'node') {

  // Make sure $base is never empty.
  if (empty($base)) {
    $base = 'node';
  }
  $cid = 'date_views_fields_' . $base;

  // cache_clear_all($cid, 'cache_views');
  // We use fields that provide filter handlers as our universe of possible
  // fields of interest.
  $all_fields = self::viewsFetchFields($base, 'filter');

  // Iterate over all the fields that Views knows about.
  $fields = [];
  foreach ((array) $all_fields as $alias => $value) {

    // Set up some default values.
    $granularity = [
      'year',
      'month',
      'day',
      'hour',
      'minute',
      'second',
    ];
    $tz_handling = 'site';
    $related_fields = [];
    $timezone_field = '';
    $offset_field = '';
    $rrule_field = '';
    $delta_field = '';

    // $sql_type = DATE_UNIX;
    $sql_type = DateTimeItemInterface::DATE_STORAGE_FORMAT;
    $type = '';
    $name = $alias;
    $tmp = explode('.', $name);
    $field_name = $tmp[1];
    $table_name = $tmp[0];

    // Unset the date filter to avoid ugly recursion and broken values.
    if ($field_name == 'date_filter') {
      continue;
    }
    $fromto = [
      $name,
      $name,
    ];

    // If we don't have a filter handler, we don't need to do anything more.
    $filterHandler = Views::handlerManager('filter');
    $handler = $filterHandler
      ->getHandler([
      'table' => $table_name,
      'field' => $field_name,
    ]);
    if ($handler instanceof Broken) {
      continue;
    }

    // $handler = views_get_handler($table_name, $field_name, 'filter');
    $pluginDefinition = $handler
      ->getPluginDefinition();

    // We don't care about anything but date handlers.
    if ($pluginDefinition['class'] != 'Drupal\\views\\Plugin\\views\\filter\\Date' && !is_subclass_of($pluginDefinition['class'], 'Drupal\\views\\Plugin\\views\\filter\\Date')) {
      continue;
    }
    $is_field = FALSE;

    // For Field module fields, get the date type.
    $custom = [];
    if ($field_name || isset($handler->definition['field_name'])) {

      // $field = FieldConfig::loadByName($field_name);
      // $field = field_info_field($handler->definition['field_name']);
      $is_field = TRUE;

      // Switch ($field['type']) {.
      switch ($handler
        ->getBaseId()) {
        case 'date':
          $sql_type = DateTimeItemInterface::DATE_STORAGE_FORMAT;

          // $sql_type = DATE_ISO;
          break;
        case 'datestamp':
          break;
        case 'datetime':

          // $sql_type = DATE_DATETIME;
          $sql_type = DateTimeItemInterface::DATE_STORAGE_FORMAT;
          break;
        default:

          // If this is not a date field, continue to the next field.
          continue 2;
      }

      // $revision = in_array($base, array('node_revision')) ? FIELD_LOAD_REVISION : FIELD_LOAD_CURRENT;
      // @todo find database info
      //   $db_info = date_api_database_info($field, $revision);
      $name = $table_name . "." . $field_name;
      $grans = [
        'year',
        'month',
        'day',
        'hour',
        'minute',
        'second',
      ];
      $granularity = !empty($field['granularity']) ? $field['granularity'] : $grans;

      // $fromto = [
      //          $table_name . '.' . $db_info['columns'][$table_name]['value'],
      //          $table_name . '.' . (!empty($field['settings']['todate']) ? $db_info['columns'][$table_name]['value2'] : $db_info['columns'][$table_name]['value']),
      //        ];
      //        if (isset($field['settings']['tz_handling'])) {
      //          $tz_handling = $field['settings']['tz_handling'];
      //          $db_info = date_api_database_info($field, $revision);
      //          if ($tz_handling == 'date') {
      //            $offset_field = $table_name . '.' . $db_info['columns'][$table_name]['offset'];
      //          }
      //          $related_fields = [
      //            $table_name . '.' . $db_info['columns'][$table_name]['value'],
      //          ];
      //          if (isset($db_info['columns'][$table_name]['value2'])) {
      //            $related_fields = array_merge($related_fields, [$table_name . '.' . $db_info['columns'][$table_name]['value2']]);
      //          }
      //          if (isset($db_info['columns'][$table_name]['timezone'])) {
      //            $related_fields = array_merge($related_fields, [$table_name . '.' . $db_info['columns'][$table_name]['timezone']]);
      //            $timezone_field = $table_name . '.' . $db_info['columns'][$table_name]['timezone'];
      //          }
      //          if (isset($db_info['columns'][$table_name]['rrule'])) {
      //            $related_fields = array_merge($related_fields, [$table_name . '.' . $db_info['columns'][$table_name]['rrule']]);
      //            $rrule_field = $table_name . '.' . $db_info['columns'][$table_name]['rrule'];
      //          }
      //        }
      // Get the delta value into the query.
      // if ($field['cardinality'] != 1) {
      //   array_push($related_fields, "$table_name.delta");
      //   $delta_field = $table_name . '_delta';
      // }
    }
    else {

      // Foreach (module_implements('date_views_fields') as $module) {
      //   $function = $module . '_date_views_fields';
      //   if ($custom = $function("$table_name.$field_name")) {
      //     $type = 'custom';
      //     break;
      //    }
      // }.
    }

    // Don't do anything if this is not a date field we can handle.
    if (!empty($type) || empty($custom)) {
      $alias = str_replace('.', '_', $alias);
      $fields['name'][$name] = [
        'is_field' => $is_field,
        'sql_type' => $sql_type,
        // 'label' => $val['group'] . ': ' . $val['title'],
        'granularity' => $granularity,
        'fullname' => $name,
        'table_name' => $table_name,
        'field_name' => $field_name,
        'query_name' => substr($alias, 0, 60),
        'fromto' => $fromto,
        'tz_handling' => $tz_handling,
        'offset_field' => $offset_field,
        'timezone_field' => $timezone_field,
        'rrule_field' => $rrule_field,
        'related_fields' => $related_fields,
        'delta_field' => $delta_field,
      ];

      // Allow the custom fields to over-write values.
      if (!empty($custom)) {
        foreach ($custom as $key => $field_value) {
          $fields['name'][$name][$key] = $field_value;
        }
      }
      $fields['name'][$name]['real_field_name'] = $field_name;
      $fields['alias'][$alias] = $fields['name'][$name];
    }
  }

  // cache_set($cid, $fields, 'cache_views');.
  return $fields;
}