I tried to override the sort options for entries in the control panel but I was not able to remove the custom fields from the sorting list.
Event::on(
Entry::class,
Element::EVENT_REGISTER_SORT_OPTIONS,
function(RegisterElementSortOptionsEvent $event) {
$event->sortOptions = [
'id' => Craft::t('app', 'ID'),
'title' => Craft::t('app', 'Title'),
];
}
);
You'll see the ID and Title at the top of the list but custom fields will be there also.
That’s expected, because the element type isn’t responsible for including sortable custom fields in the first place.
Only field types that say they are sortable will be included though. What’s your use case for wanting to remove one?
I added a couple of date fields to an entry type. However, I don't want them to appear on the sorting list. Using the above code, I was able to hide the default ones except id and title, but the additional date fields are still there in the list.
Right, the element type _is_ responsible for defining any sortable attributes that it actually knows about. But not custom fields.
Is there any way to hide those custom fields?
Unfortunately not, and I’m not sure how we could make it possible currently.
Just found a way to make this possible. As of the next release you will be able to do this from a module:
use craft\events\DefineSourceSortOptionEvent;
use craft\services\ElementIndexes;
use yii\base\Event;
$fieldId = Craft::$app->fields->getFieldByHandle('myFieldHandle')->id;
Event::on(
ElementIndexes::class,
ElementIndexes::EVENT_DEFINE_SOURCE_SORT_OPTION,
function (DefineSourceSortOptionEvent $event) use ($fieldId) {
if ($event->sortOption['attribute'] === "field:$fieldId") {
$event->sortOption = null;
}
}
);
DefineSourceSortOptionEvent also has $elementType and $sourceKey properties, if those are relevant to your needs.
To get the fix early, change your craftcms/cms requirement in composer.json to "dev-develop as 3.6.4.1" and run composer update.
Thanks a lot!
I suggest to rename the $sourceKey property to $source for consistency with other events.
Using the following code, I was able to hide all custom fields from sort options:
Event::on(
ElementIndexes::class,
ElementIndexes::EVENT_DEFINE_SOURCE_SORT_OPTION,
function (DefineSourceSortOptionEvent $event) {
$event->sortOption = null;
}
);
Another related issue:
I used RegisterElementTableAttributesEvent to add custom attributes and then used RegisterElementSortOptionsEvent to make those attributes sortable.
However, I could not apply those changes to specific sources. Is that possible?
I suggest to rename the
$sourceKeyproperty to$sourcefor consistency with other events.
Doh, good catch. Just fixed that.
I used
RegisterElementTableAttributesEventto add custom attributes and then usedRegisterElementSortOptionsEventto make those attributes sortable.
However, I could not apply those changes to specific sources. Is that possible?
Is now! Just took a different approach with that event. It’s now EVENT_DEFINE_SOURCE_SORT_OPTIONS (plural) rather than EVENT_DEFINE_SOURCE_SORT_OPTION, and I’ve also added a new EVENT_DEFINE_SOURCE_TABLE_ATTRIBUTES event for defining source-specific table attributes.
use craft\events\DefineSourceSortOptionsEvent;
use craft\events\DefineSourceTableAttributesEvent;
use craft\services\ElementIndexes;
use yii\base\Event;
Event::on(
ElementIndexes::class,
ElementIndexes::EVENT_DEFINE_SOURCE_TABLE_ATTRIBUTES,
function (DefineSourceTableAttributesEvent $event) {
// Add custom attributes
$event->attributes['myAttribute'] = ['label' => 'My Attribute'];
// ...
}
);
Event::on(
ElementIndexes::class,
ElementIndexes::EVENT_DEFINE_SOURCE_SORT_OPTIONS,
function (DefineSourceSortOptionsEvent $event) {
// Remove all custom fields
$event->sortOptions = array_filter($event->sortOptions, function($sortOption) {
return strpos($sortOption['attribute'], 'field:') !== 0;
});
// Add custom sort options
$event->sortOptions[] = [
'label' => 'My Attribute',
'orderBy' => 'myAttribute',
'attribute' => 'myAttribute',
];
// ...
}
);
That's great!
A couple of observations though:
First, the customize window in element indexes does not list the attributes defined in DefineSourceTableAttributesEvent
Second, is it possible to not repeat the attribute label by setting the sortOption in a way like this:
$event->sortOptions['attribute'] = ['orderBy' => 'myAttribute' ];
similar to DefineSourceTableAttributesEvent and other events.
First, the customize window in element indexes does not list the attributes defined in
DefineSourceTableAttributesEvent
Doh, sorry about that. Fixed now (f8fa5c7aaa89f5c88044906adfc14ae6a1d2e5e1).
Second, is it possible to not repeat the attribute label by setting the sortOption in a way like this:
$event->sortOptions['attribute'] = ['orderBy' => 'myAttribute' ];similar to
DefineSourceTableAttributesEventand other events.
No, because they are not necessarily related. They both reference element attributes, but you can have sort options that aren’t available as table attributes, and vise-versa.
DefineSourceTableAttributesEvent does require to to specify a label.
Craft 3.6.5 is out now with those new events ✨