Commit 802f07cf authored by catch's avatar catch
Browse files

fix: #3622419 EntityQuery AND condition groups do not work for fields with...

fix: #3622419 EntityQuery AND condition groups do not work for fields with unlimited cardinality after #2875033

By: dench0
By: amateescu
(cherry picked from commit 4f829c09)
parent 938f02a4
Loading
Loading
Loading
Loading
Loading
+1 −1
Changes for core/lib/Drupal/Core/Entity/Query/Sql/Tables.php: 1 added line, 1 removed line.
Original line number Diff line number Diff line
@@ -219,7 +219,7 @@ public function addField($field, $type, $langcode) {
            $next_index_prefix = "$relationship_specifier.$column";
          }
        }
        if ($index_prefix === '' && $field_storage->getCardinality() > 1) {
        if ($index_prefix === '' && $field_storage->isMultiple()) {
          $index_prefix = $condition_prefix;
        }
        $table = $this->ensureFieldTable($index_prefix, $field_storage, $type, $langcode, $base_table, $entity_id_field, $field_id_field, $delta);
+30 −2
Changes for core/tests/Drupal/KernelTests/Core/Entity/EntityQueryTest.php: 30 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@

use Drupal\Core\Database\Database;
use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\entity_test\Entity\EntityTestMulRev;
@@ -66,6 +67,13 @@ class EntityQueryTest extends EntityKernelTestBase {
   */
  public $figures;

  /**
   * Field name for the unlimited cardinality figures field.
   *
   * @var string
   */
  public $figuresUnlimited;

  /**
   * The entity_test_mulrev entity storage.
   *
@@ -84,13 +92,18 @@ protected function setUp(): void {
    $this->installConfig(['language']);

    $figures = $this->randomMachineName();
    $figures_unlimited = $this->randomMachineName();
    $greetings = $this->randomMachineName();
    foreach ([$figures => 'shape', $greetings => 'text'] as $field_name => $field_type) {
    foreach ([
      $figures => ['shape', 2],
      $figures_unlimited => ['shape', FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED],
      $greetings => ['text', 2],
    ] as $field_name => [$field_type, $cardinality]) {
      $field_storage = FieldStorageConfig::create([
        'field_name' => $field_name,
        'entity_type' => 'entity_test_mulrev',
        'type' => $field_type,
        'cardinality' => 2,
        'cardinality' => $cardinality,
      ]);
      $field_storage->save();
      $field_storages[] = $field_storage;
@@ -170,10 +183,12 @@ protected function setUp(): void {
          $entity->getTranslation($units[$key][1])->{$units[$key][0]}[] = $units[$key][2];
        }
      }
      $entity->{$figures_unlimited} = $entity->{$figures}->getValue();
      $entity->save();
    }
    $this->bundles = $bundles;
    $this->figures = $figures;
    $this->figuresUnlimited = $figures_unlimited;
    $this->greetings = $greetings;
    $this->storage = $this->container->get('entity_type.manager')->getStorage('entity_test_mulrev');
  }
@@ -184,6 +199,7 @@ protected function setUp(): void {
  public function testEntityQuery(): void {
    $greetings = $this->greetings;
    $figures = $this->figures;
    $figures_unlimited = $this->figuresUnlimited;
    $this->queryResults = $this->storage
      ->getQuery()
      ->accessCheck(FALSE)
@@ -258,6 +274,18 @@ public function testEntityQuery(): void {
    // Unit 0 and unit 1, so bits 0 1.
    $this->assertResult(3, 7, 11, 15);

    // Do the same test with an unlimited cardinality field.
    $query = $this->storage->getQuery()->accessCheck(FALSE);
    $group_blue = $query->andConditionGroup()->condition("$figures_unlimited.color", 'blue');
    $group_red = $query->andConditionGroup()->condition("$figures_unlimited.color", 'red');
    $this->queryResults = $query
      ->condition($group_blue)
      ->condition($group_red)
      ->sort('revision_id')
      ->execute();
    // Unit 0 and unit 1, so bits 0 1.
    $this->assertResult(3, 7, 11, 15);

    // Do the same test but with IN operator.
    $query = $this->storage->getQuery()->accessCheck(FALSE);
    $group_blue = $query->andConditionGroup()->condition("$figures.color", ['blue'], 'IN');