You are here

function views_database_connector_views_data in Views Database Connector 8

Same name and namespace in other branches
  1. 7 views_database_connector.views.inc \views_database_connector_views_data()

Implements hook_views_data().

File

./views_database_connector.views.inc, line 14
Responsible for hooking views to add each database and its tables.

Code

function views_database_connector_views_data() {
  $dbs = views_database_connector_get_database_schemas();
  $data = [];
  $existing_tables = find_existing_views_data_tables();

  // Iterate through each database.
  foreach ($dbs as $dname => $db) {

    // Iterate through each table.
    foreach ($db as $table) {

      // Skip any already processed by their own modules.
      if (isset($existing_tables[$table[0]])) {
        continue;
      }

      // Make sure the table name hasn't been used before.
      if (!isset($data[$table[0]])) {

        // Set the title.
        $title = '[VDC] ' . Html::escape($dname) . ':  ';
        $title .= Html::escape($table[0]);

        // Setup the table for Views to be able to see it.
        $data[$table[0]]['table']['group'] = $table[0];
        $data[$table[0]]['table']['base'] = [
          // Use the first column's name as the primary field.
          'field' => $table[1][0][1],
          'title' => $title,
          'database' => $dname,
          'weight' => -9001,
        ];

        // Add each column to its respective table.
        foreach ($table[1] as $col) {
          if ($col[0] == 'numeric') {
            $data[$table[0]][$col[1]] = [
              'title' => $col[1],
              'help' => $col[1],
              'field' => [
                'id' => 'numeric',
              ],
              'sort' => [
                'id' => 'standard',
              ],
              'filter' => [
                'id' => 'numeric',
              ],
              'argument' => [
                'id' => 'numeric',
              ],
            ];
          }
          if ($col[0] == 'date') {
            $data[$table[0]][$col[1]] = [
              'title' => $col[1],
              'help' => $col[1],
              'field' => [
                'id' => 'date',
              ],
              'sort' => [
                'id' => 'date',
              ],
              'filter' => [
                'id' => 'date',
              ],
            ];
          }
          if ($col[0] == 'string') {
            $data[$table[0]][$col[1]] = [
              'title' => $col[1],
              'help' => $col[1],
              'field' => [
                'id' => 'standard',
              ],
              'sort' => [
                'id' => 'standard',
              ],
              'filter' => [
                'id' => 'string',
              ],
              'argument' => [
                'id' => 'string',
              ],
            ];
          }
          if ($col[0] == 'broken') {
            $data[$table[0]][$col[1]] = [
              'title' => $col[1],
              'help' => $col[1],
              'field' => [
                'id' => 'broken',
              ],
              'sort' => [
                'id' => 'broken',
              ],
              'filter' => [
                'id' => 'broken',
              ],
              'argument' => [
                'id' => 'broken',
              ],
            ];
          }
        }
      }
    }
  }

  // Return the finished result, allowing Views to be able to see everything
  // that it needs.
  return $data;
}