I'm currently writing a plugin enabling users to easily add custom fields to index tables, using the native field layout designer.
My current goal is to have any datetime, boolean, string or number fields added to a source's FieldLayout, also automatically be added to the relevant index table's sortable attributes. Obviously, I've met a stumbling block with modifyEntrySortableAttributes.
Everything works great on initial pageload (I've found a way to work around the fact that modifyEntrySortableAttributes doesn't have a clue about the source).
My problem is that when navigating between sources, Craft uses AJAX, and the sortable attributes are not updated. Through logging I've confirmed that the modifyEntrySortableAttributes hook does run (see footnote!) every time the source changes, and I also know that my fix for identifying the current source also works (i.e. the plugin fetches the correct FieldLayout whenever the source changes).
So – as far as I can tell – I'm basically stuck with a CP frontend issue, which is that the ".sortmenubtn" button doesn't refresh its ".menu" container contents when the source changes. I'd be happy to refresh the menu myself through some JS in my plugin, but I've looked at the AJAX calls and can't spot any data related to the sortable attributes.
My backup solution is to create my own AJAX service to fetch the updated attributes, but it would be really cool to do this with a minimal amount of hacks (the plugin already contains a few...).
Would greatly appreciate any ideas for solving this!
- Footnote: It's actually weird that the hook runs when the source changes, as this directly contradicts the following statement in the docs:
Note that this method will only get called once for the entire index; not each time that a new source is selected.