Skip to content

Commit

Permalink
Sort dates in descending order by default
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonkelly committed Sep 4, 2020
1 parent f4e5e19 commit 291cbdf
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 8 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- The `craft\behaviors\CustomFieldBehavior` class is now saved to a file with a name based on the current field version, avoiding OPcache issues.
- Asset filenames are now automatically shortened if they are longer than 255 characters. ([#6766](https://github.com/craftcms/cms/issues/6766))
- Improved the style of up/down/right/left angles in the control panel.
- Built-in element types’ date sort options are now sorted in descending order by default when selected. ([#1153](https://github.com/craftcms/cms/issues/1153))
- It’s now possible for element sort options returned by `craft\base\ElementInterface::sortOptions()` and `craft\base\Element::defineSortOptions()` to include `defaultDir` keys, which specify the default sort direction that should be used when the option is selected. ([#1153](https://github.com/craftcms/cms/issues/1153))

### Fixed
- Fixed a bug where `update` commands weren’t working if there was no `config/license.key` file yet. ([#6777](https://github.com/craftcms/cms/issues/6777))
Expand Down
3 changes: 3 additions & 0 deletions src/base/ElementInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -377,13 +377,16 @@ public static function indexHtml(ElementQueryInterface $elementQuery, array $dis
* - `orderBy` – An array or comma-delimited string of columns to order the query by
* - `attribute` _(optional)_ – The [[tableAttributes()|table attribute]] name that this option is associated
* with (required if `orderBy` is an array or more than one column name)
* - `defaultDir` _(optional)_ – The default sort direction that should be used when sorting by this option
* (set to either `asc` or `desc`). Defaults to `asc` if not specified.
*
* ```php
* return [
* [
* 'label' => Craft::t('app', 'Attribute Label'),
* 'orderBy' => 'columnName',
* 'attribute' => 'attributeName',
* 'defaultDir' => 'asc',
* ],
* ];
* ```
Expand Down
8 changes: 7 additions & 1 deletion src/elements/Asset.php
Original file line number Diff line number Diff line change
Expand Up @@ -387,16 +387,22 @@ protected static function defineSortOptions(): array
'title' => Craft::t('app', 'Title'),
'filename' => Craft::t('app', 'Filename'),
'size' => Craft::t('app', 'File Size'),
'dateModified' => Craft::t('app', 'File Modification Date'),
[
'label' => Craft::t('app', 'File Modification Date'),
'orderBy' => 'dateModified',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Uploaded'),
'orderBy' => 'elements.dateCreated',
'attribute' => 'dateCreated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Updated'),
'orderBy' => 'elements.dateUpdated',
'attribute' => 'dateUpdated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'ID'),
Expand Down
2 changes: 2 additions & 0 deletions src/elements/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,11 +308,13 @@ protected static function defineSortOptions(): array
'label' => Craft::t('app', 'Date Created'),
'orderBy' => 'elements.dateCreated',
'attribute' => 'dateCreated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Updated'),
'orderBy' => 'elements.dateUpdated',
'attribute' => 'dateUpdated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'ID'),
Expand Down
14 changes: 12 additions & 2 deletions src/elements/Entry.php
Original file line number Diff line number Diff line change
Expand Up @@ -462,17 +462,27 @@ protected static function defineSortOptions(): array
'title' => Craft::t('app', 'Title'),
'slug' => Craft::t('app', 'Slug'),
'uri' => Craft::t('app', 'URI'),
'postDate' => Craft::t('app', 'Post Date'),
'expiryDate' => Craft::t('app', 'Expiry Date'),
[
'label' => Craft::t('app', 'Post Date'),
'orderBy' => 'postDate',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Expiry Date'),
'orderBy' => 'expiryDate',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Created'),
'orderBy' => 'elements.dateCreated',
'attribute' => 'dateCreated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Updated'),
'orderBy' => 'elements.dateUpdated',
'attribute' => 'dateUpdated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'ID'),
Expand Down
16 changes: 14 additions & 2 deletions src/elements/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,22 @@ protected static function defineSortOptions(): array
'email' => Craft::t('app', 'Email'),
'firstName' => Craft::t('app', 'First Name'),
'lastName' => Craft::t('app', 'Last Name'),
'lastLoginDate' => Craft::t('app', 'Last Login'),
[
'label' => Craft::t('app', 'Last Login'),
'orderBy' => 'lastLoginDate',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Created'),
'orderBy' => 'elements.dateCreated',
'attribute' => 'dateCreated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Updated'),
'orderBy' => 'elements.dateUpdated',
'attribute' => 'dateUpdated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'ID'),
Expand All @@ -294,16 +300,22 @@ protected static function defineSortOptions(): array
'firstName' => Craft::t('app', 'First Name'),
'lastName' => Craft::t('app', 'Last Name'),
'email' => Craft::t('app', 'Email'),
'lastLoginDate' => Craft::t('app', 'Last Login'),
[
'label' => Craft::t('app', 'Last Login'),
'orderBy' => 'lastLoginDate',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Created'),
'orderBy' => 'elements.dateCreated',
'attribute' => 'dateCreated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'Date Updated'),
'orderBy' => 'elements.dateUpdated',
'attribute' => 'dateUpdated',
'defaultDir' => 'desc',
],
[
'label' => Craft::t('app', 'ID'),
Expand Down
1 change: 1 addition & 0 deletions src/templates/_elements/toolbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
text: sortOptionLabel(sortOption),
data: {
attr: sortOption.attribute ?? sortOption.orderBy ?? key,
'default-dir': sortOption.defaultDir ?? false,
}
}) }}
</li>
Expand Down
2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/js/Craft.min.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/web/assets/cp/dist/js/Craft.min.js.map

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion src/web/assets/cp/src/js/BaseElementIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,11 @@ Craft.BaseElementIndex = Garnish.Base.extend(
this.$sortMenuBtn.attr('title', Craft.t('app', 'Sort by {attribute}', {attribute: label}));
this.$sortMenuBtn.text(label);

this.setSortDirection(attr === 'score' ? 'desc' : 'asc');
if (attr === 'score') {
this.setSortDirection('desc');
} else {
this.setSortDirection($option.data('default-dir') || 'asc');
}

if (attr === 'structure') {
this.$sortDirectionsList.find('a').addClass('disabled');
Expand Down

0 comments on commit 291cbdf

Please sign in to comment.