/** * {@inheritdoc} */ public function getQueryTypesForFacet(FacetInterface $facet) { // Get our Facets Field Identifier, which is equal to the Search API Field // identifier. $field_id = $facet->getFieldIdentifier(); // Get the Search API Server. $server = $this->index->getServerInstance(); // Get the Search API Backend. $backend = $server->getBackend(); $fields = $this->index->getFields(); foreach ($fields as $field) { if ($field->getFieldIdentifier() == $field_id) { return $this->getQueryTypesForDataType($backend, $field->getType()); } } throw new InvalidQueryTypeException($this->t("No available query types were found for facet @facet", ['@facet' => $facet->getName()])); }
/** * {@inheritdoc} */ public function build(FacetInterface $facet, array $results) { $field_identifier = $facet->getFieldIdentifier(); $entity = 'node'; // Support multiple entities when using Search API. if ($facet->getFacetSource() instanceof SearchApiFacetSourceInterface) { $index = $facet->getFacetSource()->getIndex(); $field = $index->getField($field_identifier); $entity = str_replace('entity:', '', $field->getDatasourceId()); } // If it's an entity base field, we find it in the field definitions. // We don't have access to the bundle via SearchApiFacetSourceInterface, so // we check the entity's base fields only. $base_fields = $this->entityFieldManager->getFieldDefinitions($entity, ''); // This only works for configurable fields. $config_entity_name = sprintf('field.storage.%s.%s', $entity, $field_identifier); if (isset($base_fields[$field_identifier])) { $field = $base_fields[$field_identifier]; } elseif ($this->configManager->loadConfigEntityByName($config_entity_name) !== NULL) { $field = $this->configManager->loadConfigEntityByName($config_entity_name); } if ($field) { $function = $field->getSetting('allowed_values_function'); if (empty($function)) { $allowed_values = $field->getSetting('allowed_values'); } else { $allowed_values = ${$function}($field); } if (is_array($allowed_values)) { /** @var \Drupal\facets\Result\ResultInterface $result */ foreach ($results as &$result) { if (isset($allowed_values[$result->getRawValue()])) { $result->setDisplayValue($allowed_values[$result->getRawValue()]); } } } } return $results; }
/** * {@inheritdoc} */ public function getQueryInfo(FacetInterface $facet) { $query_info = []; $field_name = $facet->getFieldIdentifier(); $default_fields = $this->getDefaultFields(); if (array_key_exists($facet->getFieldIdentifier(), $default_fields)) { // We add the language code of the indexed item to the result of the // query. So in this case we need to use the search_index table alias (i) // for the langcode field. Otherwise we will have same nid for multiple // languages as result. For more details see NodeSearch::findResults(). // @TODO review if I can refactor this. $table_alias = $facet->getFieldIdentifier() == 'langcode' ? 'i' : 'n'; $query_info = ['fields' => [$table_alias . '.' . $facet->getFieldIdentifier() => ['table_alias' => $table_alias, 'field' => $facet->getFieldIdentifier()]]]; } else { // Gets field info, finds table name and field name. $table = "node__{$field_name}"; // The column name will be different depending on the field type, it's // always the fields machine name, suffixed with '_value'. Entity // reference fields change that suffix into '_target_id'. $field_config = FieldStorageConfig::loadByName('node', $facet->getFieldIdentifier()); $field_type = $field_config->getType(); if ($field_type == 'entity_reference') { $column = $facet->getFieldIdentifier() . '_target_id'; } else { $column = $facet->getFieldIdentifier() . '_value'; } $query_info['fields'][$field_name . '.' . $column] = array('table_alias' => $table, 'field' => $column); // Adds the join on the node table. $query_info['joins'] = array($table => array('table' => $table, 'alias' => $table, 'condition' => "n.vid = {$table}.revision_id AND i.langcode = {$table}.langcode")); } // Returns query info, makes sure all keys are present. return $query_info + ['joins' => [], 'fields' => []]; }