You are here

function search_api_views_data in Search API 8

Implements hook_views_data().

For each search index, we provide the following tables:

  • One base table, with key "search_api_index_INDEX", which contains field, filter, argument and sort handlers for all indexed fields. (Field handlers, too, to allow things like click-sorting.)
  • Tables for each datasource, by default with key "search_api_datasource_INDEX_DATASOURCE", with field and (where applicable) relationship handlers for each property of the datasource. Those will be joined to the index base table by default.

Also, for each entity type encountered in any table, a table with field/relationship handlers for all of that entity type's properties is created. Those tables will use the key "search_api_entity_ENTITY".

File

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

Code

function search_api_views_data() {
  $data = [];

  /** @var \Drupal\search_api\IndexInterface $index */
  foreach (Index::loadMultiple() as $index) {
    try {

      // Fill in base data.
      $key = 'search_api_index_' . $index
        ->id();
      $table =& $data[$key];
      $index_label = $index
        ->label();
      $table['table']['group'] = t('Index @name', [
        '@name' => $index_label,
      ]);
      $table['table']['base'] = [
        'field' => 'search_api_id',
        'index' => $index
          ->id(),
        'title' => t('Index @name', [
          '@name' => $index_label,
        ]),
        'help' => t('Use the @name search index for filtering and retrieving data.', [
          '@name' => $index_label,
        ]),
        'query_id' => 'search_api_query',
      ];

      // Add suitable handlers for all indexed fields.
      foreach ($index
        ->getFields(TRUE) as $field_id => $field) {
        $field_alias = _search_api_views_find_field_alias($field_id, $table);
        $field_definition = _search_api_views_get_handlers($field);

        // The field handler has to be extra, since it is a) determined by the
        // field's underlying property and b) needs a different "real field"
        // set.
        if ($field
          ->getPropertyPath()) {
          $field_handler = _search_api_views_get_field_handler_for_property($field
            ->getDataDefinition(), $field
            ->getPropertyPath());
          if ($field_handler) {
            $field_definition['field'] = $field_handler;
            $field_definition['field']['real field'] = $field
              ->getCombinedPropertyPath();
            $field_definition['field']['search_api field'] = $field_id;
          }
        }
        if ($field_definition) {
          $field_label = $field
            ->getLabel();
          $field_definition += [
            'title' => $field_label,
            'help' => $field
              ->getDescription() ?: t('(No description available)'),
          ];
          if ($datasource = $field
            ->getDatasource()) {
            $field_definition['group'] = t('@datasource datasource', [
              '@datasource' => $datasource
                ->label(),
            ]);
          }
          else {

            // Backend defined fields that don't have a datasource should be
            // treated like special fields.
            $field_definition['group'] = t('Search');
          }
          if ($field_id != $field_alias) {
            $field_definition['real field'] = $field_id;
          }
          if (isset($field_definition['field'])) {
            $field_definition['field']['title'] = t('@field (indexed field)', [
              '@field' => $field_label,
            ]);
          }
          $table[$field_alias] = $field_definition;
        }
      }

      // Add special fields.
      _search_api_views_data_special_fields($table, $index);

      // Add relationships for field data of all datasources.
      $datasource_tables_prefix = 'search_api_datasource_' . $index
        ->id() . '_';
      foreach ($index
        ->getDatasources() as $datasource_id => $datasource) {
        $table_key = _search_api_views_find_field_alias($datasource_tables_prefix . $datasource_id, $data);
        $data[$table_key] = _search_api_views_datasource_table($datasource, $data);

        // Automatically join this table for views of this index.
        $data[$table_key]['table']['join'][$key] = [
          'join_id' => 'search_api',
        ];
      }
    } catch (\Exception $e) {
      $args = [
        '%index' => $index
          ->label(),
      ];
      watchdog_exception('search_api', $e, '%type while computing Views data for index %index: @message in %function (line %line of %file).', $args);
    }
  }
  return array_filter($data);
}