diff --git a/CHANGELOG-v3.2.md b/CHANGELOG-v3.2.md index 31f31fd0e8e..ea442400952 100644 --- a/CHANGELOG-v3.2.md +++ b/CHANGELOG-v3.2.md @@ -22,6 +22,7 @@ - Relational fields now have a “Validate related elements” setting, which ensures that the related elements pass validation before the source element can be saved with them selected. ([#4095](https://github.com/craftcms/cms/issues/4095)) - Table fields can now have Dropdown, Email, and URL columns. ([#811](https://github.com/craftcms/cms/issues/811), [#4180](https://github.com/craftcms/cms/pull/4180)) - Dropdown and Multi-select fields can now have optgroups. ([#4236](https://github.com/craftcms/cms/issues/4236)) +- Date/Time, Dropdown, Lightswitch, Number, and Radio Buttons fields are now listed as sort options in element indexes. ([#2818](https://github.com/craftcms/cms/issues/2818)) - Added the `unique` element query param, which can be used to prevent duplicate elements when querying elements across multiple sites. - Added the `preferSites` element query param, which can be used to set the preferred sites that should be used for multi-site element queries, when the `unique` param is also enabled. - Element index pages are now paginated for non-Structure views. ([#818](https://github.com/craftcms/cms/issues/818)) @@ -45,8 +46,10 @@ - Added `craft\base\ElementTrait::$hardDelete`. - Added `craft\base\ElementTrait::$revisionId`. - Added `craft\base\Field::EVENT_AFTER_ELEMENT_PROPAGATE`. +- Added `craft\base\Field::getSortOption()`. - Added `craft\base\FieldInterface::afterElementPropagate()`. - Added `craft\base\FieldInterface::valueType()`. ([#3894](https://github.com/craftcms/cms/issues/3894)) +- Added `craft\base\SortableFieldInterface`, which can be implemented by field classes that should be sortable in element indexes. - Added `craft\behaviors\DraftBehavior`. - Added `craft\behaviors\RevisionBehavior`. - Added `craft\controllers\PreviewController`. diff --git a/CHANGELOG-v3.md b/CHANGELOG-v3.md index 0a33e4b071a..5585e81cc13 100644 --- a/CHANGELOG-v3.md +++ b/CHANGELOG-v3.md @@ -1,5 +1,12 @@ # Release Notes for Craft CMS 3.x +## Unreleased + +### Added +- Date/Time, Dropdown, Lightswitch, Number, and Radio Buttons fields are now listed as sort options in element indexes. ([#2818](https://github.com/craftcms/cms/issues/2818)) +- Added `craft\base\Field::getSortOption()`. +- Added `craft\base\SortableFieldInterface`, which can be implemented by field classes that should be sortable in element indexes. + ## 3.2.0-alpha.7 - 2019-05-28 ### Added diff --git a/src/base/Element.php b/src/base/Element.php index 4715515492d..3fae3424cb6 100644 --- a/src/base/Element.php +++ b/src/base/Element.php @@ -479,6 +479,14 @@ public static function sortOptions(): array { $sortOptions = static::defineSortOptions(); + // Add custom fields to the fix + foreach (Craft::$app->getFields()->getFieldsByElementType(static::class) as $field) { + /** @var Field $field */ + if ($field instanceof SortableFieldInterface) { + $sortOptions[] = $field->getSortOption(); + } + } + // Give plugins a chance to modify them $event = new RegisterElementSortOptionsEvent([ 'sortOptions' => $sortOptions diff --git a/src/base/Field.php b/src/base/Field.php index 260366ba4b2..02b26f24394 100644 --- a/src/base/Field.php +++ b/src/base/Field.php @@ -391,6 +391,22 @@ public function getTableAttributeHtml($value, ElementInterface $element): string return StringHelper::stripHtml($value); } + /** + * Returns the sort option array that should be included in the element’s + * [[\craft\base\ElementInterface::sortOptions()|sortOptions()]] response. + * + * @return array + * @see \craft\base\SortableFieldInterface::getSortOption() + */ + public function getSortOption(): array + { + return [ + 'label' => $this->name, + 'orderBy' => ($this->columnPrefix ?: 'field_') . $this->handle, + 'attribute' => 'field:' . $this->id, + ]; + } + /** * @inheritdoc */ diff --git a/src/base/SortableFieldInterface.php b/src/base/SortableFieldInterface.php new file mode 100644 index 00000000000..325552750e9 --- /dev/null +++ b/src/base/SortableFieldInterface.php @@ -0,0 +1,29 @@ + + * @since 3.2 + */ +interface SortableFieldInterface +{ + // Public Methods + // ========================================================================= + + /** + * Returns the sort option array that should be included in the element’s + * [[\craft\base\ElementInterface::sortOptions()|sortOptions()]] response. + * + * @return array + */ + public function getSortOption(): array; +} diff --git a/src/fields/Date.php b/src/fields/Date.php index 8ad2ea5f88d..ae532bc4829 100644 --- a/src/fields/Date.php +++ b/src/fields/Date.php @@ -11,6 +11,7 @@ use craft\base\ElementInterface; use craft\base\Field; use craft\base\PreviewableFieldInterface; +use craft\base\SortableFieldInterface; use craft\elements\db\ElementQuery; use craft\elements\db\ElementQueryInterface; use craft\helpers\DateTimeHelper; @@ -25,7 +26,7 @@ * @author Pixel & Tonic, Inc. * @since 3.0 */ -class Date extends Field implements PreviewableFieldInterface +class Date extends Field implements PreviewableFieldInterface, SortableFieldInterface { // Static // ========================================================================= diff --git a/src/fields/Dropdown.php b/src/fields/Dropdown.php index 328cdd74896..bec7b9cd1dc 100644 --- a/src/fields/Dropdown.php +++ b/src/fields/Dropdown.php @@ -9,6 +9,7 @@ use Craft; use craft\base\ElementInterface; +use craft\base\SortableFieldInterface; use craft\fields\data\SingleOptionFieldData; /** @@ -17,7 +18,7 @@ * @author Pixel & Tonic, Inc. * @since 3.0 */ -class Dropdown extends BaseOptionsField +class Dropdown extends BaseOptionsField implements SortableFieldInterface { // Static // ========================================================================= diff --git a/src/fields/Lightswitch.php b/src/fields/Lightswitch.php index 06f71361e75..737caa53b21 100644 --- a/src/fields/Lightswitch.php +++ b/src/fields/Lightswitch.php @@ -11,6 +11,7 @@ use craft\base\ElementInterface; use craft\base\Field; use craft\base\PreviewableFieldInterface; +use craft\base\SortableFieldInterface; use yii\db\Schema; /** @@ -19,7 +20,7 @@ * @author Pixel & Tonic, Inc. * @since 3.0 */ -class Lightswitch extends Field implements PreviewableFieldInterface +class Lightswitch extends Field implements PreviewableFieldInterface, SortableFieldInterface { // Static // ========================================================================= diff --git a/src/fields/Number.php b/src/fields/Number.php index 1b88596a345..b57b581b8ab 100644 --- a/src/fields/Number.php +++ b/src/fields/Number.php @@ -11,6 +11,7 @@ use craft\base\ElementInterface; use craft\base\Field; use craft\base\PreviewableFieldInterface; +use craft\base\SortableFieldInterface; use craft\helpers\Db; use craft\helpers\Localization; use craft\i18n\Locale; @@ -21,7 +22,7 @@ * @author Pixel & Tonic, Inc. * @since 3.0 */ -class Number extends Field implements PreviewableFieldInterface +class Number extends Field implements PreviewableFieldInterface, SortableFieldInterface { // Static // ========================================================================= diff --git a/src/fields/RadioButtons.php b/src/fields/RadioButtons.php index 76d6dacc42a..021678c35ab 100644 --- a/src/fields/RadioButtons.php +++ b/src/fields/RadioButtons.php @@ -9,6 +9,7 @@ use Craft; use craft\base\ElementInterface; +use craft\base\SortableFieldInterface; use craft\fields\data\SingleOptionFieldData; /** @@ -17,7 +18,7 @@ * @author Pixel & Tonic, Inc. * @since 3.0 */ -class RadioButtons extends BaseOptionsField +class RadioButtons extends BaseOptionsField implements SortableFieldInterface { // Static // =========================================================================