diff --git a/search_api_db/search_api_db_defaults/src/Tests/IntegrationTest.php b/search_api_db/search_api_db_defaults/src/Tests/IntegrationTest.php index 55a0ceb..a4809dd 100644 --- a/search_api_db/search_api_db_defaults/src/Tests/IntegrationTest.php +++ b/search_api_db/search_api_db_defaults/src/Tests/IntegrationTest.php @@ -108,10 +108,12 @@ class IntegrationTest extends WebTestBase { // Add tags field to Article. // Create a tags vocabulary for the 'article' content type. - $vocabulary = entity_create('taxonomy_vocabulary', array( - 'name' => 'Tags', - 'vid' => 'tags', - )); + $vocabulary = \Drupal::entityManager() + ->getStorage('taxonomy_vocabulary') + ->create(array( + 'name' => 'Tags', + 'vid' => 'tags', + )); $vocabulary->save(); $field_name = 'field_' . $vocabulary->id(); @@ -123,20 +125,20 @@ class IntegrationTest extends WebTestBase { ); $this->createEntityReferenceField('node', 'article', $field_name, 'Tags', 'taxonomy_term', 'default', $handler_settings, FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED); - entity_get_form_display('node', 'article', 'default') + $this->getEntityDisplay('form', 'node', 'article', 'default') ->setComponent($field_name, array( 'type' => 'entity_reference_autocomplete_tags', 'weight' => -4, )) ->save(); - entity_get_display('node', 'article', 'default') + $this->getEntityDisplay('view', 'node', 'article', 'default') ->setComponent($field_name, array( 'type' => 'entity_reference_label', 'weight' => 10, )) ->save(); - entity_get_display('node', 'article', 'teaser') + $this->getEntityDisplay('view', 'node', 'article', 'teaser') ->setComponent($field_name, array( 'type' => 'entity_reference_label', 'weight' => 10, @@ -207,36 +209,73 @@ class IntegrationTest extends WebTestBase { * The newly created field configuration. */ protected function createImageField($name, $type_name, $storage_settings = array(), $field_settings = array(), $widget_settings = array()) { - entity_create('field_storage_config', array( - 'field_name' => $name, - 'entity_type' => 'node', - 'type' => 'image', - 'settings' => $storage_settings, - 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1, - ))->save(); - - $field_config = entity_create('field_config', array( - 'field_name' => $name, - 'label' => $name, - 'entity_type' => 'node', - 'bundle' => $type_name, - 'required' => !empty($field_settings['required']), - 'settings' => $field_settings, - )); + \Drupal::entityManager() + ->getStorage('field_storage_config') + ->create(array( + 'field_name' => $name, + 'entity_type' => 'node', + 'type' => 'image', + 'settings' => $storage_settings, + 'cardinality' => !empty($storage_settings['cardinality']) ? $storage_settings['cardinality'] : 1, + ))->save(); + + $field_config = \Drupal::entityManager() + ->getStorage('field_config') + ->create(array( + 'field_name' => $name, + 'label' => $name, + 'entity_type' => 'node', + 'bundle' => $type_name, + 'required' => !empty($field_settings['required']), + 'settings' => $field_settings, + )); $field_config->save(); - entity_get_form_display('node', $type_name, 'default') + $this->getEntityDisplay('form', 'node', $type_name, 'default') ->setComponent($name, array( 'type' => 'image_image', 'settings' => $widget_settings, )) ->save(); - entity_get_display('node', $type_name, 'default') + $this->getEntityDisplay('view', 'node', $type_name, 'default') ->setComponent($name) ->save(); return $field_config; } + /** + * Retrieves an entity's view or form display. + * + * @param string $type + * The type of display, either "view" or "form". + * @param string $entity_type + * The entity type. + * @param string $bundle + * The bundle. + * @param string $mode + * The view or form mode. + * + * @return \Drupal\Core\Entity\Display\EntityDisplayInterface + * The requested display. + */ + protected function getEntityDisplay($type, $entity_type, $bundle, $mode) { + // Try loading the entity from configuration. + $display_storage = \Drupal::entityManager()->getStorage("entity_{$type}_display"); + $display = $display_storage->load($entity_type . '.' . $bundle . '.' . $mode); + + // If not found, create a fresh configuration object. + if (!$display) { + $display = $display_storage->create(array( + 'targetEntityType' => $entity_type, + 'bundle' => $bundle, + 'mode' => $mode, + 'status' => TRUE, + )); + } + + return $display; + } + } diff --git a/search_api_db/src/Tests/BackendTest.php b/search_api_db/src/Tests/BackendTest.php index d8001e3..33b77cb 100644 --- a/search_api_db/src/Tests/BackendTest.php +++ b/search_api_db/src/Tests/BackendTest.php @@ -761,11 +761,13 @@ class BackendTest extends EntityUnitTestBase { \Drupal::entityManager()->getStorage('search_api_server')->resetCache(array($this->serverId)); \Drupal::entityManager()->getStorage('search_api_index')->resetCache(array($this->serverId)); - entity_create('entity_test', array( - 'id' => 6, - 'prices' => array('3.5', '3.25', '3.75', '3.5'), - 'type' => 'item', - ))->save(); + \Drupal::entityManager() + ->getStorage('field_storage_config') + ->create(array( + 'id' => 6, + 'prices' => array('3.5', '3.25', '3.75', '3.5'), + 'type' => 'item', + ))->save(); $this->indexItems($this->indexId); @@ -784,10 +786,12 @@ class BackendTest extends EntityUnitTestBase { $this->assertWarnings($results); // Regression test for #2284199. - entity_create('entity_test', array( - 'id' => 7, - 'type' => 'item', - ))->save(); + \Drupal::entityManager() + ->getStorage('field_storage_config') + ->create(array( + 'id' => 7, + 'type' => 'item', + ))->save(); $count = $this->indexItems($this->indexId); $this->assertEqual($count, 1, 'Indexing an item with an empty value for a non string field worked.'); @@ -797,12 +801,14 @@ class BackendTest extends EntityUnitTestBase { $index->save(); $this->indexItems($this->indexId); - entity_create('entity_test', array( - 'id' => 8, - 'name' => 'Article with long body', - 'type' => 'article', - 'body' => 'astringlongerthanfiftycharactersthatcantbestoredbythedbbackend', - ))->save(); + \Drupal::entityManager() + ->getStorage('field_storage_config') + ->create(array( + 'id' => 8, + 'name' => 'Article with long body', + 'type' => 'article', + 'body' => 'astringlongerthanfiftycharactersthatcantbestoredbythedbbackend', + ))->save(); $count = $this->indexItems($this->indexId); $this->assertEqual($count, 1, 'Indexing an item with a word longer than 50 characters worked.'); diff --git a/src/Plugin/search_api/datasource/ContentEntity.php b/src/Plugin/search_api/datasource/ContentEntity.php index 4b80768..8e63be2 100644 --- a/src/Plugin/search_api/datasource/ContentEntity.php +++ b/src/Plugin/search_api/datasource/ContentEntity.php @@ -523,9 +523,8 @@ class ContentEntity extends DatasourcePluginBase { // For all the loaded entities, compute all their item IDs (one for each // translation). $item_ids = array(); - $index_storage = $this->getEntityManager()->getStorage($this->getEntityTypeId()); /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ - foreach ($index_storage->loadMultiple($entity_ids) as $entity_id => $entity) { + foreach ($this->getEntityStorage()->loadMultiple($entity_ids) as $entity_id => $entity) { foreach (array_keys($entity->getTranslationLanguages()) as $langcode) { $item_ids[] = "$entity_id:$langcode"; } diff --git a/src/Tests/ExampleContentTrait.php b/src/Tests/ExampleContentTrait.php index d5d4bf7..67f377f 100644 --- a/src/Tests/ExampleContentTrait.php +++ b/src/Tests/ExampleContentTrait.php @@ -36,7 +36,8 @@ trait ExampleContentTrait { protected function insertExampleContent() { $count = \Drupal::entityQuery('entity_test')->count()->execute(); - $this->entities[1] = entity_create('entity_test', array( + $entity_test_storage = \Drupal::entityManager()->getStorage('entity_test'); + $this->entities[1] = $entity_test_storage->create(array( 'id' => 1, 'name' => 'foo bar baz', 'body' => 'test test', @@ -45,7 +46,7 @@ trait ExampleContentTrait { 'category' => 'item_category' )); $this->entities[1]->save(); - $this->entities[2] = entity_create('entity_test', array( + $this->entities[2] = $entity_test_storage->create(array( 'id' => 2, 'name' => 'foo test', 'body' => 'bar test', @@ -54,14 +55,14 @@ trait ExampleContentTrait { 'category' => 'item_category' )); $this->entities[2]->save(); - $this->entities[3] = entity_create('entity_test', array( + $this->entities[3] = $entity_test_storage->create(array( 'id' => 3, 'name' => 'bar', 'body' => 'test foobar', 'type' => 'item', )); $this->entities[3]->save(); - $this->entities[4] = entity_create('entity_test', array( + $this->entities[4] = $entity_test_storage->create(array( 'id' => 4, 'name' => 'foo baz', 'body' => 'test test test', @@ -70,7 +71,7 @@ trait ExampleContentTrait { 'category' => 'article_category' )); $this->entities[4]->save(); - $this->entities[5] = entity_create('entity_test', array( + $this->entities[5] = $entity_test_storage->create(array( 'id' => 5, 'name' => 'bar baz', 'body' => 'foo',