Unverified Commit 46f70125 authored by jhedstrom's avatar jhedstrom Committed by jibran

Issue #2908109 by jhedstrom, jibran: Entity queries broken for non-DER entity reference base fields

parent 20339b49
......@@ -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);
}
}
......@@ -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;
......
......@@ -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);
}
}
......@@ -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);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment