diff --git a/core/modules/user/src/UserDataQuery.php b/core/modules/user/src/UserDataQuery.php index 276a6b0..5b850af 100644 --- a/core/modules/user/src/UserDataQuery.php +++ b/core/modules/user/src/UserDataQuery.php @@ -65,8 +65,22 @@ public function setValue($value) { $is_scalar_value = is_scalar($value); if (!$is_scalar_value) { $value = serialize($value); + $this->setSerializedValue($value); } + else { + $this->createCondition('value', $value); + $this->select->condition('serialized', 0); + } + return $this; + } + + /** + * {@inheritdoc} + */ + public function setSerializedValue($value) { + assert('is_string($value)'); $this->createCondition('value', $value); + $this->select->condition('serialized', 1); return $this; } diff --git a/core/modules/user/src/UserDataQueryInterface.php b/core/modules/user/src/UserDataQueryInterface.php index 9fa8daa..e825ed5 100644 --- a/core/modules/user/src/UserDataQueryInterface.php +++ b/core/modules/user/src/UserDataQueryInterface.php @@ -63,6 +63,19 @@ public function setName($name); public function setValue($value); /** + * Sets the value stored and the serialized condition. + * + * @param string $value + * The value stored. + * + * @return $this + * + * @throws \Drupal\user\Exception\ExistingConditionException + * Exception thrown when the select object already has a value condition. + */ + public function setSerializedValue($value); + + /** * Runs the query. * * @return \Traversable diff --git a/core/modules/user/tests/src/Kernel/UserDataQueryTest.php b/core/modules/user/tests/src/Kernel/UserDataQueryTest.php index e19aa32..cec1dd9 100644 --- a/core/modules/user/tests/src/Kernel/UserDataQueryTest.php +++ b/core/modules/user/tests/src/Kernel/UserDataQueryTest.php @@ -145,6 +145,7 @@ public function testEmpty() { /** * @covers ::setValue + * @covers ::setSerializedValue * @covers ::execute */ public function testSetValue() { @@ -183,6 +184,10 @@ public function testSetValue() { // Test getting a pre-serialized value. $user_data_query = $user_data->getUserDataQuery(); $user_data_query->setValue(serialize(['bar'])); + $this->assertEmpty(iterator_to_array($user_data_query->execute())); + + $user_data_query = $user_data->getUserDataQuery(); + $user_data_query->setSerializedValue(serialize(['bar'])); $this->assertEquals([(object) $contents[2]], iterator_to_array($user_data_query->execute())); }