From eb8ea3abbd2bb5016a35a8e2d5fb390da4e2f6a0 Mon Sep 17 00:00:00 2001 From: brandonkelly Date: Tue, 10 Sep 2024 06:06:46 -0700 Subject: [PATCH] =?UTF-8?q?Include=20=E2=80=9CCountry=E2=80=9D=20columns?= =?UTF-8?q?=20in=20address=20indexes?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG-WIP.md | 1 + src/elements/Address.php | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+) diff --git a/CHANGELOG-WIP.md b/CHANGELOG-WIP.md index 9132f6093c9..750ffc186ca 100644 --- a/CHANGELOG-WIP.md +++ b/CHANGELOG-WIP.md @@ -3,6 +3,7 @@ ### Content Management - Improved the styling of element cards with thumbnails. ([#15692](https://github.com/craftcms/cms/pull/15692), [#15673](https://github.com/craftcms/cms/issues/15673)) - Elements within element selection inputs now have “Replace” actions. +- Address index tables can now include “Country” columns. ### Administration - All relation fields can now be selected as field layouts’ thumbnail providers. ([#15651](https://github.com/craftcms/cms/discussions/15651)) diff --git a/src/elements/Address.php b/src/elements/Address.php index a7a7600e0e2..79965754f05 100644 --- a/src/elements/Address.php +++ b/src/elements/Address.php @@ -106,6 +106,62 @@ public static function createCondition(): ElementConditionInterface return Craft::createObject(AddressCondition::class, [static::class]); } + /** + * @inheritdoc + */ + protected static function defineTableAttributes(): array + { + return array_merge(parent::defineTableAttributes(), [ + 'country' => ['label' => Craft::t('app', 'Country')], + ]); + } + + /** + * @inheritdoc + */ + protected function attributeHtml(string $attribute): string + { + switch ($attribute) { + case 'country': + return $this->getCountry()->getName(); + default: + return parent::attributeHtml($attribute); + } + } + + /** + * @inheritdoc + */ + protected static function defineSortOptions(): array + { + return [ + [ + 'label' => Craft::t('app', 'Label'), + 'orderBy' => 'title', + 'attribute' => 'title', + ], + [ + 'label' => Craft::t('app', 'Country'), + 'orderBy' => 'countryCode', + 'attribute' => 'country', + ], + [ + 'label' => Craft::t('app', 'Date Created'), + 'orderBy' => 'dateCreated', + 'defaultDir' => 'desc', + ], + [ + 'label' => Craft::t('app', 'Date Updated'), + 'orderBy' => 'dateUpdated', + 'defaultDir' => 'desc', + ], + [ + 'label' => Craft::t('app', 'ID'), + 'orderBy' => 'id', + ], + ]; + } + /** * @inheritdoc * @return AddressQuery The newly created [[AddressQuery]] instance.