View source
<?php
namespace Drupal\facets\Plugin\facets\facet_source;
use Drupal\Core\Form\FormStateInterface;
use Drupal\facets\Exception\InvalidQueryTypeException;
use Drupal\facets\FacetInterface;
use Drupal\facets\FacetSource\SearchApiFacetSourceInterface;
use Drupal\search_api\Backend\BackendInterface;
use Drupal\facets\FacetSource\FacetSourcePluginBase;
use Drupal\search_api\FacetsQueryTypeMappingInterface;
use Drupal\search_api\Utility\QueryHelper;
use Drupal\facets\QueryType\QueryTypePluginManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class SearchApiBaseFacetSource extends FacetSourcePluginBase implements SearchApiFacetSourceInterface {
protected $index;
protected $searchApiQueryHelper;
public function __construct(array $configuration, $plugin_id, $plugin_definition, QueryTypePluginManager $query_type_plugin_manager, QueryHelper $search_results_cache) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $query_type_plugin_manager);
$this->pluginDefinition = $plugin_definition;
$this->pluginId = $plugin_id;
$this->configuration = $configuration;
$this->searchApiQueryHelper = $search_results_cache;
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('plugin.manager.facets.query_type'), $container
->get('search_api.query_helper'));
}
public function getIndex() {
@trigger_error('Relying on $this->index is deprecated in facets:8.x-1.5. It will be removed from facets:8.x-2.0. Instead, all subclasses should implement ::getIndex() themselves, and the blanket implementation will be removed from SearchApiBaseFacetSource. See https://www.drupal.org/node/3154173', E_USER_DEPRECATED);
return $this->index;
}
public function getDisplay() {
return $this
->getPluginDefinition()['display_id'];
}
public function getViewsDisplay() {
return NULL;
}
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$form['field_identifier'] = [
'#type' => 'select',
'#options' => $this
->getFields(),
'#title' => $this
->t('Field'),
'#description' => $this
->t('The field from the selected facet source which contains the data to build a facet for.<br> The field types supported are <strong>boolean</strong>, <strong>date</strong>, <strong>decimal</strong>, <strong>integer</strong> and <strong>string</strong>.'),
'#required' => TRUE,
'#default_value' => $this->facet
->getFieldIdentifier(),
];
return $form;
}
public function getFields() {
$indexed_fields = [];
$fields = $this
->getIndex()
->getFields();
$server = $this
->getIndex()
->getServerInstance();
$backend = $server
->getBackend();
foreach ($fields as $field) {
$query_types = $this
->getQueryTypesForDataType($backend, $field
->getDataTypePlugin()
->getPluginId());
if (!empty($query_types)) {
$indexed_fields[$field
->getFieldIdentifier()] = $field
->getLabel() . ' (' . $field
->getPropertyPath() . ')';
}
}
return $indexed_fields;
}
public function getQueryTypesForFacet(FacetInterface $facet) {
$field_id = $facet
->getFieldIdentifier();
$server = $this
->getIndex()
->getServerInstance();
$backend = $server
->getBackend();
$fields = $this
->getIndex()
->getFields();
if (isset($fields[$field_id])) {
return $this
->getQueryTypesForDataType($backend, $fields[$field_id]
->getType());
}
throw new InvalidQueryTypeException("No available query types were found for facet {$facet->getName()}");
}
public function getQueryTypesForDataType(BackendInterface $backend, $data_type_plugin_id) {
$query_types = [];
$query_types['string'] = 'search_api_string';
switch ($data_type_plugin_id) {
case 'date':
$query_types['date'] = 'search_api_date';
break;
case 'decimal':
case 'integer':
$query_types['numeric'] = 'search_api_granular';
$query_types['range'] = 'search_api_range';
break;
}
if ($backend instanceof FacetsQueryTypeMappingInterface) {
$query_types = array_merge($query_types, $backend
->getQueryTypesForDataType($data_type_plugin_id));
}
$backend_plugin_id = $backend
->getPluginId();
\Drupal::moduleHandler()
->alter('facets_search_api_query_type_mapping', $backend_plugin_id, $query_types);
return $query_types;
}
}