You are here

private static function CalendarHelper::viewsFetchFields in Calendar 8

Fetch a list of all fields available for a given base type.

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

Parameters

$base:

$type:

bool|false $grouping:

Return value

array

1 call to CalendarHelper::viewsFetchFields()
CalendarHelper::dateViewFields in src/CalendarHelper.php
Helper for identifying Date API fields for views.

File

src/CalendarHelper.php, line 585

Class

CalendarHelper
Defines Gregorian Calendar date values.

Namespace

Drupal\calendar

Code

private static function viewsFetchFields($base, $type, $grouping = FALSE) {
  static $fields = [];
  if (empty($fields)) {
    $data = Views::viewsData()
      ->getAll();
    $start = microtime(TRUE);

    // This constructs this ginormous multi dimensional array to
    // collect the important data about fields. In the end,
    // the structure looks a bit like this (using nid as an example)
    // $strings['nid']['filter']['title'] = 'string'.
    //
    // This is constructed this way because the above referenced strings
    // can appear in different places in the actual data structure so that
    // the data doesn't have to be repeated a lot. This essentially lets
    // each field have a cheap kind of inheritance.
    foreach ($data as $table => $table_data) {
      $bases = [];
      $strings = [];
      $skip_bases = [];
      foreach ($table_data as $field => $info) {

        // Collect table data from this table.
        if ($field == 'table') {

          // Calculate what tables this table can join to.
          if (!empty($info['join'])) {
            $bases = array_keys($info['join']);
          }

          // And it obviously joins to itself.
          $bases[] = $table;
          continue;
        }
        foreach ([
          'field',
          'sort',
          'filter',
          'argument',
          'relationship',
          'area',
        ] as $key) {
          if (!empty($info[$key])) {
            if ($grouping && !empty($info[$key]['no group by'])) {
              continue;
            }
            if (!empty($info[$key]['skip base'])) {
              foreach ((array) $info[$key]['skip base'] as $base_name) {
                $skip_bases[$field][$key][$base_name] = TRUE;
              }
            }
            elseif (!empty($info['skip base'])) {
              foreach ((array) $info['skip base'] as $base_name) {
                $skip_bases[$field][$key][$base_name] = TRUE;
              }
            }

            // Don't show old fields. The real field will be added right.
            if (isset($info[$key]['moved to'])) {
              continue;
            }
            foreach ([
              'title',
              'group',
              'help',
              'base',
              'aliases',
            ] as $string) {

              // First, try the lowest possible level.
              if (!empty($info[$key][$string])) {
                $strings[$field][$key][$string] = $info[$key][$string];
              }
              elseif (!empty($info[$string])) {
                $strings[$field][$key][$string] = $info[$string];
              }
              elseif (!empty($table_data['table'][$string])) {
                $strings[$field][$key][$string] = $table_data['table'][$string];
              }
              else {
                if ($string != 'base') {
                  $strings[$field][$key][$string] = t("Error: missing @component", [
                    '@component' => $string,
                  ]);
                }
              }
            }
          }
        }
      }
      foreach ($bases as $base_name) {
        foreach ($strings as $field => $field_strings) {
          foreach ($field_strings as $type_name => $type_strings) {
            if (empty($skip_bases[$field][$type_name][$base_name])) {
              $fields[$base_name][$type_name]["{$table}.{$field}"] = $type_strings;
            }
          }
        }
      }
    }

    // vsm('Views UI data build time: ' . (views_microtime() - $start) * 1000 . ' ms');.
  }

  // If we have an array of base tables available, go through them
  // all and add them together. Duplicate keys will be lost and that's
  // Just Fine.
  if (is_array($base)) {
    $strings = [];
    foreach ($base as $base_table) {
      if (isset($fields[$base_table][$type])) {
        $strings += $fields[$base_table][$type];
      }
    }
    uasort($strings, '_views_sort_types');
    return $strings;
  }

  // @todo find out if this hack is right
  //   if (isset($fields[$base][$type])) {
  //   uasort($fields[$base][$type], '_views_sort_types');
  //   return $fields[$base][$type];
  //   }
  $all_fields = [];
  foreach ($fields as $key => $field) {
    if ($base == substr($key, 0, strlen($base))) {
      if (isset($fields[$key][$type])) {

        // uasort($fields[$key][$type], '_views_sort_types');.
        $all_fields = array_merge($all_fields, $fields[$key][$type]);
      }
    }
  }
  return $all_fields;

  // Return [];.
}