Jump to content

A hook to prefill PageTable fields with children on edit


Noel Boss
 Share

Recommended Posts

I use a PageTable field to make edits to children of pages more intuitive

To register the hooks, insert the following Snippet inside your init function in your module (or add it to your init.php file):

/**
 * Initialize the module.
 *
 * ProcessWire calls this when the module is loaded. For 'autoload' modules, this will be called
 * when ProcessWire's API is ready. As a result, this is a good place to attach hooks.
 */
public function init()
{
	// Prefill pageTable field
	$this->wire()->addHookBefore('InputfieldPageTable::render', $this, 'addChildrenToPageTableFieldsHook');
	$this->wire()->addHookBefore('InputfieldPageTableAjax::checkAjax', $this, 'addChildrenToPageTableFieldsHook');
}

Then, add this hook method:

/**
 * Fill pagetable fields with children before editing….
 *
 * @param HookEvent $event
 */
public function addChildrenToPageTableFieldsHook(HookEvent $event)
{
	$field = $event->object;

	// on ajax, the first hook has no fieldname
	if (!$field->name) {
		return;
	}
	
	// get the edited backend page
	$editID = $this->wire('input')->get->int('id');
	if (!$editID && $this->wire('process') instanceof WirePageEditor) {
		$editID = $this->wire('process')->getPage()->id;
	}
	$page = wire('pages')->get($editID);
	
	// disable output formating – without this, the ajax request will not populate the field
	$page->of(false);
	
	// you could also insert a check to only do this with sepcific field names…
	// $page->set($field->name, $page->children('template=DesiredTemplate')); // just specific templates
	$page->set($field->name, $page->children);
}

Now whenever there is a page-table field on your page, it gets populated with the children

Edited by Noel Boss
Using $page->set() instead of ->add() removes "orphant message"
  • Like 5
Link to comment
Share on other sites

@bernhard I felt my post is not qualified for a tutorial with »step-by-step instructions on how to do things in ProcessWire« – and since the description for API & Templates contains »answers and support on the API and template development« I thought it would better fit in there. I was actually looking for a hooks / snippet / recipe section in the forum – but that does not exists. I know, there is https://processwire-recipes.com but it just feels to cumbersome to add one ?

  • Like 1
Link to comment
Share on other sites

OK i understand ? I just didn't get that it was a snippet and "wasted" time to read your post and try to help where actually no problem existed and thought you might save others from that by naming it more obviously..

  • Like 1
Link to comment
Share on other sites

  • 3 years later...

@Noel Boss For some reason this doesn't work with your change from using add() to using $page->set() (PW 3.0.178). I got it to work and figured out a way to clear the orphans message:

$field->setOrphans(new PageArray());
$page->{$field->name}->add($page->children('include=all, check_access=0'));

I also added include=all and check_access=0 to make sure all children get added.

Here's the resulting full hook method:

/**
 * Fill pagetable fields with children before editing….
 *
 * @param HookEvent $event
 */
public function addChildrenToPageTableFieldsHook(HookEvent $event)
{
	$field = $event->object;

	// on ajax, the first hook has no fieldname
	if (!$field->name) {
		return;
	}
	
	// get the edited backend page
	$editID = $this->wire('input')->get->int('id');
	if (!$editID && $this->wire('process') instanceof WirePageEditor) {
		$editID = $this->wire('process')->getPage()->id;
	}
	$page = wire('pages')->get($editID);
	
	// disable output formating – without this, the ajax request will not populate the field
	$page->of(false);
	
	// you could also insert a check to only do this with sepcific field names…
	$field->setOrphans(new PageArray()); // Clear out the "orphans" notice
	$page->{$field->name}->add($page->children('include=all, check_access=0'));
}

 

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