diff --git a/core/lib/Drupal/Core/Config/Entity/Query/Condition.php b/core/lib/Drupal/Core/Config/Entity/Query/Condition.php index bb20fbe..e6d2d52 100644 --- a/core/lib/Drupal/Core/Config/Entity/Query/Condition.php +++ b/core/lib/Drupal/Core/Config/Entity/Query/Condition.php @@ -7,6 +7,7 @@ namespace Drupal\Core\Config\Entity\Query; +use Drupal\Component\Utility\Unicode; use Drupal\Core\Entity\Query\ConditionBase; use Drupal\Core\Entity\Query\ConditionInterface; use Drupal\Core\Entity\Query\QueryException; @@ -33,6 +34,15 @@ public function compile($configs) { if (!isset($condition['operator'])) { $condition['operator'] = is_array($condition['value']) ? 'IN' : '='; } + + // Lowercase condition value(s) for case-insensitive matches. + if (is_array($condition['value'])) { + $condition['value'] = array_map('Drupal\Component\Utility\Unicode::strtolower', $condition['value']); + } + elseif (!is_bool($condition['value'])) { + $condition['value'] = Unicode::strtolower($condition['value']); + } + $single_conditions[] = $condition; } } @@ -150,6 +160,11 @@ protected function matchArray(array $condition, array $data, array $needs_matchi */ protected function match(array $condition, $value) { if (isset($value)) { + // We always want a case-insensitive match. + if (!is_bool($value)) { + $value = Unicode::strtolower($value); + } + switch ($condition['operator']) { case '=': return $value == $condition['value']; diff --git a/core/lib/Drupal/Core/Entity/Query/QueryInterface.php b/core/lib/Drupal/Core/Entity/Query/QueryInterface.php index d7110ca..5c32717 100644 --- a/core/lib/Drupal/Core/Entity/Query/QueryInterface.php +++ b/core/lib/Drupal/Core/Entity/Query/QueryInterface.php @@ -46,9 +46,9 @@ public function getEntityType(); * @endcode * * @param $value - * The value for $field. In most cases, this is a scalar. For more complex - * options, it is an array. The meaning of each element in the array is - * dependent on $operator. + * The value for $field. In most cases, this is a scalar and it's treated as + * case-insensitive. For more complex options, it is an array. The meaning + * of each element in the array is dependent on $operator. * @param $operator * Possible values: * - '=', '<>', '>', '>=', '<', '<=', 'STARTS_WITH', 'CONTAINS', diff --git a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php index 2205942..ccfc043 100644 --- a/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php +++ b/core/modules/system/lib/Drupal/system/Form/DateFormatFormBase.php @@ -9,6 +9,7 @@ use Drupal\Core\Ajax\AjaxResponse; use Drupal\Core\Ajax\ReplaceCommand; +use Drupal\Core\Config\Entity\ConfigStorageController; use Drupal\Core\Datetime\Date; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Datetime\DrupalDateTime; @@ -42,19 +43,29 @@ protected $dateService; /** + * The date format storage controller. + * + * @var \Drupal\Core\Config\Entity\ConfigStorageController + */ + protected $dateFormatStorage; + + /** * Constructs a new date format form. * * @param \Drupal\Core\Entity\Query\QueryFactory $query_factory * The entity query factory. * @param \Drupal\Core\Datetime\Date $date_service * The date service. + * @param \Drupal\Core\Config\Entity\ConfigStorageController $date_format_storage + * The date format storage controller. */ - public function __construct(QueryFactory $query_factory, Date $date_service) { + public function __construct(QueryFactory $query_factory, Date $date_service, ConfigStorageController $date_format_storage) { $date = new DrupalDateTime(); $this->patternType = $date->canUseIntl() ? DrupalDateTime::INTL : DrupalDateTime::PHP; $this->queryFactory = $query_factory; $this->dateService = $date_service; + $this->dateFormatStorage = $date_format_storage; } /** @@ -63,7 +74,8 @@ public function __construct(QueryFactory $query_factory, Date $date_service) { public static function create(ContainerInterface $container) { return new static( $container->get('entity.query'), - $container->get('date') + $container->get('date'), + $container->get('entity.manager')->getStorageController('date_format') ); } @@ -183,16 +195,12 @@ public function validate(array $form, array &$form_state) { // The machine name field should already check to see if the requested // machine name is available. Regardless of machine_name or human readable // name, check to see if the provided pattern exists. - $format = trim($form_state['values']['date_format_pattern']); - $formats = $this->queryFactory - ->get($this->entity->entityType()) - ->condition('pattern.' . $this->patternType, $format) - ->execute(); - - // Exclude the current format. - unset($formats[$this->entity->id()]); - if (!empty($formats)) { - form_set_error('date_format_pattern', t('This format already exists. Enter a unique format string.')); + $pattern = trim($form_state['values']['date_format_pattern']); + foreach ($this->dateFormatStorage->loadMultiple() as $format) { + if ($format->getPattern() == $pattern) { + form_set_error('date_format_pattern', t('This format already exists. Enter a unique format string.')); + continue; + } } } diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/ConfigEntityQueryTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/ConfigEntityQueryTest.php index 9a84de9..3926f29 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/ConfigEntityQueryTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/ConfigEntityQueryTest.php @@ -110,7 +110,7 @@ protected function setUp() { $array['level1']['level2'] = 3; $entity = entity_create('config_query_test', array( - 'label' => $this->randomName() . '_test_contains_' . $this->randomName(), + 'label' => $this->randomName() . '_TEST_contains_' . $this->randomName(), 'id' => '5', 'number' => 53, 'array' => $array, @@ -462,6 +462,22 @@ protected function testDotted() { } /** + * Tests case sensitivity. + */ + public function testCaseSensitivity() { + // Filter by label with a known containing case-sensitive word. + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'TEST', 'CONTAINS') + ->execute(); + $this->assertResults(array('3', '4', '5')); + + $this->queryResults = $this->factory->get('config_query_test') + ->condition('label', 'test', 'CONTAINS') + ->execute(); + $this->assertResults(array('3', '4', '5')); + } + + /** * Asserts the results as expected regardless of order. * * @param array $expected