Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
thisanimus opened this issue Apr 26, 2018 · 34 comments
Closed

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

thisanimus opened this issue Apr 26, 2018 · 34 comments
Labels
authoring ✍️ features related to author experience enhancement improvements to existing features

Comments

@thisanimus
Copy link

thisanimus commented Apr 26, 2018

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:
#859
#987

@jsunsawyer
Copy link

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.

@thisanimus
Copy link
Author

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.

@brandonkelly brandonkelly added enhancement improvements to existing features authoring ✍️ features related to author experience labels Apr 26, 2018
@KatieMFritz
Copy link
Contributor

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

@RitterKnightCreative
Copy link

+1 for this.

@paul-frost
Copy link

+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.

@pixeldeluxe
Copy link

+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!

@thisanimus
Copy link
Author

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?

@brandonkelly
Copy link
Member

brandonkelly commented Sep 18, 2018

@thisanimus I can’t 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>'
    ];
});

@thisanimus
Copy link
Author

beautiful, thanks!

@benface
Copy link
Contributor

benface commented Dec 14, 2018

Very odd that this isn't built in yet!

@brandonkelly
Copy link
Member

We only have time for 10 things at a time!

@thisanimus
Copy link
Author

@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.

@KatieMFritz
Copy link
Contributor

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?

@brandonkelly
Copy link
Member

@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>'
        ];
    }
});

@KatieMFritz
Copy link
Contributor

@brandonkelly Thank you! @angrybrad just helped me and I was writing my own answer to the question. 😆

@jonlongnecker
Copy link

+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.

@littlered
Copy link

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

@brandonkelly
Copy link
Member

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

@xaddict
Copy link

xaddict commented Jun 6, 2019

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?

@brandonkelly
Copy link
Member

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

@xaddict
Copy link

xaddict commented Jun 7, 2019

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?

@craftcms craftcms deleted a comment from xaddict Jun 7, 2019
@brandonkelly
Copy link
Member

brandonkelly commented Jun 7, 2019

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

@xaddict
Copy link

xaddict commented Jun 27, 2019

@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.

@brandonkelly
Copy link
Member

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

@KatieMFritz
Copy link
Contributor

beautiful

@mdunbavan
Copy link

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

@brandonkelly
Copy link
Member

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

@mdunbavan
Copy link

@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

@brandonkelly
Copy link
Member

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

@mdunbavan
Copy link

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

@brandonkelly
Copy link
Member

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

@simonkuran
Copy link

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?

@brandonkelly
Copy link
Member

To be clear, it wouldn’t 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’s output setting would be:

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

@simonkuran
Copy link

That seems like a solid workaround, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
authoring ✍️ features related to author experience enhancement improvements to existing features
Projects
None yet
Development

No branches or pull requests