You are here

function apachesolr_views_views_data_alter in Apache Solr Views 6

Implementation of hook_views_data_alter().

File

./apachesolr_views.views.inc, line 352

Code

function apachesolr_views_views_data_alter(&$data) {

  // Currently only looking at base table of node.
  $base_tables = array_keys(module_invoke_all('apachesolr_entities'));
  $new_tables = array();
  foreach ($data as $table => $table_definition) {

    // Determine if this is one of our base tables.
    $is_base_table = in_array($table, $base_tables);
    $intersect = array();

    // See if this normal SQL table can join to one of our base tables.
    if (isset($table_definition['table']) && isset($table_definition['table']['join']) && is_array($table_definition['table']['join'])) {
      $intersect = array_intersect($base_tables, array_keys($table_definition['table']['join']));
    }
    if ($is_base_table) {
      $intersect[] = $table;
    }
    if (!empty($intersect)) {
      foreach ($intersect as $base_table) {
        isset($table_definition['table']['group']) ? $default_group = $table_definition['table']['group'] : ($default_group = '');
        foreach ($table_definition as $database_field => $definition) {
          if ($database_field != 'table' && isset($definition['field'])) {
            if (!isset($definition['group'])) {
              $definition['group'] = $default_group;
            }

            // Only allow title, help, group and field keys. Filters and sorts are
            // not doable.
            foreach (array(
              'title',
              'help',
              'field',
            ) as $allowed_key) {
              if (isset($definition[$allowed_key])) {
                $new_tables['apachesolr_' . $base_table . '_' . $table][$database_field][$allowed_key] = $definition[$allowed_key];
              }
            }

            // Make separate groups for the Apache Solr fields
            if (isset($definition['group'])) {
              $new_tables['apachesolr_' . $base_table . '_' . $table][$database_field]['group'] = 'Apache Solr ' . $definition['group'];
            }

            // Add to the new_tables. This will be merged into data at the end.
            // This prevents and infinite loop.
            $new_tables['apachesolr_' . $base_table . '_' . $table]['table']['join'] = array(
              'apachesolr_' . $base_table => array(
                'left_table' => $table,
              ),
            );

            // If it isn't the base table provide the know how to join it to the
            // SQL query.
            if ($base_table != $table) {
              $new_tables['apachesolr_' . $base_table . '_' . $table]['table']['join'][$base_table] = array_merge(array(
                'table' => $table,
              ), $data[$table]['table']['join'][$base_table]);
            }
          }
        }
      }
    }
  }
  $data += $new_tables;
}