You are here

function view_custom_table_views_data in Views Custom Table 8

Same name and namespace in other branches
  1. 7 view_custom_table.module \view_custom_table_views_data()
  2. 9.0.x view_custom_table.module \view_custom_table_views_data()

Implements hook_views_data().

File

./view_custom_table.module, line 29
view_custom_table.module

Code

function view_custom_table_views_data() {
  $all_custom_tables = \Drupal::config('view_custom_table.tables')
    ->getRawData();

  // My Sql Data types.
  $int_types = [
    'tinyint',
    'smallint',
    'mediumint',
    'int',
    'bigint',
    'decimal',
    'float',
    'double',
    'bit',
  ];
  $text_types = [
    'varchar',
    'char',
    'tinytext',
    'text',
    'mediumtext',
    'longtext',
    'binary',
    'varbinary',
    'tinyblob',
    'mediumblob',
    'blob',
    'longblob',
    'enum',
    'set',
  ];
  $time_type = [
    'timestamp',
    'date',
    'datetime',
    'time',
    'year',
  ];
  if (!empty($all_custom_tables)) {
    foreach ($all_custom_tables as $table_name => $custom_table) {
      $table_relations = unserialize($custom_table['column_relations']);
      $table_display_name = ucfirst($table_name);
      $table_display_name = str_replace('_', ' ', $table_display_name);
      $data[$table_name]['table']['group'] = t('Custom Table Views');
      $table = view_custom_table_load_table_structure($table_name, $custom_table['table_database']);
      foreach ($table as $column) {
        $column_display_name = ucfirst($column->Field);
        $column_display_name = str_replace('_', ' ', $column_display_name);
        if (strpos($column->Type, '(')) {
          $column_type_values = explode('(', $column->Type);
          $column_type = $column_type_values[0];
        }
        else {
          $column_type = $column->Type;
        }
        if ($column->Key == 'PRI') {
          $data[$table_name]['table']['base'] = [
            'field' => $column->Field,
            'title' => t('@table data', [
              '@table' => $table_display_name,
            ]),
            'help' => t('Data of @table.', [
              '@table' => $table_display_name,
            ]),
            'database' => $custom_table['table_database'],
            'weight' => 10,
          ];
        }
        if (in_array($column_type, $int_types)) {
          $data[$table_name][$column->Field] = [
            'title' => t('@column_name', [
              '@column_name' => $column_display_name,
            ]),
            'help' => t('@column_name data form @table.', [
              '@column_name' => $column_display_name,
              '@table' => $table_display_name,
            ]),
            'field' => [
              'id' => 'numeric',
            ],
            'sort' => [
              'id' => 'standard',
            ],
            'filter' => [
              'id' => 'numeric',
            ],
            'argument' => [
              'id' => 'numeric',
            ],
          ];
        }
        if (in_array($column_type, $text_types)) {
          $data[$table_name][$column->Field] = [
            'title' => t('@column_name', [
              '@column_name' => $column_display_name,
            ]),
            'help' => t('@column_name data form @table.', [
              '@column_name' => $column_display_name,
              '@table' => $table_display_name,
            ]),
            'field' => [
              'id' => 'standard',
            ],
            'sort' => [
              'id' => 'standard',
            ],
            'filter' => [
              'id' => 'string',
            ],
            'argument' => [
              'id' => 'string',
            ],
          ];
        }
        if (in_array($column_type, $time_type)) {
          $data[$table_name][$column->Field] = [
            'title' => t('@column_name', [
              '@column_name' => $column_display_name,
            ]),
            'help' => t('@column_name data form @table.', [
              '@column_name' => $column_display_name,
              '@table' => $table_display_name,
            ]),
            'field' => [
              'id' => 'mysql_date',
            ],
            'filter' => [
              'id' => 'mysql_date',
            ],
          ];
        }
        if (!empty($table_relations)) {
          if (array_key_exists($column->Field, $table_relations)) {
            $all_entities = \Drupal::entityTypeManager()
              ->getDefinitions();
            if (isset($all_entities[$table_relations[$column->Field]])) {
              $entity_info = \Drupal::entityTypeManager()
                ->getDefinition($table_relations[$column->Field]);
              $entity_keys = $entity_info
                ->getKeys();
              $data[$table_name][$column->Field]['relationship'] = [
                'base' => $entity_info
                  ->getDataTable(),
                'id' => 'standard',
                'base field' => $entity_keys['id'],
                'label' => t('@column_name', [
                  '@column_name' => $column_display_name,
                ]),
                'title' => t('@column_name', [
                  '@column_name' => $column_display_name,
                ]),
                'help' => t('@entity - @column_name relationship', [
                  '@column_name' => $column_display_name,
                  '@entity' => $entity_info
                    ->getBaseTable(),
                ]),
              ];
            }
            else {
              $related_table_info = $all_custom_tables[$table_relations[$column->Field]];
              $related_table = view_custom_table_load_table_structure($related_table_info['table_name'], $related_table_info['table_database']);
              foreach ($related_table as $related_table_column) {
                if ($related_table_column->Key == 'PRI') {
                  $related_table_pri_key = $related_table_column->Field;
                }
              }
              $data[$table_name][$column->Field]['relationship'] = [
                'base' => $table_relations[$column->Field],
                'database' => $related_table_info['table_database'],
                'id' => 'standard',
                'base field' => $related_table_pri_key,
                'label' => t('@column_name', [
                  '@column_name' => $column_display_name,
                ]),
                'title' => t('@column_name', [
                  '@column_name' => $column_display_name,
                ]),
                'help' => t('@entity - @column_name relationship', [
                  '@column_name' => $column_display_name,
                  '@entity' => $table_relations[$column->Field],
                ]),
              ];
            }
          }
        }
      }
    }
  }
  return isset($data) ? $data : [];
}