Jump to content

[SOLVED] Change parent_id of InputfieldPage when opening edit page


monollonom
 Share

Recommended Posts

Hi,

I'm looking for the right hook to change the parent_id property of an InputfieldPage.

I have another InputfieldPage in my template which determines the parent of the selectable pages using this hook :

$wire->addHookAfter("InputfieldPage::getSelectablePages", function($event) {
  $page = $event->arguments(0);
  if($event->object->hasField == "subcategories") {
    if ($page->category) {
      $event->return = $event->pages->find("template=subcategory,parent=$page->category");
    } else {
      $event->return = [];
    }
  }
});

But in order to be able to create new pages using the field I need to dynamically change the parent_id before it [the field] is created in the edit page.

What would be the right hook (or way ?) to do so ?

Thanks !

Link to comment
Share on other sites

Hm this is actually happening when saving the page, while I was looking to be able to show the "Create new" link in the admin panel. Sorry I must have not been clear enough in my post.

But you pointed me to the right place to look at, I think I should instead change the parent_id in a beforeHook of https://github.com/processwire/processwire/blob/master/wire/modules/Inputfield/InputfieldPage/InputfieldPage.module#L682

Link to comment
Share on other sites

My apologies, you were also right @Zeka !

As the parent_id does not persist after ___renderAddable, I'm adding the same hook for both functions in my ready.php :

$wire->addHookBefore('InputfieldPage::renderAddable', null, "changeParentId");
$wire->addHookBefore('InputfieldPage::processInputAddPages', null, "changeParentId");

function changeParentId($event) {
  $inputfieldPage = $event->object;
  $page = $inputfieldPage->hasPage;
  if($inputfieldPage->hasField == "subcategories") {
    if ($page->category) {
      $inputfieldPage->set("parent_id", $page->category->id);
    }
  }
}

And it works great ! Thank you !

  • Like 2
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...