I would like to add a custom field for order by on the drop down menu on admin CP. How can I do it, is there any event for this in Craft 3?

It is a plugin related question, we are using custom plugin to customize our application


You can take a look at this question Set custom sort order for index pages in the CP? for Craft 2 and see updating Plugins for Craft 3 in the docs

// Old:
public function modifyEntrySortableAttributes(&$attributes)
    $attributes['id'] = Craft::t('ID');

// New:
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['id'] = \Craft::t('app', 'ID');

Keep in mind your field must be sortable via SQL

  • In this way, this sort option type will be applied to all the sections. What if some of the sections don't have this sort option type?
    – yu xia
    Jun 20 '18 at 2:50
  • @yuxia $event->sender contains the element so you can check it's section and entry type Jun 21 '18 at 8:08
  • Unfortunately, for me it's not working. I dd out the $event: ` RegisterElementSortOptionsEvent {#4331 ▼ +sortOptions: array:8 [▶] +name: "registerSortOptions" +sender: null +handled: false +data: null } `
    – yu xia
    Jun 22 '18 at 0:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.