suntrop Posted October 4, 2017 Share Posted October 4, 2017 On the Settings-Tab you can select the template of a page. Two questions: How do I change the order of them? Currently it looks very messy and I would like to sort it alphabetically How do I exclude certain templates from this list? I couldn't find a system field for it and have looked at other field configs, but noting helped. I've got some pages I don't want my client to be selectable. I have set "Can this template be used for new pages?" to no, but that doesn't work. Link to comment Share on other sites More sharing options...
bernhard Posted October 4, 2017 Share Posted October 4, 2017 what is the exact usecase? are you talking about changing the template after the page was saved? or are you talking about the process of adding new pages, where you want to limit + sort the selectable templates? Link to comment Share on other sites More sharing options...
suntrop Posted October 4, 2017 Author Share Posted October 4, 2017 Yes, I am talking about the settings tab, so after the page is saved or for existing pages. The dropdown list is about 20 entries long and it is pretty hard to find the exact template you're looking for, because there is naturally no order. That is bad usability and I want it to sort in an alphabetically order to make it easier finding a template. The exact same use case goes for excluding templates that make no sense for the editor (like templates just for settings or "admin" pages, etc.). Link to comment Share on other sites More sharing options...
bernhard Posted October 4, 2017 Share Posted October 4, 2017 two options come into my mind: hook into the buildform method, get the field and modify the available options modify the dropdown via javascript (at least sorting should be very easy like this - maybe also hiding different options, depends on your usecase and setup) 1 Link to comment Share on other sites More sharing options...
abdus Posted October 4, 2017 Share Posted October 4, 2017 Based on @bernhard's suggestion, you can combine template tags with ProcessPageEdit::buildSettingsForm hook and remove any template that doesnt have a specific tag. Start by adding a specific tag to each template you need to show, say `my-template-compatible`. Then add this hook to ready.php wire()->addHookAfter('ProcessPageEdit::buildFormSettings', function (HookEvent $e) { $page = $e->object->getPage(); if ($page->template != "my-template") return; $templatesField = $e->return->children->get('id=template'); $templates = $templatesField->options; foreach ($templates as $tid => $tname) { $template = $e->templates->get($tid); if (strpos($template->tags, 'my-template-compatible') === false) $templatesField->removeOption($tid); } }); When you edit a page with `my-template` template, you'll see that non compatible templates wont show up. 2 Link to comment Share on other sites More sharing options...
adrian Posted October 4, 2017 Share Posted October 4, 2017 7 hours ago, suntrop said: How do I change the order of them? Currently it looks very messy and I would like to sort it alphabetically Mine are sorted alphabetically. I would like to know why your's aren't! Link to comment Share on other sites More sharing options...
suntrop Posted October 4, 2017 Author Share Posted October 4, 2017 I thought there is some hidden config and an easy step. Seems there is no option at all https://github.com/processwire/processwire/blob/master/wire/modules/Process/ProcessPageEdit/ProcessPageEdit.module#L978 @adrian I am using labels, because template files are in Englisch, but my clients prefer a more eligible, German name. Will have a look tomorrow Link to comment Share on other sites More sharing options...
abdus Posted October 4, 2017 Share Posted October 4, 2017 @suntrop, I've updated my hook to include sorting by labels wire()->addHookAfter('ProcessPageEdit::buildFormSettings', function (HookEvent $e) { $page = $e->object->getPage(); if ($page->template != "my-template") return; $templatesField = $e->return->children->get('id=template'); $templates = $templatesField->getOptions(); // sort first asort($templates); foreach ($templates as $tid => $tname) { // remove incompatible fields $template = $e->templates->get($tid); if (!$template) continue; if (strpos($template->tags, 'my-template-compatible') === false) unset($templates[$tid]); } $templatesField->set('options', $templates); }); 3 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now