diff --git a/config/schema/search_api.index.schema.yml b/config/schema/search_api.index.schema.yml index 6f7eb60..caf0fd2 100644 --- a/config/schema/search_api.index.schema.yml +++ b/config/schema/search_api.index.schema.yml @@ -35,23 +35,23 @@ search_api.index.*: label: 'The processor''s weight for this stage' settings: type: plugin.plugin_configuration.search_api_processor.[%parent.processor_id] + fields: + type: sequence + label: 'Indexed fields' + sequence: + type: mapping + label: field + mapping: + type: + type: string + label: 'Type of field' + boost: + type: float + label: 'Boost' options: type: mapping label: 'Options' mapping: - fields: - type: sequence - label: 'Indexed fields' - sequence: - type: mapping - label: field - mapping: - type: - type: string - label: 'Type of field' - boost: - type: float - label: 'Boost' additional fields: type: sequence label: 'Included additional fields' diff --git a/search_api.api.php b/search_api.api.php index 0fa2331..0c3fde8 100644 --- a/search_api.api.php +++ b/search_api.api.php @@ -152,7 +152,7 @@ function hook_search_api_query_alter(\Drupal\search_api\Query\QueryInterface &$q return; } // Otherwise, exclude the node with ID 10 from the search results. - $fields = $query->getIndex()->getFields(); + $fields = $query->getIndex()->getCachedFields(); foreach ($query->getIndex()->getDatasources() as $datasource_id => $datasource) { if ($datasource->getEntityTypeId() == 'node') { $field = \Drupal\search_api\Utility::createCombinedId($datasource_id, 'nid'); @@ -173,7 +173,7 @@ function hook_search_api_query_alter(\Drupal\search_api\Query\QueryInterface &$q */ function hook_search_api_query_TAG_alter(\Drupal\search_api\Query\QueryInterface &$query) { // Exclude the node with ID 10 from the search results. - $fields = $query->getIndex()->getFields(); + $fields = $query->getIndex()->getCachedFields(); foreach ($query->getIndex()->getDatasources() as $datasource_id => $datasource) { if ($datasource->getEntityTypeId() == 'node') { $field = \Drupal\search_api\Utility::createCombinedId($datasource_id, 'nid'); diff --git a/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml b/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml index 691992a..8c949e3 100644 --- a/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml +++ b/search_api_db/search_api_db_defaults/config/optional/search_api.index.default_index.yml @@ -131,35 +131,35 @@ processors: fields: - rendered_item - 'entity:node/title' +fields: + search_api_language: + type: string + 'entity:node/title': + type: text + boost: 8 + rendered_item: + type: text + 'entity:node/created': + type: date + 'entity:node/changed': + type: date + 'entity:node/status': + type: boolean + 'entity:node/sticky': + type: boolean + 'entity:node/field_tags:entity:name': + type: string + 'entity:node/uid:entity:name': + type: string + 'entity:node/uid': + type: integer + search_api_node_grants: + type: string + 'entity:node/type': + type: string options: index_directly: true cron_limit: 50 - fields: - search_api_language: - type: string - 'entity:node/title': - type: text - boost: 8 - rendered_item: - type: text - 'entity:node/created': - type: date - 'entity:node/changed': - type: date - 'entity:node/status': - type: boolean - 'entity:node/sticky': - type: boolean - 'entity:node/field_tags:entity:name': - type: string - 'entity:node/uid:entity:name': - type: string - 'entity:node/uid': - type: integer - search_api_node_grants: - type: string - 'entity:node/type': - type: string 'additional fields': 'entity:node/field_tags': 'entity:node/field_tags' 'entity:node/field_tags:entity': 'entity:node/field_tags:entity' diff --git a/search_api_db/src/Plugin/search_api/backend/Database.php b/search_api_db/src/Plugin/search_api/backend/Database.php index 2336a9e..39dc263 100644 --- a/search_api_db/src/Plugin/search_api/backend/Database.php +++ b/search_api_db/src/Plugin/search_api/backend/Database.php @@ -515,7 +515,7 @@ class Database extends BackendPluginBase { $this->getKeyValueStore()->set($index->id(), $db_info); // If there are no fields, we are done now. - if (!$index->getFields()) { + if (!$index->getCachedFields()) { return; } } @@ -773,7 +773,7 @@ class Database extends BackendPluginBase { try { $db_info = $this->getIndexDbInfo($index); $fields = &$db_info['field_tables']; - $new_fields = $index->getFields(); + $new_fields = $index->getCachedFields(); $reindex = FALSE; $cleared = FALSE; diff --git a/search_api_db/src/Tests/BackendTest.php b/search_api_db/src/Tests/BackendTest.php index 725315a..a8409ab 100644 --- a/search_api_db/src/Tests/BackendTest.php +++ b/search_api_db/src/Tests/BackendTest.php @@ -159,13 +159,13 @@ class BackendTest extends EntityUnitTestBase { // Remove a field from the index and check if the change is matched in the // server configuration. $field_id = $this->getFieldId('keywords'); - if (empty($index->getFields()[$field_id])) { + if (empty($index->getCachedFields()[$field_id])) { throw new \Exception(); } - $index->getFields()[$field_id]->setIndexed(FALSE, TRUE); + $index->getCachedFields()[$field_id]->setIndexed(FALSE, TRUE); $index->save(); - $index_fields = array_keys($index->getOption('fields', array())); + $index_fields = array_keys($index->getFields()); $db_info = \Drupal::keyValue(BackendDatabase::INDEXES_KEY_VALUE_STORE_ID)->get($this->indexId); $server_fields = array_keys($db_info['field_tables']); @@ -175,7 +175,7 @@ class BackendTest extends EntityUnitTestBase { $this->assertEqual($index_fields, $server_fields); // Add the field back for the next assertions. - $index->getFields(FALSE)[$field_id]->setIndexed(TRUE, TRUE); + $index->getCachedFields(FALSE)[$field_id]->setIndexed(TRUE, TRUE); $index->save(); } @@ -186,7 +186,7 @@ class BackendTest extends EntityUnitTestBase { /** @var \Drupal\search_api\IndexInterface $index */ $index = $this->getIndex(); - $index->getFields(FALSE)[$this->getFieldId('body')]->setIndexed(TRUE, TRUE); + $index->getCachedFields(FALSE)[$this->getFieldId('body')]->setIndexed(TRUE, TRUE); $processors = $index->getProcessorSettings(); $processors['html_filter'] = array( @@ -207,7 +207,7 @@ class BackendTest extends EntityUnitTestBase { $processors = $index->getProcessorSettings(); unset($processors['html_filter']); $index->setProcessorSettings($processors); - $index->getFields()[$this->getFieldId('body')]->setIndexed(FALSE, TRUE); + $index->getCachedFields()[$this->getFieldId('body')]->setIndexed(FALSE, TRUE); $index->save(); } @@ -864,7 +864,7 @@ class BackendTest extends EntityUnitTestBase { /** @var \Drupal\search_api\IndexInterface $index */ $index = $this->getIndex(); $index->resetCaches(); - $fields = $index->getFields(FALSE); + $fields = $index->getCachedFields(FALSE); $price_field = $fields[$this->getFieldId('prices')]; $price_field->setType('decimal')->setIndexed(TRUE, TRUE); $success = $index->save(); @@ -910,7 +910,7 @@ class BackendTest extends EntityUnitTestBase { $this->assertEqual($count, 1, 'Indexing an item with an empty value for a non string field worked.'); // Regression test for #2471509. - $index->getFields(FALSE)[$this->getFieldId('body')]->setIndexed(TRUE, TRUE); + $index->getCachedFields(FALSE)[$this->getFieldId('body')]->setIndexed(TRUE, TRUE); $index->save(); $this->indexItems($this->indexId); @@ -927,7 +927,7 @@ class BackendTest extends EntityUnitTestBase { // Regression test for #2616268. $index = $this->getIndex(); - $index->getFields()[$this->getFieldId('body')]->setType('string', TRUE); + $index->getCachedFields()[$this->getFieldId('body')]->setType('string', TRUE); $index->save(); $count = $this->indexItems($this->indexId); $this->assertEqual($count, 8, 'Switching type from text to string worked.'); @@ -940,7 +940,7 @@ class BackendTest extends EntityUnitTestBase { $this->assertIgnored($results); $this->assertWarnings($results); - $index->getFields()[$this->getFieldId('body')]->setIndexed(FALSE, TRUE); + $index->getCachedFields()[$this->getFieldId('body')]->setIndexed(FALSE, TRUE); $index->save(); } diff --git a/src/Entity/Index.php b/src/Entity/Index.php index d814c8d..7763f02 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -201,12 +201,14 @@ class Index extends ConfigEntityBase implements IndexInterface { * @var \Drupal\search_api\Item\GenericFieldInterface[][][]|null * * @see computeFields() - * @see getFields() + * @see getCachedFields() * @see getFieldsByDatasource() * @see getAdditionalFields() * @see getAdditionalFieldsByDatasource() */ - protected $fields; + protected $cachedFields; + + protected $fields = array(); /** * Cached fields data, grouped by datasource and indexed state. @@ -354,6 +356,21 @@ class Index extends ConfigEntityBase implements IndexInterface { /** * {@inheritdoc} */ + public function setFields(array $fields = array()) { + $this->fields = $fields; + return $this; + } + + /** + * {@inheritdoc} + */ + public function getFields() { + return $this->fields; + } + + /** + * {@inheritdoc} + */ public function isValidDatasource($datasource_id) { $datasources = $this->getDatasources(); return !empty($datasources[$datasource_id]); @@ -605,10 +622,10 @@ class Index extends ConfigEntityBase implements IndexInterface { /** * {@inheritdoc} */ - public function getFields($only_indexed = TRUE) { + public function getCachedFields($only_indexed = TRUE) { $this->computeFields(); $only_indexed = $only_indexed ? 1 : 0; - return $this->fields[$only_indexed]['fields']; + return $this->cachedFields[$only_indexed]['fields']; } /** @@ -622,18 +639,18 @@ class Index extends ConfigEntityBase implements IndexInterface { // @todo Since labels and descriptions are translated, we probably need to // cache per language? $cid = $this->getCacheId(); - if (empty($this->fields)) { + if (empty($this->cachedFields)) { if ($cached = \Drupal::cache()->get($cid)) { - $this->fields = $cached->data; - if ($this->fields) { - $this->updateFieldsIndex($this->fields); + $this->cachedFields = $cached->data; + if ($this->cachedFields) { + $this->updateFieldsIndex($this->cachedFields); } } } // If not cached, fetch the list of fields and their properties. - if (empty($this->fields)) { - $this->fields = array( + if (empty($this->cachedFields)) { + $this->cachedFields = array( 0 => array( 'fields' => array(), 'additional fields' => array(), @@ -662,7 +679,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $vars['%index'] = $this->label(); \Drupal::logger('search_api')->warning('Warning while retrieving available fields for index %index: could not find a type mapping for the following fields: @fields.', $vars); } - \Drupal::cache()->set($cid, $this->fields, Cache::PERMANENT, $this->getCacheTags()); + \Drupal::cache()->set($cid, $this->cachedFields, Cache::PERMANENT, $this->getCacheTags()); } } @@ -710,7 +727,7 @@ class Index extends ConfigEntityBase implements IndexInterface { */ protected function convertPropertyDefinitionsToFields(array $properties, $datasource_id = NULL, $prefix = '', $label_prefix = '') { $type_mapping = Utility::getFieldTypeMapping(); - $field_options = isset($this->options['fields']) ? $this->options['fields'] : array(); + $field_options = $this->fields; $enabled_additional_fields = isset($this->options['additional fields']) ? $this->options['additional fields'] : array(); // All field identifiers should start with the datasource ID. @@ -774,12 +791,12 @@ class Index extends ConfigEntityBase implements IndexInterface { if ($original_property instanceof PropertyInterface) { $additional_field->setHidden($original_property->isHidden()); } - $this->fields[0]['additional fields'][$key] = $additional_field; + $this->cachedFields[0]['additional fields'][$key] = $additional_field; if ($additional_field->isEnabled()) { while ($pos = strrpos($property_path, ':')) { $property_path = substr($property_path, 0, $pos); /** @var \Drupal\search_api\Item\AdditionalFieldInterface $additional_field */ - $additional_field = $this->fields[0]['additional fields'][$property_path]; + $additional_field = $this->cachedFields[0]['additional fields'][$property_path]; $additional_field->setEnabled(TRUE); $additional_field->setLocked(); } @@ -838,7 +855,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $field->setTypeLocked($original_property->isTypeLocked()); $field->setHidden($original_property->isHidden()); } - $this->fields[0]['fields'][$key] = $field; + $this->cachedFields[0]['fields'][$key] = $field; if (isset($field_options[$key]) || $field->isIndexedLocked()) { $field->setIndexed(TRUE); if (isset($field_options[$key])) { @@ -847,7 +864,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $field->setBoost($field_options[$key]['boost']); } } - $this->fields[1]['fields'][$key] = $field; + $this->cachedFields[1]['fields'][$key] = $field; } } foreach ($recurse as $arguments) { @@ -858,8 +875,8 @@ class Index extends ConfigEntityBase implements IndexInterface { $sort_by_label = function(GenericFieldInterface $field1, GenericFieldInterface $field2) { return strnatcasecmp($field1->getLabel(), $field2->getLabel()); }; - uasort($this->fields[0]['fields'], $sort_by_label); - uasort($this->fields[0]['additional fields'], $sort_by_label); + uasort($this->cachedFields[0]['fields'], $sort_by_label); + uasort($this->cachedFields[0]['additional fields'], $sort_by_label); } /** @@ -872,7 +889,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->datasourceFields = array_fill_keys($this->datasources, array(array(), array())); $this->datasourceFields[NULL] = array(array(), array()); /** @var \Drupal\search_api\Item\FieldInterface $field */ - foreach ($this->fields[0]['fields'] as $field_id => $field) { + foreach ($this->cachedFields[0]['fields'] as $field_id => $field) { $this->datasourceFields[$field->getDatasourceId()][0][$field_id] = $field; if ($field->isIndexed()) { $this->datasourceFields[$field->getDatasourceId()][1][$field_id] = $field; @@ -887,7 +904,7 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function getAdditionalFields() { $this->computeFields(); - return $this->fields[0]['additional fields']; + return $this->cachedFields[0]['additional fields']; } /** @@ -899,7 +916,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->datasourceAdditionalFields = array_fill_keys($this->datasources, array()); $this->datasourceAdditionalFields[NULL] = array(); /** @var \Drupal\search_api\Item\FieldInterface $field */ - foreach ($this->fields[0]['additional fields'] as $field_id => $field) { + foreach ($this->cachedFields[0]['additional fields'] as $field_id => $field) { $this->datasourceAdditionalFields[$field->getDatasourceId()][$field_id] = $field; } } @@ -914,8 +931,8 @@ class Index extends ConfigEntityBase implements IndexInterface { if (!isset($this->fulltextFields[$i])) { $this->fulltextFields[$i] = array(); if ($only_indexed) { - if (isset($this->options['fields'])) { - foreach ($this->options['fields'] as $key => $field) { + if (isset($this->fields)) { + foreach ($this->fields as $key => $field) { if (Utility::isTextType($field['type'])) { $this->fulltextFields[$i][] = $key; } @@ -923,7 +940,7 @@ class Index extends ConfigEntityBase implements IndexInterface { } } else { - foreach ($this->getFields(FALSE) as $key => $field) { + foreach ($this->getCachedFields(FALSE) as $key => $field) { if (Utility::isTextType($field->getType())) { $this->fulltextFields[$i][] = $key; } @@ -1024,7 +1041,7 @@ class Index extends ConfigEntityBase implements IndexInterface { if (!$this->status) { throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (index is disabled)", array('%index' => $this->label()))); } - if (empty($this->options['fields'])) { + if (empty($this->fields)) { throw new SearchApiException(new FormattableMarkup("Couldn't index values on index %index (no fields selected)", array('%index' => $this->label()))); } @@ -1192,6 +1209,7 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->datasourcePlugins = NULL; $this->trackerPlugin = NULL; $this->serverInstance = NULL; + $this->cachedFields = NULL; $this->fields = NULL; $this->datasourceFields = NULL; $this->fulltextFields = NULL; @@ -1257,15 +1275,15 @@ class Index extends ConfigEntityBase implements IndexInterface { if ($property instanceof PropertyInterface && $property->isIndexedLocked()) { $settings = $property->getFieldSettings(); // Don't overwrite settings that aren't locked (like the boost). - if (isset($this->options['fields'][$key])) { - $settings += $this->options['fields'][$key]; + if (isset($this->fields[$key])) { + $settings += $this->fields[$key]; } if (empty($settings['type']) || $property->isTypeLocked()) { $mapping = Utility::getFieldTypeMapping(); $type = $property->getDataType(); $settings['type'] = !empty($mapping[$type]) ? $mapping[$type] : 'string'; } - $this->options['fields'][$key] = $settings; + $this->fields[$key] = $settings; $change = TRUE; } } @@ -1555,7 +1573,7 @@ class Index extends ConfigEntityBase implements IndexInterface { unset($properties['datasourcePlugins']); unset($properties['trackerPlugin']); unset($properties['serverInstance']); - unset($properties['fields']); + unset($properties['cachedFields']); unset($properties['datasourceFields']); unset($properties['fulltextFields']); unset($properties['processors']); diff --git a/src/Form/IndexFieldsForm.php b/src/Form/IndexFieldsForm.php index bb56cc6..1ee223f 100644 --- a/src/Form/IndexFieldsForm.php +++ b/src/Form/IndexFieldsForm.php @@ -106,6 +106,8 @@ class IndexFieldsForm extends EntityForm { public function buildForm(array $form, FormStateInterface $form_state) { $index = $this->entity; + debug($index->getFields()); + // Set an appropriate page title. $form['#title'] = $this->t('Manage fields for search index %label', array('%label' => $index->label())); $form['#tree'] = TRUE; @@ -256,7 +258,7 @@ class IndexFieldsForm extends EntityForm { // Store the fields configuration. $values = $form_state->getValues(); $fields = $values['fields']; - foreach ($index->getFields(FALSE) as $field_id => $field) { + foreach ($index->getCachedFields(FALSE) as $field_id => $field) { if (isset($fields[$field_id])) { $field->setType($fields[$field_id]['type']); $field->setBoost($fields[$field_id]['boost']); diff --git a/src/IndexInterface.php b/src/IndexInterface.php index 78f9d3b..03c2674 100644 --- a/src/IndexInterface.php +++ b/src/IndexInterface.php @@ -132,6 +132,9 @@ interface IndexInterface extends ConfigEntityInterface { */ public function getDatasourceIds(); + public function getFields(); + public function setFields(array $fields); + /** * Determines whether the given datasource ID is valid for this index. * @@ -336,7 +339,7 @@ interface IndexInterface extends ConfigEntityInterface { * An array of all known (or indexed, if $only_indexed is TRUE) fields for * this index, keyed by field identifier. */ - public function getFields($only_indexed = TRUE); + public function getCachedFields($only_indexed = TRUE); /** * Returns a list of all known fields of a specific datasource. diff --git a/src/Item/Field.php b/src/Item/Field.php index 0660ca7..3039aef 100644 --- a/src/Item/Field.php +++ b/src/Item/Field.php @@ -79,10 +79,10 @@ class Field implements \IteratorAggregate, FieldInterface { public function setType($type, $notify = FALSE) { $this->type = $type; if ($notify) { - $fields = $this->index->getOption('fields', array()); + $fields = $this->index->getFields(); if (isset($fields[$this->fieldIdentifier])) { $fields[$this->fieldIdentifier]['type'] = $type; - $this->index->setOption('fields', $fields); + $this->index->setFields($fields); } } return $this; @@ -140,7 +140,7 @@ class Field implements \IteratorAggregate, FieldInterface { */ public function isIndexed() { if (!isset($this->indexed)) { - $fields = $this->index->getOption('fields', array()); + $fields = $this->index->getFields(); $this->indexed = isset($fields[$this->fieldIdentifier]); } return $this->isIndexedLocked() || $this->indexed; @@ -152,7 +152,7 @@ class Field implements \IteratorAggregate, FieldInterface { public function setIndexed($indexed, $notify = FALSE) { $this->indexed = (bool) $indexed; if ($notify) { - $fields = $this->index->getOption('fields', array()); + $fields = $this->index->getFields(); if ($indexed) { $fields[$this->fieldIdentifier] = array('type' => $this->getType()); if (($boost = $this->getBoost()) != 1.0) { @@ -162,7 +162,7 @@ class Field implements \IteratorAggregate, FieldInterface { else { unset($fields[$this->fieldIdentifier]); } - $this->index->setOption('fields', $fields); + $this->index->setFields($fields); } return $this; } @@ -172,7 +172,7 @@ class Field implements \IteratorAggregate, FieldInterface { */ public function getBoost() { if (!isset($this->boost)) { - $fields = $this->index->getOption('fields', array()); + $fields = $this->index->getFields(); $this->boost = isset($fields[$this->fieldIdentifier]['boost']) ? (float) $fields[$this->fieldIdentifier]['boost'] : 1.0; } return $this->boost; @@ -185,7 +185,7 @@ class Field implements \IteratorAggregate, FieldInterface { $boost = (float) $boost; $this->boost = $boost; if ($notify) { - $fields = $this->index->getOption('fields', array()); + $fields = $this->index->getFields(); if (isset($fields[$this->fieldIdentifier])) { if ($boost != 1.0) { $fields[$this->fieldIdentifier]['boost'] = $boost; @@ -193,7 +193,7 @@ class Field implements \IteratorAggregate, FieldInterface { else { unset($fields[$this->fieldIdentifier]['boost']); } - $this->index->setOption('fields', $fields); + $this->index->setFields($fields); } } return $this; diff --git a/src/Plugin/search_api/datasource/ContentEntity.php b/src/Plugin/search_api/datasource/ContentEntity.php index 3c53b91..db1a129 100644 --- a/src/Plugin/search_api/datasource/ContentEntity.php +++ b/src/Plugin/search_api/datasource/ContentEntity.php @@ -868,7 +868,7 @@ class ContentEntity extends DatasourcePluginBase { // @todo This should definitely be the responsibility of the index class, // this is nothing specific to the datasource. $fields = array(); - foreach ($this->getIndex()->getFields() as $field) { + foreach ($this->getIndex()->getCachedFields() as $field) { if ($field->getDatasourceId() === $this->pluginId) { $fields[] = $field->getPropertyPath(); } diff --git a/src/Plugin/search_api/processor/AggregatedFields.php b/src/Plugin/search_api/processor/AggregatedFields.php index e981379..8dba920 100644 --- a/src/Plugin/search_api/processor/AggregatedFields.php +++ b/src/Plugin/search_api/processor/AggregatedFields.php @@ -105,7 +105,7 @@ class AggregatedFields extends ProcessorPluginBase { $types = $this->getTypes(); // Get the available fields for this index. - $fields = $this->index->getFields(FALSE); + $fields = $this->index->getCachedFields(FALSE); $field_options = array(); $field_properties = array(); @@ -399,7 +399,7 @@ class AggregatedFields extends ProcessorPluginBase { } $types = $this->getTypes('type'); if (isset($this->configuration['fields'])) { - $index_fields = $this->index->getFields(FALSE); + $index_fields = $this->index->getCachedFields(FALSE); foreach ($this->configuration['fields'] as $field_id => $field) { $definition = array( 'label' => $field['label'], diff --git a/src/Plugin/search_api/processor/Highlight.php b/src/Plugin/search_api/processor/Highlight.php index c6d154b..1d2372a 100644 --- a/src/Plugin/search_api/processor/Highlight.php +++ b/src/Plugin/search_api/processor/Highlight.php @@ -237,7 +237,7 @@ class Highlight extends ProcessorPluginBase { // All the index's fulltext fields, grouped by datasource. $fulltext_fields = array(); - foreach ($this->index->getFields() as $field_id => $field) { + foreach ($this->index->getCachedFields() as $field_id => $field) { if (Utility::isTextType($field->getType())) { $fulltext_fields[$field->getDatasourceId()][$field_id] = $field; } diff --git a/src/Plugin/views/argument/SearchApiFulltext.php b/src/Plugin/views/argument/SearchApiFulltext.php index 44b939c..d197998 100644 --- a/src/Plugin/views/argument/SearchApiFulltext.php +++ b/src/Plugin/views/argument/SearchApiFulltext.php @@ -118,7 +118,7 @@ class SearchApiFulltext extends SearchApiArgument { return array(); } - $fields_info = $index->getFields(); + $fields_info = $index->getCachedFields(); foreach ($index->getFulltextFields() as $field_id) { $fields[$field_id] = $fields_info[$field_id]->getPrefixedLabel(); } diff --git a/src/Plugin/views/argument/SearchApiMoreLikeThis.php b/src/Plugin/views/argument/SearchApiMoreLikeThis.php index f1da51f..4b777ba 100644 --- a/src/Plugin/views/argument/SearchApiMoreLikeThis.php +++ b/src/Plugin/views/argument/SearchApiMoreLikeThis.php @@ -79,7 +79,7 @@ class SearchApiMoreLikeThis extends SearchApiArgument { } $fields = isset($this->options['fields']) ? $this->options['fields'] : array(); if (!$fields) { - foreach ($this->query->getIndex()->getOption('fields', array()) as $key => $field) { + foreach ($this->query->getIndex()->getFields() as $key => $field) { $fields[] = $key; } } diff --git a/src/Processor/FieldsProcessorPluginBase.php b/src/Processor/FieldsProcessorPluginBase.php index 340a4ab..b00b70d 100644 --- a/src/Processor/FieldsProcessorPluginBase.php +++ b/src/Processor/FieldsProcessorPluginBase.php @@ -47,7 +47,7 @@ abstract class FieldsProcessorPluginBase extends ProcessorPluginBase { public function buildConfigurationForm(array $form, FormStateInterface $form_state) { $form = parent::buildConfigurationForm($form, $form_state); - $fields = $this->index->getFields(); + $fields = $this->index->getCachedFields(); $field_options = array(); $default_fields = array(); if (isset($this->configuration['fields'])) { @@ -223,7 +223,7 @@ abstract class FieldsProcessorPluginBase extends ProcessorPluginBase { * passed by reference. */ protected function processConditions(array &$conditions) { - $fields = $this->index->getFields(); + $fields = $this->index->getCachedFields(); foreach ($conditions as $key => &$condition) { if ($condition instanceof ConditionInterface) { $field = $condition->getField(); diff --git a/src/Tests/CustomDataTypesUnitTest.php b/src/Tests/CustomDataTypesUnitTest.php index d75662e..829dbb3 100644 --- a/src/Tests/CustomDataTypesUnitTest.php +++ b/src/Tests/CustomDataTypesUnitTest.php @@ -80,7 +80,7 @@ class CustomDataTypesUnitTest extends EntityUnitTestBase { */ public function testCustomDataTypes() { $original_value = $this->entities[1]->get('name')->value; - $original_type = $this->index->getFields()['entity:entity_test/name']->getType(); + $original_type = $this->index->getCachedFields()['entity:entity_test/name']->getType(); $item = $this->index->loadItem('entity:entity_test/1:en'); $item = Utility::createItemFromObject($this->index, $item, 'entity:entity_test/1:en'); @@ -95,7 +95,7 @@ class CustomDataTypesUnitTest extends EntityUnitTestBase { // Reset the fields on the item and change to the supported data type. $item->setFieldsExtracted(FALSE); $item->setFields(array()); - $this->index->getFields()['entity:entity_test/name']->setType('search_api_test_data_type'); + $this->index->getCachedFields()['entity:entity_test/name']->setType('search_api_test_data_type'); $name_field = $item->getField('entity:entity_test/name'); $processed_value = $name_field->getValues()[0]; @@ -107,7 +107,7 @@ class CustomDataTypesUnitTest extends EntityUnitTestBase { // Reset the fields on the item and change to the non-supported data type. $item->setFieldsExtracted(FALSE); $item->setFields(array()); - $this->index->getFields()['entity:entity_test/name']->setType('search_api_unsupported_test_data_type'); + $this->index->getCachedFields()['entity:entity_test/name']->setType('search_api_unsupported_test_data_type'); $name_field = $item->getField('entity:entity_test/name'); $processed_value = $name_field->getValues()[0]; @@ -119,7 +119,7 @@ class CustomDataTypesUnitTest extends EntityUnitTestBase { // Reset the fields on the item and change to the data altering data type. $item->setFieldsExtracted(FALSE); $item->setFields(array()); - $this->index->getFields()['entity:entity_test/name']->setType('search_api_altering_test_data_type'); + $this->index->getCachedFields()['entity:entity_test/name']->setType('search_api_altering_test_data_type'); $name_field = $item->getField('entity:entity_test/name'); $processed_value = $name_field->getValues()[0]; diff --git a/src/Tests/IntegrationTest.php b/src/Tests/IntegrationTest.php index 4d71336..7b8888a 100644 --- a/src/Tests/IntegrationTest.php +++ b/src/Tests/IntegrationTest.php @@ -62,6 +62,7 @@ class IntegrationTest extends WebTestBase { * Tests various operations via the Search API's admin UI. */ public function testFramework() { + $this->drupalLogin($this->adminUser); // Test that the overview page exists and its permissions work. @@ -450,8 +451,10 @@ class IntegrationTest extends WebTestBase { $edit = array('fields[entity:node/field__field_][indexed]' => 1); $this->drupalGet($this->getIndexPath('fields')); + $this->assertHtmlEscaped($field_name); $this->drupalPostForm(NULL, $edit, $this->t('Save changes')); $this->assertHtmlEscaped($field_name); + $this->assertFieldChecked('edit-fields-entitynodefield-field-indexed'); $edit = array( 'datasource_configs[entity:node][default]' => 1, @@ -485,7 +488,7 @@ class IntegrationTest extends WebTestBase { $this->indexStorage->resetCache(array($this->indexId)); /** @var $index \Drupal\search_api\IndexInterface */ $index = $this->indexStorage->load($this->indexId); - $fields = $index->getFields(FALSE); + $fields = $index->getCachedFields(FALSE); $this->assertEqual($fields['entity:node/nid']->isIndexed(), $edit['fields[entity:node/nid][indexed]'], 'nid field is indexed.'); $this->assertEqual($fields['entity:node/title']->isIndexed(), $edit['fields[entity:node/title][indexed]'], 'title field is indexed.'); @@ -532,7 +535,7 @@ class IntegrationTest extends WebTestBase { $this->indexStorage->resetCache(array($this->indexId)); /** @var $index \Drupal\search_api\IndexInterface */ $index = $this->indexStorage->load($this->indexId); - $fields = $index->getFields(); + $fields = $index->getCachedFields(); $this->assertTrue(!isset($fields['entity:node/body']), 'The body field has been removed from the index.'); } diff --git a/src/Tests/Processor/ProcessorTestBase.php b/src/Tests/Processor/ProcessorTestBase.php index 57a9ed0..93ea110 100644 --- a/src/Tests/Processor/ProcessorTestBase.php +++ b/src/Tests/Processor/ProcessorTestBase.php @@ -92,7 +92,7 @@ abstract class ProcessorTestBase extends EntityUnitTestBase { 'tracker' => 'default', )); $this->index->setServer($this->server); - $this->index->setOption('fields', array( + $this->index->setFields(array( 'entity:comment/subject' => array( 'type' => 'text', ), diff --git a/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml b/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml index d76f3d7..5bd2ee6 100644 --- a/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml +++ b/tests/search_api_test_db/config/install/search_api.index.database_search_index.yml @@ -8,25 +8,25 @@ processors: weights: preprocess_index: -50 settings: { } +fields: + 'entity:entity_test/id': + type: integer + 'entity:entity_test/name': + type: text + boost: 5.0 + 'entity:entity_test/body': + type: text + 'entity:entity_test/type': + type: string + 'entity:entity_test/keywords': + type: string + 'entity:entity_test/category': + type: string + search_api_language: + type: string options: cron_limit: -1 index_directly: false - fields: - 'entity:entity_test/id': - type: integer - 'entity:entity_test/name': - type: text - boost: 5.0 - 'entity:entity_test/body': - type: text - 'entity:entity_test/type': - type: string - 'entity:entity_test/keywords': - type: string - 'entity:entity_test/category': - type: string - search_api_language: - type: string datasources: - 'entity:entity_test' datasource_configs: { } diff --git a/tests/search_api_test_hooks/search_api_test_hooks.module b/tests/search_api_test_hooks/search_api_test_hooks.module index d400acd..4475cd1 100644 --- a/tests/search_api_test_hooks/search_api_test_hooks.module +++ b/tests/search_api_test_hooks/search_api_test_hooks.module @@ -85,7 +85,7 @@ function search_api_test_hooks_search_api_field_type_mapping_alter(array &$mappi */ function search_api_test_hooks_search_api_query_andrew_hill_alter(\Drupal\search_api\Query\QueryInterface &$query) { // Exclude the node with ID 2 from the search results. - $fields = $query->getIndex()->getFields(); + $fields = $query->getIndex()->getCachedFields(); foreach ($query->getIndex()->getDatasources() as $datasource_id => $datasource) { if ($datasource->getEntityTypeId() == 'node') { $field = \Drupal\search_api\Utility::createCombinedId($datasource_id, 'nid');