Detect currently selected source of custom elements I have a split-style + New Element button for my custom element type. Thanks to this previous thread, the right-side dropdown menu is working perfectly!



My question now is, how do I get the + New Element side of it working?

I keep studying the entries templates, to see how it's done over there. But I've noticed that when you navigate between entry sources, the page URL actually changes. When navigating between my custom element sources, the page URL does not change.

How can I help the + New Element button detect what source is currently being viewed?
You can see how this is working by opening craft/app/resources/js/craft.js and searching for the Craft.EntryIndex class definition.

Craft.EntryIndex is an extension of Craft.BaseElementIndex, and is used to govern all Entry indexes in Craft (including Entry selection modals). We pull that off with this line of code, which you’ll find immediately after the class definition:



Craft.registerElementIndexClass('Entry', Craft.EntryIndex);


The logic governing the “New Entry” button is a little complicated, and it relies on an initial version of the button HTML already being available on the page (thanks to the craft/app/templates/entries/_edit.html template), but all the magic happens within its onSelectSource() method, including updating the URL.