Jump to content
Robin S

Adding a clone button to Page Edit

Recommended Posts

If you have the core ProcessPageClone module installed you can copy a page via Page List:

2020-12-21_150048.png.95af4987daf701c2fbd11d919a2f1bd5.png

 

But sometimes I don't want to hunt around through Page List to find the page I want to copy, and instead I have found the page via the admin search and have it open in Page Edit. So I used this hook in /site/ready.php to add a "Clone this page" button to the bottom of the Settings tab.

2020-12-21_150341.png.55582c1011e29f3755732d0e95435e62.png

// Add a clone button to the Settings tab of Page Edit if this page is allowed to be cloned
$wire->addHookAfter('ProcessPageEdit::buildFormSettings', function(HookEvent $event) {
	$wrapper = $event->return;
	$modules = $event->wire()->modules;
	$page = $event->process->getPage();
	/** @var ProcessPageClone $ppc */
	$ppc = $modules->get('ProcessPageClone');
	if($page && $ppc->hasPermission($page)) {
		/** @var InputfieldButton $f */
		$f = $modules->get('InputfieldButton');
		$f->value = 'Clone this page';
		$f->href = $event->wire()->config->urls->admin . 'page/clone/?id=' . $page->id;
		$wrapper->add($f);
	}
});

Maybe somebody else has this need and finds this useful.

  • Like 9

Share this post


Link to post
Share on other sites

Also, I want to open the page in Page Edit after it is cloned. Here is a hook for that:

// Edit the cloned page after it is created
$wire->addHookBefore('ProcessPageClone::execute', function(HookEvent $event) {
	$event->wire()->addHookBefore('Session::redirect', function(HookEvent $event) {
		$url = $event->arguments(0);
		$id = (int) filter_var($url, FILTER_SANITIZE_NUMBER_INT);
		$redirect_url = $event->wire()->config->urls->admin . 'page/edit/?id=' . $id;
		$event->arguments(0, $redirect_url);
	});
});

 

  • Like 3

Share this post


Link to post
Share on other sites

This is what I am using:


    $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 + create copy',
      ];

      $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->title .= ' (copy ' . uniqid() . ')';
        $copy->save();
        $this->wire->session->redirect($copy->editUrl);
      }
    });

 

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...