Jump to content
cehlo

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

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.

 

Share this post


Link to post
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.

Share this post


Link to post
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? 😄

Share this post


Link to post
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

Share this post


Link to post
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...

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Peter Troeger
      Hello 🙂
      I have set up pagination on a mulitlanguage site.
      I've done this before, but this time I have a problem I can't solve.
      Pagination is activated on 'parent-template' and 'child-template' just to be sure 🙂
      This is my code:
      $children = $page->children('limit=1'); foreach($children as $child) { $title = $child->title; echo $title; } echo $children->renderPager(); The navigation is output correctly and the link look correct as well 'parent-page/page2/'.
      But when I click the link, the site seems to redirect back to 'parent-page/
      Any help would be greatly appreciated 🙂
      - Peter
    • By Jens Martsch - dotnetic
      Hi guys I need an estimate for the following task:
      I developed a job candidate application in ProcessWire and need an extension to a Lister Pro Page.
      My customer wants to display a summary table of the filtered data like in the attached screenshot.
      When you filtered the view, that summary should show how many candidates have which status according to the actual filtered view.
      If you change the filter, the summary table has to be updated also.
      Who can accomplish this task and what would it cost?
      I first need an estimate to tell it to my customer. If he says the price is ok I will provide you with FTP Data to a dev server version of  that tool.
      Thank you in advance.

    • By LuisM
      Hi there,
      im a bit lost right now. I added an hook after templates::save which should get the affected template and write its template ID into a JSON.
      The problem is, the event->object gives me an instance of the templates class. How can I narrow this down to the saved Template and get its ID and fieldgroup?
    • By Clément Lambelet
      Hey!
      I've been working on a Processwire installation (3.0.123) for a few days now and I must have made a big mistake this morning because the links in the admin's main menu no longer appear.
      This is not related to the admin theme, because the bug occurs with all themes (Default, Reno, Uikit).
      I tried to reinstall with the dev version (3.0.136), but the problem is still there. I also uninstalled all the modules I had added, without success. There is no error in the js console. 
      I still can access/view/edit the pages  by going through the admin/page list.
      Thanks in advance for your help!


    • By lecrackffm
      Hey all,
      i do, most probably have a problem with file encoding and / or BOM. 
      My <head>  content is getting moved into <body> and i can inspect a couple of &#65279;  ( ZERO WIDTH NO-BREAK SPACE) chars with dev tools.
      I checked all template files with phpstorm, they are all in utf-8 w/o BOM.
      The current installation is based on a profile i exported from another project where i had not such problems.
      However, in the new project i did change the default language to german and installed english as the second language. 
      Might there be any conflicts with the language support modules?
      Hints, tips or solutions are highly appreciated 🙂
×
×
  • Create New...