-
Notifications
You must be signed in to change notification settings - Fork 641
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
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 |
+1 |
I'm holding a site back from prod because of this.
|
@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>'
];
}); |
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 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 |
If I want to add this functionality right now and I want to sort by related fields, what would I fill in under |
@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 |
Okay. |
@xaddict You can already sort by most fields using |
@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’s what this feature request is all about. Yes, implemented in 3.2. |
Hey has this been implemented for Craft Commerce 2 @brandonkelly ? |
@mdunbavan It’s 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’t 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’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(', ') }} |
That seems like a solid workaround, thanks. |
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
The text was updated successfully, but these errors were encountered: