Jump to content

Preventing page moves


MarkE
 Share

Recommended Posts

Preventing a page from being moved is not necessarily a straightforward matter of trapping the change in a save hook in the same way as a page edit.

A problem occurs if the page is only to be 'blocked' conditionally - when the page path is in some given array, for example. In hooking before page save, the page path is the new page path after the move, rather than before it, so you need to do something like this:

// .... In the before page:saveReady hook
// $page is the page :) The code below operates when (isset($page->parentPrevious) && $object->parentPrevious != $object->parent)
				$name = $page->parentPrevious->path . $page->name . '/';  // need the path before it was moved
				if(in_array($name, $blockedNames)) {  // $blockedNames are the ones where we don't want moves
				// Because this hook operates after the move, we need to reverse the move
					$page->parent = $page->parentPrevious;
				// Alternatively, to completely stop the save
					//$event->replace = true;
					//$event->return = false;
				}
//... rest of hook

This prevents the page being moved, both in the page hierarchy (by dragging) or in the page editor by changing the parent in the settings tab. 

It is also possible to hook before Page:moveable with something like this

		/** @var Page $page */
		$page = $event->object;
		$moveable = $event->return;
		// ... code to set $moveable to false if conditions met
		$event->return = $moveable;

Interestingly, the (not documented?) moveable method is created as a hook by PagePermissions and so is hookable. However, this method appears to catch only the situation where the move is effected by dragging the page in the tree, not when the parent is changed in the settings.

  • Like 3
  • Thanks 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

×
×
  • Create New...