1

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!

Note that this method will only get called once for the entire index; not each time that a new source is selected.

2

The modifyEntrySortableAttributes technically gets called each time you load new elements on the index page, just so that the controller can confirm that the posted order attribute is valid. But the only time that your plugin can actually affect what is shown in the dropdown is the first time it’s called, on page load.

Maybe a better way to go about this would be to have your plugin return all the available options up front, and then simply hide/show them with JavaScript when the source changes.

| improve this answer | |
  • Brandon, could you please make such a script part of the core and add a new source parameter to modifyEntrySortableAttributes? – carlcs Mar 18 '15 at 11:23
  • Thank you for the reply, Brandon. I actually already got far in implementing an AJAX service which returns the modified sortable attributes whenever the source changes – still haven't figured out how to use that data to actually change the contents of the sortMenu in Craft.EntryIndex (if its even possible). Your suggestion is a very viable approach, though – I'm a bit concerned about performance with pulling all the layout models and attributes at once, but I guess most sites will have sources in the single or double digits, which should be fine. Back to the drawing board, I guess! – Mats Mikkel Rummelhoff Mar 18 '15 at 11:41
  • @MatsMikkelRummelhoff I added a feature request to have this whole functionality available as a core feature. Maybe you can contribute? – carlcs Mar 20 '15 at 17:13
  • @carlcs Sweet, voted and commented. I wouldn't hold my breath, though my plugin will have a working, albeit hacked-together, implementation of this sometime soon. Jury is still out on the final approach, but I'm pretty close and will get it sorted (pun intended). – Mats Mikkel Rummelhoff Mar 20 '15 at 20:16
  • Shameless plug: Just pushed DashCols 1.2 which implements sorting for most columns, by source. Went with Brandon's suggested approach. github.com/mmikkel/DashCols-Craft – Mats Mikkel Rummelhoff Apr 6 '15 at 1:04

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.