You are here

function _search_api_views_data_special_fields in Search API 8

Adds definitions for our special fields to a Views data table definition.

Parameters

array $table: The existing Views data table definition.

\Drupal\search_api\IndexInterface $index: The index for which the Views data table is created.

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

File

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

Code

function _search_api_views_data_special_fields(array &$table, IndexInterface $index) {
  $id_field = _search_api_views_find_field_alias('search_api_id', $table);
  $table[$id_field]['title'] = t('Item ID');
  $table[$id_field]['help'] = t("The item's internal (Search API-specific) ID");
  $table[$id_field]['field']['id'] = 'standard';
  $table[$id_field]['sort']['id'] = 'search_api';
  if ($id_field != 'search_api_id') {
    $table[$id_field]['real field'] = 'search_api_id';
  }
  $datasource_field = _search_api_views_find_field_alias('search_api_datasource', $table);
  $table[$datasource_field]['title'] = t('Datasource');
  $table[$datasource_field]['help'] = t('The datasource ID');
  $table[$datasource_field]['argument']['id'] = 'search_api';
  $table[$datasource_field]['argument']['disable_break_phrase'] = TRUE;
  $table[$datasource_field]['field']['id'] = 'standard';
  $table[$datasource_field]['filter']['id'] = 'search_api_datasource';
  $table[$datasource_field]['sort']['id'] = 'search_api';
  if ($datasource_field != 'search_api_datasource') {
    $table[$datasource_field]['real field'] = 'search_api_datasource';
  }
  $language_field = _search_api_views_find_field_alias('search_api_language', $table);
  $table[$language_field]['title'] = t('Item language');
  $table[$language_field]['help'] = t("The item's language");
  $table[$language_field]['field']['id'] = 'language';
  $table[$language_field]['filter']['id'] = 'search_api_language';
  $table[$language_field]['filter']['allow empty'] = FALSE;
  $table[$language_field]['sort']['id'] = 'search_api';
  if ($language_field != 'search_api_language') {
    $table[$language_field]['real field'] = 'search_api_language';
  }
  $relevance_field = _search_api_views_find_field_alias('search_api_relevance', $table);
  $table[$relevance_field]['group'] = t('Search');
  $table[$relevance_field]['title'] = t('Relevance');
  $table[$relevance_field]['help'] = t('The relevance of this search result with respect to the query');
  $table[$relevance_field]['field']['type'] = 'decimal';
  $table[$relevance_field]['field']['id'] = 'numeric';
  $table[$relevance_field]['field']['search_api field'] = 'search_api_relevance';
  $table[$relevance_field]['sort']['id'] = 'search_api';
  if ($relevance_field != 'search_api_relevance') {
    $table[$relevance_field]['real field'] = 'search_api_relevance';
  }
  $excerpt_field = _search_api_views_find_field_alias('search_api_excerpt', $table);
  $table[$excerpt_field]['group'] = t('Search');
  $table[$excerpt_field]['title'] = t('Excerpt');
  $table[$excerpt_field]['help'] = t('The search result excerpted to show found search terms');
  $table[$excerpt_field]['field']['id'] = 'search_api';
  $table[$excerpt_field]['field']['filter_type'] = 'xss';
  if ($excerpt_field != 'search_api_excerpt') {
    $table[$excerpt_field]['real field'] = 'search_api_excerpt';
  }
  $fulltext_field = _search_api_views_find_field_alias('search_api_fulltext', $table);
  $table[$fulltext_field]['group'] = t('Search');
  $table[$fulltext_field]['title'] = t('Fulltext search');
  $table[$fulltext_field]['help'] = t('Search several or all fulltext fields at once.');
  $table[$fulltext_field]['filter']['id'] = 'search_api_fulltext';
  $table[$fulltext_field]['argument']['id'] = 'search_api_fulltext';
  if ($fulltext_field != 'search_api_fulltext') {
    $table[$fulltext_field]['real field'] = 'search_api_fulltext';
  }
  $mlt_field = _search_api_views_find_field_alias('search_api_more_like_this', $table);
  $table[$mlt_field]['group'] = t('Search');
  $table[$mlt_field]['title'] = t('More like this');
  $table[$mlt_field]['help'] = t('Find similar content.');
  $table[$mlt_field]['argument']['id'] = 'search_api_more_like_this';
  if ($mlt_field != 'search_api_more_like_this') {
    $table[$mlt_field]['real field'] = 'search_api_more_like_this';
  }
  $rendered_field = _search_api_views_find_field_alias('search_api_rendered_item', $table);
  $table[$rendered_field]['group'] = t('Search');
  $table[$rendered_field]['title'] = t('Rendered item');
  $table[$rendered_field]['help'] = t('Renders item in a view mode.');
  $table[$rendered_field]['field']['id'] = 'search_api_rendered_item';
  if ($rendered_field != 'search_api_rendered_item') {
    $table[$rendered_field]['real field'] = 'search_api_rendered_item';
  }

  // If at least one datasource is based on an entity type that offers
  // operations, we provide them as a field.
  foreach ($index
    ->getDatasources() as $datasource) {
    if ($entity_type_id = $datasource
      ->getEntityTypeId()) {
      $entity_type_id = \Drupal::entityTypeManager()
        ->getDefinition($entity_type_id);
      if ($entity_type_id
        ->hasListBuilderClass()) {
        $operations_field = _search_api_views_find_field_alias('search_api_operations', $table);
        $table[$operations_field]['title'] = t('Operations links');
        $table[$operations_field]['help'] = t('Provides links to perform entity operations.');
        $table[$operations_field]['field']['id'] = 'search_api_entity_operations';
        if ($operations_field !== 'search_api_operations') {
          $table[$operations_field]['real field'] = 'search_api_operations';
        }
        break;
      }
    }
  }

  // If there are taxonomy term references indexed in the index, include the
  // "All taxonomy term fields" contextual filter. We also save for all fields
  // whether they contain only terms of a certain vocabulary, keying that
  // information by vocabulary for later ease of use.
  $vocabulary_fields = [];

  /** @var \Drupal\search_api\Item\Field $field */
  foreach ($index
    ->getFields() as $field_id => $field) {

    // Search for taxonomy term reference fields, and catch problems early on.
    try {
      $property = $field
        ->getDataDefinition();
      $datasource = $field
        ->getDatasource();
    } catch (SearchApiException $e) {

      // Will probably cause other problems elsewhere, but here we can just
      // ignore it.
      continue;
    }
    if (!$datasource) {
      continue;
    }
    if ($property
      ->getDataType() !== 'field_item:entity_reference') {
      continue;
    }
    $settings = $property
      ->getSettings();
    if (($settings['target_type'] ?? '') !== 'taxonomy_term') {
      continue;
    }
    $entity_type_id = $datasource
      ->getEntityTypeId();

    // Field instances per bundle can reference different vocabularies, make
    // sure we add them all.
    $bundles = \Drupal::getContainer()
      ->get('entity_type.bundle.info')
      ->getBundleInfo($entity_type_id);
    foreach ($bundles as $bundle_id => $bundle) {
      $bundle_fields = \Drupal::getContainer()
        ->get('entity_field.manager')
        ->getFieldDefinitions($entity_type_id, $bundle_id);

      // Check if this bundle has the taxonomy entity reference field.
      if (array_key_exists($field
        ->getFieldIdentifier(), $bundle_fields)) {

        /** @var \Drupal\Core\Field\FieldDefinitionInterface $field_definition */
        $field_definition = $bundle_fields[$field
          ->getFieldIdentifier()];
        $bundle_settings = $field_definition
          ->getSettings();
        if (!empty($bundle_settings['handler_settings']['target_bundles'])) {
          foreach ($bundle_settings['handler_settings']['target_bundles'] as $vocabulary_id) {
            $vocabulary_fields[$vocabulary_id][] = $field_id;
          }
        }
        else {

          // If we can't determine the referenced vocabularies, we use the
          // special "" key to mean "any vocabulary".
          $vocabulary_fields[''][] = $field_id;
        }
      }
    }
  }
  if ($vocabulary_fields) {

    // Make sure $vocabulary_fields doesn't contain duplicates for fields that
    // are shared between bundles.
    $vocabulary_fields = array_map('array_unique', $vocabulary_fields);
    $all_terms_field = _search_api_views_find_field_alias('search_api_all_terms', $table);
    $table[$all_terms_field]['group'] = t('Search');
    $table[$all_terms_field]['title'] = t('All taxonomy term fields');
    $table[$all_terms_field]['help'] = t('Search all indexed taxonomy term fields');
    $table[$all_terms_field]['argument']['id'] = 'search_api_all_terms';
    $table[$all_terms_field]['argument']['vocabulary_fields'] = $vocabulary_fields;
    if ($all_terms_field != 'search_api_all_terms') {
      $table[$all_terms_field]['real field'] = 'search_api_all_terms';
    }
  }
  $bulk_form_field = _search_api_views_find_field_alias('search_api_bulk_form', $table);
  $table[$bulk_form_field] = [
    'title' => t('Bulk update'),
    'help' => t('Allows users to apply an action to one or more items.'),
    'field' => [
      'id' => 'search_api_bulk_form',
    ],
  ];
}