Jump to content

Create new pages through Page Reference field as children of the current page?


lpa
 Share

Recommended Posts

I have defined a Page Reference field in my template. I want to be able to create new pages through that field. I am using AdminPageFieldEditLinks-module.

My problem is that I would like to define the new pages to be created as childs to the current page. I don't know how to define Selectable Pages to be the children of the current page. If I leave Selectable pages:Parent empty, I get the error: 

ProcessWire: ProcessPageAdd: No parent templates defined for <template>

Any idea if this is possible and how to define the target of the new pages to be children of the current page?

Link to comment
Share on other sites

Thank you for your answer. Off course I know that it should work like that. Maybe my question was not clear enough. 

I asked, how could I define the new pages to be created as childs of the current page as I can't select the "current page" dynamically. I tried to define it like "parent=[page]", but that did not work. 

Link to comment
Share on other sites

In your field settings define "Template" for selectable pages but not  "Parent".

Check the "Enable link to create new pages?" checkbox that is added by the AdminPageFieldEditLinks module, but don't select anything in the "Parent for new pages created" field.

Add the following hook to your /site/ready.php to define the parent dynamically.

$wire->addHookBefore('InputfieldPage::render', function(HookEvent $event) {
	/** @var InputfieldPage $inputfield */
	$inputfield = $event->object;
	$page = $inputfield->hasPage;
	$field = $inputfield->hasField;
	if($page && $field && $field->name === 'your_field_name') {
		$inputfield->parent_id = $page->id;
	}
});

 

Link to comment
Share on other sites

Great! That was just what I needed.

I think there is something missing or a bug in AdminPageFieldEditLinks as it says it should give me the opportunity to select the path where save the new page if no Parent is given. Now it gives me an error. And maybe the Select Parent should somehow support the dynamic parent to be the current page. 

Link to comment
Share on other sites

  • 2 weeks later...
On 2/4/2025 at 5:32 AM, Robin S said:

In your field settings define "Template" for selectable pages but not  "Parent".

Check the "Enable link to create new pages?" checkbox that is added by the AdminPageFieldEditLinks module, but don't select anything in the "Parent for new pages created" field.

Add the following hook to your /site/ready.php to define the parent dynamically.

$wire->addHookBefore('InputfieldPage::render', function(HookEvent $event) {
	/** @var InputfieldPage $inputfield */
	$inputfield = $event->object;
	$page = $inputfield->hasPage;
	$field = $inputfield->hasField;
	if($page && $field && $field->name === 'your_field_name') {
		$inputfield->parent_id = $page->id;
	}
});

 

Thank you so much for the help.

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