summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjhedstrom2017-09-26 04:39:06 (GMT)
committerJibran Ijaz2017-09-26 04:39:06 (GMT)
commit46f7012579a9cc5113c468c4910efd70604247e3 (patch)
treeb04cf8e113a47c04b17fcff3ac159c5055589603
parent20339b4918f0b5f1c1140ad1b6560aa05a915711 (diff)
Issue #2908109 by jhedstrom, jibran: Entity queries broken for non-DER entity reference base fields8.x-2.0-alpha6
-rw-r--r--src/Query/Tables.php7
-rw-r--r--tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module8
-rw-r--r--tests/src/Kernel/DynamicEntityReferenceBaseFieldTest.php36
-rw-r--r--tests/src/Kernel/DynamicEntityReferenceFieldTest.php60
4 files changed, 108 insertions, 3 deletions
diff --git a/src/Query/Tables.php b/src/Query/Tables.php
index c95d842..5ac74f7 100644
--- a/src/Query/Tables.php
+++ b/src/Query/Tables.php
@@ -4,6 +4,7 @@ namespace Drupal\dynamic_entity_reference\Query;
use Drupal\Core\Entity\EntityType;
use Drupal\Core\Entity\Query\Sql\Tables as BaseTables;
+use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\dynamic_entity_reference\Plugin\Field\FieldType\DynamicEntityReferenceItem;
/**
@@ -14,16 +15,16 @@ class Tables extends BaseTables {
/**
* {@inheritdoc}
*/
- protected function addNextBaseTable(EntityType $entity_type, $table, $sql_column) {
+ protected function addNextBaseTable(EntityType $entity_type, $table, $sql_column, FieldStorageDefinitionInterface $field_storage = NULL) {
// Parent method is overridden in order to choose the correct column to
// join on (string or int).
$entity_type_id_key = $entity_type->getKey('id');
- if ($entity_type_id_key !== FALSE) {
+ if (($field_storage && $field_storage->getType() === 'dynamic_entity_reference') && ($entity_type_id_key !== FALSE)) {
if (DynamicEntityReferenceItem::entityHasIntegerId($entity_type->id())) {
$sql_column .= '_int';
}
}
- return parent::addNextBaseTable($entity_type, $table, $sql_column);
+ return parent::addNextBaseTable($entity_type, $table, $sql_column, $field_storage);
}
}
diff --git a/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module
index ebb6b4d..f0bc1cd 100644
--- a/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module
+++ b/tests/modules/dynamic_entity_reference_entity_test/dynamic_entity_reference_entity_test.module
@@ -58,6 +58,14 @@ function dynamic_entity_reference_entity_test_entity_base_field_info(EntityTypeI
$fields['der'] = BaseFieldDefinition::createFromFieldStorageDefinition($fields['dynamic_references'])
->setName('der');
}
+
+ // Adds a normal entity reference field if set to do so.
+ if (\Drupal::state()->get('dynamic_entity_reference_entity_test_with_normal', FALSE)) {
+ $fields['normal_reference'] = BaseFieldDefinition::create('entity_reference')
+ ->setLabel(t('Entity ID'))
+ ->setDescription(t('The referenced ID of the entity.'))
+ ->setRequired(TRUE);
+ }
}
return $fields;
diff --git a/tests/src/Kernel/DynamicEntityReferenceBaseFieldTest.php b/tests/src/Kernel/DynamicEntityReferenceBaseFieldTest.php
index ef4b59b..f412946 100644
--- a/tests/src/Kernel/DynamicEntityReferenceBaseFieldTest.php
+++ b/tests/src/Kernel/DynamicEntityReferenceBaseFieldTest.php
@@ -297,4 +297,40 @@ class DynamicEntityReferenceBaseFieldTest extends EntityKernelTestBase {
$this->assertSame($int_column, $str_column);
}
+ /**
+ * Test entity field query with normal entity reference and DER base fields.
+ */
+ public function testEntityFieldQuery() {
+ \Drupal::state()->set('dynamic_entity_reference_entity_test_with_two_base_fields', TRUE);
+ \Drupal::state()->set('dynamic_entity_reference_entity_test_with_normal', TRUE);
+ $this->enableModules(['dynamic_entity_reference_entity_test']);
+ \Drupal::entityDefinitionUpdateManager()->applyUpdates();
+ $this->installEntitySchema('entity_test_mul');
+
+ // Add some users and test entities.
+ $accounts = $entities = [];
+ foreach (range(1, 3) as $i) {
+ $accounts[$i] = $this->createUser();
+ $entity = EntityTestMul::create();
+
+ // Add reference to user 2 for entities 2 and 3.
+ if ($i > 1) {
+ $entity->normal_reference = $accounts[2];
+ }
+
+ $entity->save();
+ $entities[$i] = $entity;
+ }
+
+ $result = \Drupal::entityTypeManager()->getStorage('entity_test_mul')->getQuery()
+ ->condition('normal_reference.entity:user.status', 1)
+ ->sort('id')
+ ->execute();
+ $expected = [
+ $entities[2]->id() => $entities[2]->id(),
+ $entities[3]->id() => $entities[3]->id(),
+ ];
+ $this->assertSame($expected, $result);
+ }
+
}
diff --git a/tests/src/Kernel/DynamicEntityReferenceFieldTest.php b/tests/src/Kernel/DynamicEntityReferenceFieldTest.php
index 88d7d11..9c17240 100644
--- a/tests/src/Kernel/DynamicEntityReferenceFieldTest.php
+++ b/tests/src/Kernel/DynamicEntityReferenceFieldTest.php
@@ -4,6 +4,7 @@ namespace Drupal\Tests\dynamic_entity_reference\Kernel;
use Drupal\config\Tests\SchemaCheckTestTrait;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
+use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestStringId;
use Drupal\field\Entity\FieldConfig;
@@ -472,4 +473,63 @@ class DynamicEntityReferenceFieldTest extends EntityKernelTestBase {
$this->assertEquals($entities[1]->id(), $referenced_entity->id());
}
+ /**
+ * Tests with normal entity reference fields.
+ */
+ public function testNormalEntityReference() {
+ // Create a field.
+ FieldStorageConfig::create([
+ 'field_name' => 'field_normal_er',
+ 'type' => 'entity_reference',
+ 'entity_type' => $this->entityType,
+ 'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
+ 'settings' => [
+ 'exclude_entity_types' => FALSE,
+ 'entity_type_ids' => [
+ 'user',
+ ],
+ ],
+ ])->save();
+
+ FieldConfig::create([
+ 'field_name' => 'field_normal_er',
+ 'entity_type' => $this->entityType,
+ 'bundle' => $this->bundle,
+ 'label' => 'Field test',
+ 'settings' => [
+ $this->referencedEntityType => [
+ 'handler' => 'default:user',
+ 'handler_settings' => [
+ 'target_bundles' => NULL,
+ ],
+ ],
+ ],
+ ])->save();
+
+ // Add some users and test entities.
+ $accounts = $entities = [];
+ foreach (range(1, 3) as $i) {
+ $accounts[$i] = $this->createUser();
+ $entity = EntityTest::create();
+
+ // Add reference to user 2 for entities 2 and 3.
+ if ($i > 1) {
+ $entity->field_normal_er = $accounts[2];
+ }
+
+ $entity->save();
+ $entities[$i] = $entity;
+ }
+
+ $result = \Drupal::entityTypeManager()->getStorage('entity_test')->getQuery()
+ ->condition('field_normal_er.entity:user.status', 1)
+ ->sort('id')
+ ->execute();
+ $expected = [
+ $entities[2]->id() => $entities[2]->id(),
+ $entities[3]->id() => $entities[3]->id(),
+ ];
+ $this->assertSame($expected, $result);
+ }
+
}