Jump to content
Hurme

Template order in backend

Recommended Posts

Heya,
Is there any way to affect the order of templates in the backend, specifically when you create a new page and select the template it uses? It would be useful to have the most used template at top of the list as an example.

Share this post


Link to post
Share on other sites

One way would be to give them labels sorted in alphabetical order. Something like:

  • 01 - Most used template (a label for most_used_template)
  • 02 - Not so much used template (a label for not_so_much_used_template)

Share this post


Link to post
Share on other sites

Or a hook like this? (not tested much).

in site/ready.php

$wire->addHookAfter('ProcessPageAdd::getAllowedTemplates', function (HookEvent $event) {
    $allowedTemplatesArray = $event->return;// normal PHP Array
    $allowedTemplatesWireArray = new WireArray();// create new WireArray() for sorting convenience
    $allowedTemplatesWireArray->setArray($allowedTemplatesArray);// populate the WireArray with data
    // whatever sort you want -> id, name, or multiple props, etc
    // maybe even a $int = $template->getNumPages(); ? PERFORMANCE ISSUES? (would require looping through all templates)
    $allowedTemplatesWireArray->sort('-id'); // sorting by ID DESC
    $allowedTemplatesModifiedArray = $allowedTemplatesWireArray->getArray();// get PHP Array back from sorted/modified WireArray
    $event->return = $allowedTemplatesModifiedArray;// give it back for passing to ProcessPageAdd -> buildForm()
});

You could also use PHP's sort functions to sort the $allowedTemplatesArray but it is simpler to user WireArray sort instead 😉.

Edited by kongondo
  • Like 3

Share this post


Link to post
Share on other sites

Thanks to you both,
The numbering option was something I thought about too, but it seems bit clunky in the end.

I'll take a look at your hook next week, thanks for that!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...