Jump to content

[SOLVED] Add additional values when creating page via Page reference field


cehlo
 Share

Recommended Posts

Hello everyone,

I am starting to creating some hooks, but I am little bit new to this part of Processwire so please help ?

My problem: 

I have multiple fields in one template - Page Reference - I activated autocomplete and also possibility of creating new pages from the fields. 

Is it possible to add additional values to newly created page via Page Reference field? 

For example: I know that the name of Page reference field is called for example car - I am searching with autocomplete for example for Mercedez, it is not created, so I know that newly created page via this field will have value in category field - cars,...

I think I should addHookbefore to InputfieldPage::processInputAddPages, am I on the right track? Any assumptions? 

Thanks for help.

 

Link to comment
Share on other sites

I'm not sure I understand. Do you mean to not only create a new page and the default title, but also populate other fields of the same template? What kind of data would that be?

2 hours ago, cehlo said:

For example: I know that the name of Page reference field is called for example car - I am searching with autocomplete for example for Mercedez, it is not created, so I know that newly created page via this field will have value in category field - cars,...

^ can you rephrase that? If you just hit enter, it will create a new page; that's an in-built functionality.

Link to comment
Share on other sites

Yes exactly, I want to create page with default title, but at same time populate value to one field in created page in relation with Page Reference field in which was new page created. 

My assumption is that I need to return value of page id created with this function ___processInputAddPages($input) /if that is possible/ and than add to created page desired value in relation with Page reference field. 

$wire->addHookAfter('InputfieldPage::processInputAddPages', function($event) {

// Retrieve added page id - this is the hard part, I don't know how to do this


// And than populate values to another field 
   
$field = $event->object;

if($field->name == "dog_father"){
		$page = $pages->get(???); //need for ID of created page 
		$page->setAndSave('dog_sex', 1);
} else if($field->name == "dog_mother"){
		$page = $pages->get(???); //need for ID of created page 
		$page->setAndSave('dog_sex', 2);
}


});

Or I am coming totally wrong on this? ?

Link to comment
Share on other sites

4 hours ago, cehlo said:

But in all cases how would be the correct way to return created page id with that hook? Is it even possible?

No time to test right now, but perhaps you actually need to use: Pages::added

Link to comment
Share on other sites

It might be a good idea to tell us more about your goal ? Maybe there is a better solution to what you are trying to achieve with an additional field populated after the page is added from the page field...

If I understand you correctly, you have a template "dog" with two page fields, one for "father" and one for "mother", right? So maybe you don't even need to populate this field, because you could just use another query. Or you could just use a saveReady hook, that set's the sex of the father to male and sex of the mother to female whenever the child dog is saved...

Link to comment
Share on other sites

The solution with module suggested by Adrian works perfectly, no more headaches. 

My problem was that the field dog-father and dog mother are fields Page Autocomplete, but with query selector, for dog_mother -> show only pages of template where dog_sex = female and for dog_father ->show only pages of template where dog_sex = male. So when I create new page via Autocomplete by hitting enter, it does not populate created page in field because, created page have no dog_sex setting saved, only title when doing it this way. 

Now I create all page in modal, dog_sex is required field so, problem solved. 

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