You are here

public function SearchApiSolrBackend::getBackendDefinedFields in Search API Solr 8

Same name and namespace in other branches
  1. 8.3 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getBackendDefinedFields()
  2. 8.2 src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getBackendDefinedFields()
  3. 4.x src/Plugin/search_api/backend/SearchApiSolrBackend.php \Drupal\search_api_solr\Plugin\search_api\backend\SearchApiSolrBackend::getBackendDefinedFields()

Provides information on additional fields made available by the backend.

If a backend indexes additional data with items and wants to make this available as fixed fields on the index (for example, to be used with Views), it can implement this method to facilitate this.

Fields returned here are expected to work correctly with this server when used in query conditions, sorts or similar places.

Parameters

\Drupal\search_api\IndexInterface $index: The index for which fields are being determined.

Return value

\Drupal\search_api\Item\FieldInterface[] An array of additional fields that are available for this index, keyed by their field IDs. The field IDs should always start with "search_api_" (avoiding the special field IDs defined by \Drupal\search_api\Query\QueryInterface::sort()) to avoid conflicts with user-defined fields.

Overrides BackendPluginBase::getBackendDefinedFields

File

src/Plugin/search_api/backend/SearchApiSolrBackend.php, line 2834

Class

SearchApiSolrBackend
Apache Solr backend for search api.

Namespace

Drupal\search_api_solr\Plugin\search_api\backend

Code

public function getBackendDefinedFields(IndexInterface $index) {
  $location_distance_fields = [];
  foreach ($index
    ->getFields() as $field) {
    if ($field
      ->getType() == 'location') {
      $distance_field_name = $field
        ->getFieldIdentifier() . '__distance';
      $property_path_name = $field
        ->getPropertyPath() . '__distance';
      $distance_field = new Field($index, $distance_field_name);
      $distance_field
        ->setLabel($field
        ->getLabel() . ' (distance)');
      $distance_field
        ->setDataDefinition(DataDefinition::create('decimal'));
      $distance_field
        ->setType('decimal');
      $distance_field
        ->setDatasourceId($field
        ->getDatasourceId());
      $distance_field
        ->setPropertyPath($property_path_name);
      $location_distance_fields[$distance_field_name] = $distance_field;
    }
  }
  return $location_distance_fields;
}