Skip to content

Commit

Permalink
Sortable custom fields
Browse files Browse the repository at this point in the history
Resolves #2818
  • Loading branch information
brandonkelly committed May 29, 2019
1 parent b337b36 commit 5325221
Show file tree
Hide file tree
Showing 10 changed files with 73 additions and 5 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG-v3.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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`.
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG-v3.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 8 additions & 0 deletions src/base/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions src/base/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
29 changes: 29 additions & 0 deletions src/base/SortableFieldInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/

namespace craft\base;

/**
* SortableFieldInterface defines the common interface to be implemented by field classes that can be available as
* sort options on element indexes.
*
* @author Pixel & Tonic, Inc. <[email protected]>
* @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;
}
3 changes: 2 additions & 1 deletion src/fields/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +26,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class Date extends Field implements PreviewableFieldInterface
class Date extends Field implements PreviewableFieldInterface, SortableFieldInterface
{
// Static
// =========================================================================
Expand Down
3 changes: 2 additions & 1 deletion src/fields/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\base\ElementInterface;
use craft\base\SortableFieldInterface;
use craft\fields\data\SingleOptionFieldData;

/**
Expand All @@ -17,7 +18,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class Dropdown extends BaseOptionsField
class Dropdown extends BaseOptionsField implements SortableFieldInterface
{
// Static
// =========================================================================
Expand Down
3 changes: 2 additions & 1 deletion src/fields/Lightswitch.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use craft\base\ElementInterface;
use craft\base\Field;
use craft\base\PreviewableFieldInterface;
use craft\base\SortableFieldInterface;
use yii\db\Schema;

/**
Expand All @@ -19,7 +20,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class Lightswitch extends Field implements PreviewableFieldInterface
class Lightswitch extends Field implements PreviewableFieldInterface, SortableFieldInterface
{
// Static
// =========================================================================
Expand Down
3 changes: 2 additions & 1 deletion src/fields/Number.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -21,7 +22,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class Number extends Field implements PreviewableFieldInterface
class Number extends Field implements PreviewableFieldInterface, SortableFieldInterface
{
// Static
// =========================================================================
Expand Down
3 changes: 2 additions & 1 deletion src/fields/RadioButtons.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

use Craft;
use craft\base\ElementInterface;
use craft\base\SortableFieldInterface;
use craft\fields\data\SingleOptionFieldData;

/**
Expand All @@ -17,7 +18,7 @@
* @author Pixel & Tonic, Inc. <[email protected]>
* @since 3.0
*/
class RadioButtons extends BaseOptionsField
class RadioButtons extends BaseOptionsField implements SortableFieldInterface
{
// Static
// =========================================================================
Expand Down

0 comments on commit 5325221

Please sign in to comment.