Jump to content

Edit Template Dropdown on Settings Tab


suntrop
 Share

Recommended Posts

On the Settings-Tab you can select the template of a page. Two questions:

  1. How do I change the order of them?  Currently it looks very messy and I would like to sort it alphabetically 
  2. 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

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

two options come into my mind:

  1. hook into the buildform method, get the field and modify the available options
  2. 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)
  • Like 1
Link to comment
Share on other sites

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.

  • Like 2
Link to comment
Share on other sites

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!

59d50ee7ba117_ScreenShot2017-10-04at9_39_13AM.png.dd905c835c3f4fa9ae6b6b9137827f21.png

Link to comment
Share on other sites

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

@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);
});

 

  • Like 3
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...