froot Posted February 11, 2021 Share Posted February 11, 2021 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 More sharing options...
monollonom Posted February 11, 2021 Share Posted February 11, 2021 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. 1 Link to comment Share on other sites More sharing options...
froot Posted February 11, 2021 Author Share Posted February 11, 2021 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 More sharing options...
monollonom Posted February 11, 2021 Share Posted February 11, 2021 I've never had your situation so I wanted to be sure first. I quickly had a look at the code and I found this "Disable suggestions" option in $config->pageAdd, maybe try this ? 1 Link to comment Share on other sites More sharing options...
froot Posted February 11, 2021 Author Share Posted February 11, 2021 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 More sharing options...
monollonom Posted February 11, 2021 Share Posted February 11, 2021 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. 1 Link to comment Share on other sites More sharing options...
froot Posted February 11, 2021 Author Share Posted February 11, 2021 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 More sharing options...
monollonom Posted February 11, 2021 Share Posted February 11, 2021 (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 February 11, 2021 by monollonom Removed commas in code example... Link to comment Share on other sites More sharing options...
monollonom Posted February 11, 2021 Share Posted February 11, 2021 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. Link to comment Share on other sites More sharing options...
froot Posted February 11, 2021 Author Share Posted February 11, 2021 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 More sharing options...
Zeka Posted February 11, 2021 Share Posted February 11, 2021 @fruid Have you tried to hook ProcessPageAdd::executeTemplate or ProcessPageAdd::buildForm? 1 Link to comment Share on other sites More sharing options...
froot Posted February 11, 2021 Author Share Posted February 11, 2021 @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 More sharing options...
Zeka Posted February 11, 2021 Share Posted February 11, 2021 $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 More sharing options...
froot Posted February 11, 2021 Author Share Posted February 11, 2021 thanks but sorry, doesn't work I get: hasStatus does not exist or is not callable in this context no idea why 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