Jump to content

Clone, hooks, and page name auto increment


taotoo
 Share

Recommended Posts

I'm cloning a page several times, each time giving the new page the same title.

A hook is being used to perform the clone, and another to set both the page title and name.

With each new page PW is correctly appending a number to the page name, and incrementing it as necessary.

But after having created a few of these pages in succession, if I then go back and clone the first of the pages, the numbering of the first and last pages get messed up. This is perhaps easiest to see in a video: https://youtu.be/Xo6VaKPkyxk

This is the hook I'm using to clone (thank you to the original creator):

	$this->wire->addHookAfter('ProcessPageEdit::getSubmitActions', function($event) {
	  $page = $event->process->getPage();
	  if($page->template == "foo") return;
	  $actions = $event->return;
	  unset($actions['next']);

	  $actions['clone'] = [
		'value' => 'clone',
		'icon' => 'clone',
		'label' => 'Save + Add New Similar',
	  ];

	  $event->return = $actions;
	});

	$this->wire->addHookAfter('ProcessPageEdit::processSubmitAction', function($event) {
	  $action = $event->arguments(0); // action name, i.e. 'hello'
	  $page = $event->process->getPage(); // Page that was edited/saved
	  if($page->template == 'foo') return;

	  if($action === 'clone') {
		$copy = $this->wire->pages->clone($page);
		$copy->work_title .= ' (copy ' . uniqid() . ')';
		$copy->save();
		$this->wire->session->redirect($copy->editUrl);
	  }
	});

And this is the hook used to set the title and name:

$wire->addHookAfter("Pages::saveReady(template=work,id>0)", function(HookEvent $event) {
  $page = $event->arguments(0);
  $page->title = $page->work_artist->first->title.": ".$page->work_title;
  $page->name = $this->pages->names()->uniquePageName($name = $page->work_artist->first->name."-".$page->work_title);
});

Any thoughts appreciated!

Link to comment
Share on other sites

6 hours ago, taotoo said:
$wire->addHookAfter("Pages::saveReady(template=work,id>0)", function(HookEvent $event) {
  $page = $event->arguments(0);
  $page->title = $page->work_artist->first->title.": ".$page->work_title;
  $page->name = $this->pages->names()->uniquePageName($name = $page->work_artist->first->name."-".$page->work_title);
});

I'd start debugging by adding bd($page) from tracy debugger in this hook so that you see when and which page is saved. It seems that this hook somehow fires for all pages and therefore renames all those pages where it should only fire once. Not sure why this should be the case. Any other hooks involved?

  • Thanks 1
Link to comment
Share on other sites

1 hour ago, bernhard said:

I'd start debugging by adding bd($page) from tracy debugger in this hook so that you see when and which page is saved. It seems that this hook somehow fires for all pages and therefore renames all those pages where it should only fire once. Not sure why this should be the case. Any other hooks involved?

edit: I think it might be uniquePageNames not allowing the current name to be used, because it's already being used by that very same page. See my post after this one.

Thanks - there's no other hooks at all. I've actually now removed the first hook (the cloning one) to simplify things. In the new video I:

  • Manually create a page
  • Manually create a second page, with the same title. It correctly gets -1 appended to its name
  • Edit the first page. All I do is resave it, at which point it immediately gets -2 appended to it

I'm not sure what I should be looking at in TracyDebugger, but I added bd($page) to the hook and here's a screenshot taken just after the resave. It shows the previous and new name.

dump.thumb.jpg.d9899d441af502ef6f0ddd4c1d309fd0.jpg

Edited by taotoo
Link to comment
Share on other sites

A couple of further thoughts:

1. Looking at the page modified times on the settings tab, I think it only modifies the original page (and if I use the clone hook, the final page too).

2. If I just keep clicking save, it toggles between the current name and the next available name with a number appended to it

3. I wonder if uniquePageNames doesn't allow the current name to be used, since it's already taken by the page I'm editing...

 

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