Jump to content

Preset of template in template-pulldown (when creating a new page)


cabrooney
 Share

Recommended Posts

I have the following situation: The user can add child-pages to a page and can choose between two different possible templates (On the page with the headline "Add New"). Is it possible to select, which of the two templates is preselected in the pulldown? (Because one is only used in 5% of the cases, but is preselected at the moment).

What i tried so far:
- I found the "PageAdd module settings" but changeing the order there does not seem to work.
- Also the order of the parents "Family" section under "Select the template" does not change anything.

Link to comment
Share on other sites

6 hours ago, cabrooney said:

Because one is only used in 5% of the cases, but is preselected at the moment

ProcessPageAdd preselects the template according to these principles:

  • If there is already one or more child pages under the parent then it preselects the same template as used by the most recently added child page.
  • If there are no child pages it preselects the same template as used by the parent page. But if that template isn't allowed to be used under the parent page then no template is preselected.

So probably you recently added a page using the template that is only used in 5% of the cases, or that is the template of the parent page.

You can force a particular template to be preselected in a couple of ways.

1. You can add a "template_id" GET variable to the URL of the "Add New" page. Example: "/processwire/page/add/?parent_id=1234&template_id=29"
This might be a good approach if you are constructing your own custom link to the "Add New" screen but not so useful if you are just adding a new page via the standard PW admin.

2. You can use a hook in /site/ready.php to set a template_id GET variable to $input:

// Before the ProcessPageAdd form is built
$wire->addHookBefore('ProcessPageAdd::buildForm', function(HookEvent $event) {
	$input = $event->wire()->input;
	// Get the parent page the new page is being added under
	$parent_id = (int) $input->get('parent_id');
	$parent = $event->wire()->pages->get($parent_id);
	// Return early if a single parent page isn't available or valid  
	if(!$parent->id) return;
	// Do whatever check you need to identify if this is the parent you want to target
	if($parent->template == 'home') {
		// Get the template you want to be preselected
		$child_template = $event->wire()->templates->get('basic_page');
		// Set the template ID to the "template_id" GET variable
		$input->get->template_id = $child_template->id;
	}
});

 

  • Like 5
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...