diff --git a/CHANGELOG.md b/CHANGELOG.md index 6622e9274a1..2b5ad270981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - Fixed a bug where tabs within field layout designers weren’t always getting positioned correctly when wrapped. ([#15590](https://github.com/craftcms/cms/issues/15590)) - Fixed a bug where editable table rows’ action buttons were misaligned for newly-created rows. ([#15602](https://github.com/craftcms/cms/issues/15602)) - Fixed a bug where relational fields’ element query results weren’t limited to the selected relations if the `id` param was overridden. ([#15570](https://github.com/craftcms/cms/issues/15570)) +- Fixed a bug where ordering element queries by textual custom fields would factor in character marks. ([#15609](https://github.com/craftcms/cms/issues/15609)) ## 5.3.6 - 2024-08-26 diff --git a/src/base/Field.php b/src/base/Field.php index 591207913f0..9392459f5f8 100644 --- a/src/base/Field.php +++ b/src/base/Field.php @@ -773,15 +773,25 @@ public function getPreviewHtml(mixed $value, ElementInterface $element): string */ public function getSortOption(): array { - if (static::dbType() === null || !isset($this->layoutElement)) { + $dbType = static::dbType(); + if ($dbType === null || !isset($this->layoutElement)) { throw new NotSupportedException('getSortOption() not supported by ' . $this->name); } + $orderBy = $this->getValueSql(); + + // for mysql, we have to make sure text column type is cast to char, otherwise it won't be sorted correctly + // see https://github.com/craftcms/cms/issues/15609 + $db = Craft::$app->getDb(); + if ($db->getIsMysql() && Db::parseColumnType($dbType) === Schema::TYPE_TEXT) { + $orderBy = "CAST($orderBy AS CHAR(255))"; + } + // The attribute name should match the table attribute name, // per ElementSources::getTableAttributesForFieldLayouts() return [ 'label' => Craft::t('site', $this->name), - 'orderBy' => $this->getValueSql(), + 'orderBy' => $orderBy, 'attribute' => isset($this->layoutElement->handle) ? "fieldInstance:{$this->layoutElement->uid}" : "field:$this->uid",