Jump to content

change default template selected upon page creation


Recommended Posts

Posted

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.

Posted

Does the parent page need to accept multiple templates as children ? If not you can set this in the "Family" tab of your template to only allow for one template, and then it will be automatically selected upon page creation.

  • Like 1
Posted

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.

Posted

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!

Posted

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
Posted

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

Posted (edited)

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

Ok one issue though is that it's a global setting so I'm not sure how it will play out with the rest of your setup... so maybe you do need to use a hook in the end to be able to change this setting before ProcessPageAdd is started.

Posted

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

Posted

@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?

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

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