Cms: Feature Request: CP Sort by Custom Field - 3.X

Created on 26 Apr 2018  路  34Comments  路  Source: craftcms/cms

It would be nice to be able to sort entities by their custom fields. This issue has been raised in Craft 2.5, but I did not see that anyone had mentioned it for 3.X.

Related Issues in 2.5:
https://github.com/craftcms/cms/issues/859
https://github.com/craftcms/cms/issues/987

authoring enhancement

Most helpful comment

@thisanimus I can鈥檛 give you an ETA or anything at this time. You can pull it off with a module/plugin like this:

use craft\base\Element;
use craft\elements\Entry;
use craft\events\RegisterElementSortOptionsEvent;
use yii\base\Event;

Event::on(Entry::class, Element::EVENT_REGISTER_SORT_OPTIONS, function(RegisterElementSortOptionsEvent $event) {
    $event->sortOptions[] = [
        'label' => '<FieldName>',
        'orderBy' => 'field_<FieldHandle>',
        'attribute' => 'field:<FieldID>'
    ];
});

All 34 comments

I previously used https://github.com/mmikkel/CpSortableCustomColumns-Craft to achieve this in Craft 2. Really feel that this should be built into Craft though.

From a content authoring standpoint, Craft is basically a magical unicorn. Everything else is so easy and customizable that it makes this one issue stick out like a sore thumb.

Plus one! A plugin would work too, but I can't see NOT wanting this feature in any instance.

+1 for this.

+1
I need this feature on every site I build. The plugin for 2.5 was part of my standard instal so I forgot it was a plugin. I've just started working on my first Craft 3 site and am stuck.
Any site that has events is going to have a custom date field, and being able to list them in date order in the CP is very helpfull for editors.
I don't want to commision a custom plugin as that could be a problem going forward with maintaining it and I don't have the time of skill to build it myself.

+1
There are a lot of times when I want certain cols to be sorted. Thanks in advance and keep up the good work, you guys!

I'm holding a site back from prod because of this.

  • Is this something that will be incorporated into Craft soon?
  • Would it be better for me to develop a plugin to do this?

@thisanimus I can鈥檛 give you an ETA or anything at this time. You can pull it off with a module/plugin like this:

use craft\base\Element;
use craft\elements\Entry;
use craft\events\RegisterElementSortOptionsEvent;
use yii\base\Event;

Event::on(Entry::class, Element::EVENT_REGISTER_SORT_OPTIONS, function(RegisterElementSortOptionsEvent $event) {
    $event->sortOptions[] = [
        'label' => '<FieldName>',
        'orderBy' => 'field_<FieldHandle>',
        'attribute' => 'field:<FieldID>'
    ];
});

beautiful, thanks!

Very odd that this isn't built in yet!

We only have time for 10 things at a time!

@brandonkelly Craft is such an amazing CMS, I really appreciate all of the thought and work y'all have put into this product. It's really head and shoulders above any other open source CMS I've ever used. Keep up the good work, and thanks for listening to us squeaky wheels.

Hi @brandonkelly - thanks for the code snippet! I was able to add sort functionality for some custom fields in Entries.

I want to do the same for Assets. Can I do that in the same module, or do I need a separate module?

@KatieMFritz You could use the same module, either with a second event listener (for craft\elements\Asset instead than craft\elements\Entry) or if you want the _exact same_ sort options added for assets, a single event listener on craft\base\Element would be easier:

use craft\base\Element;
use craft\elements\Asset;
use craft\elements\Entry;
use craft\events\RegisterElementSortOptionsEvent;
use yii\base\Event;

Event::on(Element::class, Element::EVENT_REGISTER_SORT_OPTIONS, function(RegisterElementSortOptionsEvent $event) {
    if ($event->sender instanceof Asset || $event->sender instanceof Entry) {
        $event->sortOptions[] = [
            'label' => '<FieldName>',
            'orderBy' => 'field_<FieldHandle>',
            'attribute' => 'field:<FieldID>'
        ];
    }
});

@brandonkelly Thank you! @angrybrad _just_ helped me and I was writing my own answer to the question. 馃槅

+1 from me as well. Had to create an events section, and because of the weird way that Craft's default entry date field worked, it made more sense to create a separate start date field. But now I can't sort any of the events entries by that custom start date in the control panel! For everyone's sanity I'd love to be able to click and sort by that field.

+1. This would make an awesome CP even awesomer!!

The next Craft 3.2 Alpha release will give custom fields the ability to opt into being sort options, by implementing a new SortableFieldInterface.

If I want to add this functionality right now and I want to sort by related fields, what would I fill in under orderBy and attribute?

@xaddict The change is just for the CP, and relational fields don鈥檛 even support it. Not really an easy way to sort elements by their related elements. Definitely more complicated than just a custom orderBy value.

Okay.
I could see a way where orderBy is a function or something to take care of that.
Will the 3.2 Stable release support sorting for one dimensional fields out of the box?

@xaddict You can already sort by most fields using .orderBy('fieldHandle ASC') (or DESC).

@brandonkelly I meant in the backend for default views like the entries list. Being able to sort/filter by anything is a real nice addition to any CMS. Craft is already way ahead of some other systems, but this would be a top feature for a lot of my clients.

@xaddict that鈥檚 what this feature request is all about. Yes, implemented in 3.2.

beautiful

Hey has this been implemented for Craft Commerce 2 @brandonkelly ?

@mdunbavan It鈥檚 been added for all element types that have index pages, including Commerce products & orders.

@brandonkelly this doesn't show up when I am on the index page for commerce. If I hit the cog icon at the bottom of the product types on the left and look for a custom field it isn't there

Only custom fields that implement PreviewableFieldinterface can be added to the table; and only fields that implement SortableFieldInterface will be listed as sort options.

Ah right so things like categories wouldn't be possible then?

Relational fields (including Categories) can be added as table columns, but they can鈥檛 be sorted.

Any updates on sorting by a relation? It would be really nice to be able to sort by the title of the first entry in an Entries field.

Is there a straightforward way to build this in a plugin?

To be clear, it wouldn鈥檛 be possible to sort by relation fields, as that would result in a bunch of duplicate results in the element index.

What you could do is use a Preparse field to store the title of the related element(s), and then show & sort that column instead of the actual relation field.

The field鈥檚 output setting would be:

{{ element.myRelationField.all()|column('title')|join(', ') }}

That seems like a solid workaround, thanks.

Was this page helpful?
0 / 5 - 0 ratings