From 7ea136e79cf92c7427b1144ab4ca9966097f35a1 Mon Sep 17 00:00:00 2001 From: webchick Date: Fri, 28 Aug 2015 22:58:34 -0700 Subject: [PATCH] Issue #2559309 by Berdir, jhedstrom: Logic error in SqlContentEntityStorage::countFieldData() drops dedicated field tables (again) --- .../Entity/Sql/SqlContentEntityStorage.php | 10 +++++---- .../field/src/Tests/FieldDataCountTest.php | 22 +++++++++++++++++++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php index 259d240d1b7..c826714deef 100644 --- a/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php +++ b/core/lib/Drupal/Core/Entity/Sql/SqlContentEntityStorage.php @@ -1634,10 +1634,12 @@ class SqlContentEntityStorage extends ContentEntityStorageBase implements SqlEnt foreach ($storage_definition->getColumns() as $column_name => $data) { $or->isNotNull($table_mapping->getFieldColumnName($storage_definition, $column_name)); } - $query - ->condition($or) - ->fields('t', array('entity_id')) - ->distinct(TRUE); + $query->condition($or); + if (!$as_bool) { + $query + ->fields('t', array('entity_id')) + ->distinct(TRUE); + } } elseif ($table_mapping->allowsSharedTableStorage($storage_definition)) { // Ascertain the table this field is mapped too. diff --git a/core/modules/field/src/Tests/FieldDataCountTest.php b/core/modules/field/src/Tests/FieldDataCountTest.php index aef5e7e821d..9edbbdb2bc2 100644 --- a/core/modules/field/src/Tests/FieldDataCountTest.php +++ b/core/modules/field/src/Tests/FieldDataCountTest.php @@ -8,6 +8,8 @@ namespace Drupal\field\Tests; use Drupal\Core\Entity\Sql\SqlContentEntityStorage; +use Drupal\field\Entity\FieldConfig; +use Drupal\field\Entity\FieldStorageConfig; /** * Tests counting field data records and the hasData() method on @@ -146,6 +148,20 @@ class FieldDataCountTest extends FieldUnitTestBase { * Verify that we can count a table that contains an entry with index 0. */ public function testCountWithIndex0() { + // Create a field that will require dedicated storage. + /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */ + $field_storage = FieldStorageConfig::create(array( + 'field_name' => 'field_int', + 'entity_type' => 'user', + 'type' => 'integer', + 'cardinality' => 2, + )); + $field_storage->save(); + FieldConfig::create(array( + 'field_storage' => $field_storage, + 'bundle' => 'user', + ))->save(); + // Create an entry for the anonymous user, who has user ID 0. $user = $this->storageUser ->create(array( @@ -153,11 +169,17 @@ class FieldDataCountTest extends FieldUnitTestBase { 'name' => 'anonymous', 'mail' => NULL, 'status' => FALSE, + 'field_int' => 42, )); $user->save(); + // Test shared table storage. $storage = $user->getFieldDefinition('name')->getFieldStorageDefinition(); $this->assertIdentical(TRUE, $this->storageUser->countFieldData($storage, TRUE)); + + // Test dedicated table storage. + $storage = $user->getFieldDefinition('field_int')->getFieldStorageDefinition(); + $this->assertIdentical(TRUE, $this->storageUser->countFieldData($storage, TRUE)); } }