diff --git a/search_api_db/tests/src/Kernel/BackendTest.php b/search_api_db/tests/src/Kernel/BackendTest.php index 3f5146c..a294032 100644 --- a/search_api_db/tests/src/Kernel/BackendTest.php +++ b/search_api_db/tests/src/Kernel/BackendTest.php @@ -1076,6 +1076,7 @@ class BackendTest extends KernelTestBase { ); $field = Utility::createField($index, $property_name, $field_info); $index->addField($field); + $index->save(); } } diff --git a/src/Entity/Index.php b/src/Entity/Index.php index 188ac80..9c55a45 100644 --- a/src/Entity/Index.php +++ b/src/Entity/Index.php @@ -117,6 +117,13 @@ class Index extends ConfigEntityBase implements IndexInterface { protected $field_settings = array(); /** + * An array of field instances. + * + * @var \Drupal\search_api\Item\FieldInterface[]|null + */ + protected $fieldInstances = NULL; + + /** * An array of options configuring this index. * * @var array @@ -605,7 +612,7 @@ class Index extends ConfigEntityBase implements IndexInterface { throw new SearchApiException(new FormattableMarkup('Cannot add field with machine name %field_id: machine name is already taken.', $args)); } - $this->field_settings[$field_id] = $field->getSettings(); + $this->fieldInstances[$field_id] = $field; return $this; } @@ -624,11 +631,11 @@ class Index extends ConfigEntityBase implements IndexInterface { } if (isset($this->getFields()[$new_field_id])) { $args['%field_id'] = $new_field_id; - throw new SearchApiException(new FormattableMarkup('%field_id is a reserved value and cannot be used as the machine name of a normal field.', $args)); + throw new SearchApiException(new FormattableMarkup('%field_id already exists and can\'t be used as a new field id.', $args)); } - $this->field_settings[$new_field_id] = $this->field_settings[$old_field_id]; - unset($this->field_settings[$old_field_id]); + $this->fieldInstances[$new_field_id] = $this->fieldInstances[$old_field_id]; + unset($this->fieldInstances[$old_field_id]); return $this; } @@ -646,7 +653,7 @@ class Index extends ConfigEntityBase implements IndexInterface { throw new SearchApiException(new FormattableMarkup('Cannot remove field with machine name %field_id: field is locked.', $args)); } - unset($this->field_settings[$field_id]); + unset($this->fieldInstances[$field_id]); return $this; } @@ -655,11 +662,17 @@ class Index extends ConfigEntityBase implements IndexInterface { * {@inheritdoc} */ public function getFields() { + if ($this->fieldInstances !== NULL) { + return $this->fieldInstances; + } + $fields = array(); foreach ($this->field_settings as $key => $field_info) { $fields[$key] = Utility::createField($this, $key, $field_info); } + $this->fieldInstances = $fields; + return $fields; } @@ -689,8 +702,8 @@ class Index extends ConfigEntityBase implements IndexInterface { */ public function getFulltextFields() { $fulltext_fields = array(); - foreach ($this->field_settings as $key => $field_info) { - if (Utility::isTextType($field_info['type'])) { + foreach ($this->getFields() as $key => $field) { + if (Utility::isTextType($field->getType())) { $fulltext_fields[] = $key; } } @@ -996,6 +1009,11 @@ class Index extends ConfigEntityBase implements IndexInterface { $this->disable(); } + $this->field_settings = array(); + foreach ($this->getFields() as $field_id => $field) { + $this->field_settings[$field_id] = $field->getSettings(); + } + // Remove all "locked" and "hidden" flags from all fields of the index. If // they are still valid, they should be re-added by the processors. foreach ($this->field_settings as $field_id => $field_settings) { @@ -1558,6 +1576,7 @@ class Index extends ConfigEntityBase implements IndexInterface { unset($properties['trackerInstance']); unset($properties['serverInstance']); unset($properties['processorInstances']); + unset($properties['fieldInstances']); unset($properties['cache']); return array_keys($properties); } diff --git a/tests/src/Kernel/CliTest.php b/tests/src/Kernel/CliTest.php index 95c3395..c99833e 100644 --- a/tests/src/Kernel/CliTest.php +++ b/tests/src/Kernel/CliTest.php @@ -109,8 +109,8 @@ class CliTest extends KernelTestBase { $total_items = $index->getTrackerInstance()->getTotalItemsCount(); $indexed_items = $index->getTrackerInstance()->getIndexedItemsCount(); - $this->assertEquals($total_items, 2, 'The 2 items are tracked.'); - $this->assertEquals($indexed_items, 0, 'No items are indexed'); + $this->assertEquals(2, $total_items,'The 2 items are tracked.'); + $this->assertEquals(0, $indexed_items, 'No items are indexed'); EntityTest::create(array( 'name' => 'foo bar baz föö smile', @@ -130,8 +130,8 @@ class CliTest extends KernelTestBase { $total_items = $index->getTrackerInstance()->getTotalItemsCount(); $indexed_items = $index->getTrackerInstance()->getIndexedItemsCount(); - $this->assertEquals($total_items, 4, 'All 4 items are tracked.'); - $this->assertEquals($indexed_items, 2, '2 items are indexed'); + $this->assertEquals(4, $total_items, 'All 4 items are tracked.'); + $this->assertEquals(0, $indexed_items, '0 items are indexed'); } }