psy Posted November 15, 2018 Share Posted November 15, 2018 (edited) I have a template that is the only child of a parent template so the admin is not asked to enter a title/name. The page is automatically created and admin presented with the custom fields screen. All good so far. The page name is auto-generated with the date/time. Again all good. Problem occurs once the admin changes fields on the page. The new title and page name is a combination of: 1. Calculation of start date and end date of the event, eg 1/11/2018 to 2/11/2018 results in "2 Day " 2. Title of a page reference field, eg "Long Reining Clinic", and 3. The start date That works providing the clinic title doesn't have an ampersand in it... that results in an Error 403 Permission Denied error when editing the event. Got over that one only just and no understanding why it happens Immediate and bigger problem is that 2 pages are created: 1. First on creation of the page - unpublished with default title and no custom data 2. Second on admin update of the custom fields - often there is a conflict of page ids Below is my hook in ready.php to change the page title & name once the relevant custom fields have been entered. It's taken pretty much directly from the PW docs. The CustomFunctionDDH::clinicDays returns a string as per above, eg "2 Days " /***** Update page title for event/clinic pages */ wire()->addHookBefore('Pages::saveReady', function (HookEvent $event) { $pages = $event->object; $page = $event->arguments(0); if ($page->template != 'event') return; if (empty($page->id)) return; // doesnt work for new pages if ($page->isChanged()) { $title = CustomFunctionsDDH::clinicDays($page) . " "; $title .= $pages->get("id=" . $page->pg_clinic_type_title)->title; $page->of(false); $page->title = $title; $startDate = wire('datetime')->date('d m Y', $page->start_date); $newName = str_ireplace('---', '-', wire('sanitizer')->pageName("$title $startDate")); if ($page->name != $newName) $page->setName($newName); $page->seo_title = $title; } }); What am I doing wrong? Help appreciated Solution: Problem due to operator error/digital malfunction, ie me! Doh! BEFORE I set the page parent template Events to only accept child pages with template Event, I had added one child page with template 'basic-page'. No wonder PW got confused. Changed the parent template Events to also accept child pages with template 'basic-page', problem solved. Now though, instead of automatically going to the 'event' custom fields, the admin must enter a page title (could be aaaaa) and save. Client and I can live with that. Thank you @kongondo for your suggestions. Sometimes the obvious is staring me in the face... Edited November 20, 2018 by psy Solved problem Link to comment Share on other sites More sharing options...
kongondo Posted November 19, 2018 Share Posted November 19, 2018 On 11/15/2018 at 9:57 AM, psy said: Immediate and bigger problem is that 2 pages are created: 1. First on creation of the page - unpublished with default title and no custom data 2. Second on admin update of the custom fields - often there is a conflict of page ids Maybe I'm missing the point since I don't see the code where you create the page. However, both actions 1 and 2 fire a page->save() event. So, if your code doesn't check whether the page is a new page vs an existing page being edited, a new page will be created. Maybe you can post that part of the code, the one where you create a new page? You might need to hook into two places: When page is created (newly created page): $pages->addHookAfter("added", function(HookEvent $event) { // code here }); And when page is edited: $pages->addHookBefore("saveReady", function(HookEvent $event) { // code here }); Link to comment Share on other sites More sharing options...
psy Posted November 19, 2018 Author Share Posted November 19, 2018 Thanks @kongondo The page is created automatically by PW, not via the API when the admin clicks on the parent page -> new. I think I need to find the right conditional statement to say return if it's a new page with no required custom fields completed. Maybe instead of checking if the page has an id: if (wire('pages')->added($page)) return; Link to comment Share on other sites More sharing options...
kongondo Posted November 19, 2018 Share Posted November 19, 2018 (edited) 20 minutes ago, psy said: The page is created automatically by PW, not via the API when the admin clicks on the parent page -> new. It shouldn't matter and I don't mean to be pedantic here ? but page creation (actually anything CRUD in ProcessWire) whether via GUI or otherwise, is carried out by the API, the very same API. 20 minutes ago, psy said: if it's a new page That's what this will tell you ?: $pages->addHookAfter("added", function(HookEvent $event) { // code here }); It's only called when a new page is created. It will be called whether the page is created directly using the API (ProcessWire, template-file, etc) or via the GUI. It depends on your use case though, so I maybe off here and you could just do a different condition check. Edited November 19, 2018 by kongondo typos 1 Link to comment Share on other sites More sharing options...
kongondo Posted November 19, 2018 Share Posted November 19, 2018 28 minutes ago, psy said: I think I need to find the right conditional statement to say return if it's a new page with no required fields completed. The challenge here is that what if admin opens the page for editing but saves it without filling any of the required fields? Doesn't the page still save in that situation? (I can't remember). If it does save, then the condition will fail since the page has been saved but the required fields are empty, and hence, a new page will be created. One alternative (just thinking on my feet here), would be to assign the newly created pages a 'temporary-template'. When editing the page, on save we change its template (e.g. 'some-template'). Then in your hook, check if template == temporary-template return. Link to comment Share on other sites More sharing options...
psy Posted November 19, 2018 Author Share Posted November 19, 2018 Your alt method sounds plausible. Will try it ? Link to comment Share on other sites More sharing options...
kongondo Posted November 20, 2018 Share Posted November 20, 2018 On 11/15/2018 at 9:57 AM, psy said: problem solved. Excellent. Glad you got it sorted. 1 Link to comment Share on other sites More sharing options...
szabesz Posted August 29, 2019 Share Posted August 29, 2019 (edited) On 11/15/2018 at 10:57 AM, psy said: BEFORE I set the page parent template Events to only accept child pages with template Event, I had added one child page with template 'basic-page'. No wonder PW got confused. Changed the parent template Events to also accept child pages with template 'basic-page', problem solved. Now though, instead of automatically going to the 'event' custom fields, the admin must enter a page title (could be aaaaa) and save. Client and I can live with that. I have just run into – probably – the same issue, although I did not mixed up any children. I have a parent-child template relationship set up as usual, for the parent only the given children are allowed, for the child only a the given parent. When clicking on "new page" in the admin, two children are created at once, instead of one. I also added an unrelated template to the "Allowed template(s) for Children" of the parent template as you did to "solve" the issue, but I think it is just a workaround as this behavior looks like a bug to me. Edited August 31, 2019 by szabesz typo 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now