Jump to content

Hook to save name when adding a new page


cb2004
 Share

Recommended Posts

I am coming up against a bug where I cant make use of the functionality 'Name format for children', and it doesn't seem like a hook is possible to save the name:

$wire->addHookBefore('Pages::setupNew', function(HookEvent $e) {
    $page = $e->arguments(0);
    if($this->input->parent_id === '1') {
        $page->name = 'some-name';
    }
});

I have tried many solutions from the forums, nothing is achieving what I would like, even for example if I am trying it on the home page like the code above.

Any help would be much appreciated.

Link to comment
Share on other sites

Try this...

$wire->addHookBefore('ProcessPageAdd::execute', function(HookEvent $event) {
	// Get the id of the parent page the new page is being added under
	$parent_id = $event->input->get->int('parent_id');
	// Return early if not the id of the parent page you want to target
	if($parent_id !== 1234) return;
	// The following line probably not strictly necessary because we will redirect in a moment anyway
	$event->replace = true;
	// Create the page
	$p = $event->pages->add('your-template-name', $parent_id, 'your-page-name');
	// Make the page unpublished to begin with
	$p->addStatus(Page::statusUnpublished);
	// Save the unpublished status
	$p->save();
	// Redirect to edit the page just created
	$event->session->redirect($p->editUrl);
});

 

  • Like 7
Link to comment
Share on other sites

8 hours ago, Robin S said:

Try this...


$wire->addHookBefore('ProcessPageAdd::execute', function(HookEvent $event) {
	// Get the id of the parent page the new page is being added under
	$parent_id = $event->input->get->int('parent_id');
	// Return early if not the id of the parent page you want to target
	if($parent_id !== 1234) return;
	// The following line probably not strictly necessary because we will redirect in a moment anyway
	$event->replace = true;
	// Create the page
	$p = $event->pages->add('your-template-name', $parent_id, 'your-page-name');
	// Make the page unpublished to begin with
	$p->addStatus(Page::statusUnpublished);
	// Save the unpublished status
	$p->save();
	// Redirect to edit the page just created
	$event->session->redirect($p->editUrl);
});

 

Perfect, thanks so much Robin.

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