Jump to content

change default template selected upon page creation


fruid
 Share

Recommended Posts

I want to change the template that gets selected by default when creating a page. Somehow it just always remembers the template of the last page that was created.

Though it seems like a small issue, I realise that a number of clients find this quite confusing and understandibly run into issues.

Link to comment
Share on other sites

fair enough, yes I'm aware I can and I indeed did restrict the selection of templates a bit but that wouldn't solve my issue. It's still more than just one template that need to be allowed and among those, a specific one should be default, no matter what the previously created page's template is.

Link to comment
Share on other sites

OK how did you find this?

I'm asking cause it puzzles me how everyone seems to refer to the Github Processwire repository when they need anything while mortals like me are left with the Processwire API documentation.

And then a follow up question, how would I use $config->pageAdd then? This might be self-explanatory to someone but it's not to me at all. Do I create a hook? Do I set something to false in the config file? No idea.

I thought it would be something like

$this->addHookBefore('ProcessTemplate::execute', function($event) {…}); 

but no.

Thanks for help!

Link to comment
Share on other sites

Lol you're not alone, I struggle a bit sometimes too...

To tell you the truth here was my train of thought: when trying to replicate your situation by adding several templates as authorized children, I came across the ProcessPageAdd module settings, and thought I might take a look at its code to see where it does the check to pull out the available templates. And then I finally stumbled upon this option I linked above...

In your config.php file (in the /site/ folder), you can just add a line

$config->pageAdd("noSuggestTemplates", true)

So no need for a hook.

Let me know how it goes.

  • Like 1
Link to comment
Share on other sites

haha OK, so I'm not alone ?

I tried, it does what it promises, it disables them all, so none is pre-selected.

Now, how to always pre-select the first one no matter what?

it says…

protected $settings = array(
    'noSuggestTemplates' => '', // Disable suggestions: 1|true=disable all, or space-separated template names
);

with that in mind I experimented but true seems is the only one that works

Link to comment
Share on other sites

What if you try to put all the template names except the one you want to be selected ? Re: "space-separated template names"

$config->pageAdd("disableTemplates", "template1 template2 template4")

Is "template3" then selected as you want ?

Edited by monollonom
Removed commas in code example...
Link to comment
Share on other sites

I tried that, doesn't work. It's also quite hard-coded this way. The real issue is basically that it always changes and remembers the template of the last page which is likely irrelevant. It'd be better if it were consistent, either select none (this we accomplished) or the first (which happens to be the one I want to be selected cause it's alphabetical).

Link to comment
Share on other sites

@Zeka Yes, I am giving this one a go as we speak

So maybe that goes in the right direction?

https://processwire.com/api/ref/process-page-add/set-predefined-templates/

but then, after reading an API doc like this I don't feel smarter, maybe even less smart than before. How should I read this?

Also, before I try to figure out how to access the array/WireArray of available templates (if I understand correctly these are the only types of arguments accepted here), how is an array going to help me specify the first one?

Here's what I have so far…

$this->addHook('ProcessPageAdd::executeTemplate', function($event) {
    $preselect = $event->object;
    $temps = $preselect->template; // how to select the optional templates?
    $preselect->setPredefinedTemplates($temps); // but that's an array, how to tell it to pick the first
});

OK now you see how my mind works ?

EDIT: OK I  guess I can do 

$preselect->parent->childTemplates();

to  get the available templates, right?

Link to comment
Share on other sites

		$this->wire()->addHookBefore('ProcessPageAdd::execute', function($event) {
			$object = $event->object;
			$allowedTemplates = WireArray($object->getAllowedTemplates($this->pages($this->input->get('parent_id'))));
			if($allowedTemplates->has('basic-page')) {
				$this->wire()->addHookBefore('InputfieldSelect::render', function($event) {
					$event->object->set('defaultValue', $this->wire('templates')->get('basic-page')->id);
				});
			}
		});

EDIT: this code worded for some pages, but not for other. So, probably buggy solution.

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...