diff --git a/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Query.php b/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Query.php index 0abb5d98b30..44023ce7781 100644 --- a/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Query.php +++ b/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Query.php @@ -69,6 +69,7 @@ class Query extends QueryBase { $entity_tables[$base_table] = drupal_get_schema($base_table); $sqlQuery = $this->connection->select($base_table, 'base_table', array('conjunction' => $this->conjunction)); $sqlQuery->addMetaData('configurable_fields', $configurable_fields); + $sqlQuery->addMetaData('entity_type', $entity_type); // Determines the key of the column to join on. This is either the entity // id key or the revision id key, depending on whether the entity type // supports revisions. diff --git a/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php b/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php index 068b26c5758..7ba89b3b792 100644 --- a/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php +++ b/core/modules/field/modules/field_sql_storage/lib/Drupal/field_sql_storage/Entity/Tables.php @@ -129,7 +129,8 @@ class Tables { if ($field['cardinality'] != 1) { $this->sqlQuery->addMetaData('simple_query', FALSE); } - $this->fieldTables[$field_name] = $this->addJoin($type, $table, "%alias.$field_id_field = base_table.$entity_id_field", $langcode); + $entity_type = $this->sqlQuery->getMetaData('entity_type'); + $this->fieldTables[$field_name] = $this->addJoin($type, $table, "%alias.$field_id_field = base_table.$entity_id_field AND %alias.entity_type = '$entity_type'", $langcode); } return $this->fieldTables[$field_name]; } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php index d146494e8e7..a7afcc60b08 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityQueryTest.php @@ -367,6 +367,37 @@ class EntityQueryTest extends WebTestBase { $this->assertResult(range(15, 1)); } + /** + * Test entity count query. + */ + protected function testCount() { + // Attach the existing 'figures' field to a second entity type so that we + // can test whether cross entity type fields produce the correct query. + $field_name = $this->figures; + $bundle = $this->randomName(); + $instance = array( + 'field_name' => $field_name, + 'entity_type' => 'test_entity_bundle', + 'bundle' => $bundle, + ); + field_create_instance($instance); + + $entity = entity_create('test_entity_bundle', array( + 'ftid' => 1, + 'fttype' => $bundle, + )); + $entity->enforceIsNew(); + $entity->setNewRevision(); + $entity->save(); + // As the single entity of this type we just saved does not have a value + // in the color field, the result should be 0. + $count = $this->factory->get('test_entity_bundle') + ->exists("$field_name.color") + ->count() + ->execute(); + $this->assertFalse($count); + } + protected function assertResult() { $assert = array(); $expected = func_get_args();