You are here

function _search_api_views_datasource_table in Search API 8

Creates a Views table definition for one datasource of an index.

Parameters

\Drupal\search_api\Datasource\DatasourceInterface $datasource: The datasource for which to create a table definition.

array $data: The existing Views data definitions. Passed by reference so additionally needed tables can be inserted.

Return value

array A Views table definition for the given datasource.

1 call to _search_api_views_datasource_table()
search_api_views_data in ./search_api.views.inc
Implements hook_views_data().

File

./search_api.views.inc, line 679
Views hook implementations for the Search API module.

Code

function _search_api_views_datasource_table(DatasourceInterface $datasource, array &$data) {
  $datasource_id = $datasource
    ->getPluginId();
  $table = [
    'table' => [
      'group' => t('@datasource datasource', [
        '@datasource' => $datasource
          ->label(),
      ]),
      'index' => $datasource
        ->getIndex()
        ->id(),
      'datasource' => $datasource_id,
    ],
  ];
  $entity_type_id = $datasource
    ->getEntityTypeId();
  if ($entity_type_id) {
    $table['table']['entity type'] = $entity_type_id;
    $table['table']['entity revision'] = FALSE;
  }
  _search_api_views_add_handlers_for_properties($datasource
    ->getPropertyDefinitions(), $table, $data);

  // Prefix the "real field" of each entry with the datasource ID.
  foreach ($table as $key => $definition) {
    if ($key == 'table') {
      continue;
    }
    $real_field = $definition['real field'] ?? $key;
    $table[$key]['real field'] = Utility::createCombinedId($datasource_id, $real_field);

    // Relationships sometimes have different real fields set, since they might
    // also include the nested property that contains the actual reference. So,
    // if a "real field" is set for that, we need to adapt it as well.
    if (isset($definition['relationship']['real field'])) {
      $real_field = $definition['relationship']['real field'];
      $table[$key]['relationship']['real field'] = Utility::createCombinedId($datasource_id, $real_field);
    }
  }
  return $table;
}