diff --git a/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php b/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php index 05f338b..b67f22b 100644 --- a/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php +++ b/core/modules/entity/lib/Drupal/entity/EntityFieldQuery.php @@ -804,7 +804,7 @@ class EntityFieldQuery { $select_query->addExpression(':entity_type', 'entity_type', array(':entity_type' => $entity_type)); // Process the property conditions. foreach ($this->propertyConditions as $property_condition) { - $this->addCondition($select_query, "$base_table." . $property_condition['column'], $property_condition); + $this->addCondition($select_query, $base_table . '.' . $property_condition['column'], $property_condition); } // Process the four possible entity condition. // The id field is always present in entity keys. @@ -812,7 +812,7 @@ class EntityFieldQuery { $id_map['entity_id'] = $sql_field; $select_query->addField($base_table, $sql_field, 'entity_id'); if (isset($this->entityConditions['entity_id'])) { - $this->addCondition($select_query, $sql_field, $this->entityConditions['entity_id']); + $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['entity_id']); } // If there is a revision key defined, use it. @@ -820,7 +820,7 @@ class EntityFieldQuery { $sql_field = $entity_info['entity keys']['revision']; $select_query->addField($base_table, $sql_field, 'revision_id'); if (isset($this->entityConditions['revision_id'])) { - $this->addCondition($select_query, $sql_field, $this->entityConditions['revision_id']); + $this->addCondition($select_query, $base_table . '.' . $sql_field, $this->entityConditions['revision_id']); } } else { @@ -845,7 +845,13 @@ class EntityFieldQuery { } $id_map['bundle'] = $sql_field; if (isset($this->entityConditions['bundle'])) { - $this->addCondition($select_query, $sql_field, $this->entityConditions['bundle'], $having); + if (!empty($entity_info['entity keys']['bundle'])) { + $this->addCondition($select_query, $sql_field, $this->entityConditions['bundle'], $having); + } + else { + // This entity has no bundle, so invalidate the query. + $select_query->where('1 = 0'); + } } // Order the query. @@ -858,7 +865,7 @@ class EntityFieldQuery { $select_query->orderBy($id_map[$key], $order['direction']); } elseif ($order['type'] == 'property') { - $select_query->orderBy("$base_table." . $order['specifier'], $order['direction']); + $select_query->orderBy($base_table . '.' . $order['specifier'], $order['direction']); } } diff --git a/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php b/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php index 9b4a91c..47a4fe2 100644 --- a/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php +++ b/core/modules/entity/lib/Drupal/entity/Tests/EntityFieldQueryTest.php @@ -1563,4 +1563,23 @@ class EntityFieldQueryTest extends WebTestBase { $this->fail('Exception thrown: '. $e->getMessage()); } } + + /** + * Tests EFQ table prefixing with multiple conditions and an altered join. + * + * @see field_test_query_efq_table_prefixing_test_alter() + */ + function testTablePrefixing() { + $query = new EntityFieldQuery(); + $query = $query + ->entityCondition('entity_type', 'test_entity') + ->entityCondition('bundle', 'test_bundle') + ->entityCondition('entity_id', '1') + ->addTag('efq_table_prefixing_test'); + + $expected = array(array('test_entity', 1)); + + $this->assertEntityFieldQuery($query, $expected, 'An EntityFieldQuery returns the expected results when altered with an additional join on the base table.'); + } + } diff --git a/core/modules/field/tests/modules/field_test/field_test.module b/core/modules/field/tests/modules/field_test/field_test.module index 8a4c7cd..81f90a5 100644 --- a/core/modules/field/tests/modules/field_test/field_test.module +++ b/core/modules/field/tests/modules/field_test/field_test.module @@ -262,3 +262,14 @@ function field_test_field_widget_form_alter(&$element, &$form_state, $context) { break; } } + +/** + * Implements hook_query_TAG_alter() for tag 'efq_table_prefixing_test'. + * + * @see Drupal\entity\Tests\EntityFieldQueryTest::testTablePrefixing() + */ +function field_test_query_efq_table_prefixing_test_alter(&$query) { + // Add an additional join onto the entity base table. This will cause an + // exception if the EFQ does not properly prefix the base table. + $query->join('test_entity','te2','%alias.ftid = test_entity.ftid'); +}