Liam88 Posted September 27 Share Posted September 27 (edited) Hey Having a struggle with this one. My project sends content to openAI, it sends back tags. Where the tags aren't an existing page, it creates them. I then want to add those tag pages to the content. assetPage is the content content_tags is the page reference within that page content-tag is the template for the tag pages added. I'm getting it to say it's adding the pages to content_tags, but in any form I'm trying it doesn't add them. I have popped my example php below and here are an example log. You will see it gets stuck at attempting to save. Any advice on where I am going wrong in adding multiple pages to a page reference field at once would be amazing. Is it the way I'm trying to save? Date/Time User URL Text 10 seconds ago 2024-09-27 17:35:13 guest /api/ad-review/ Inside try block - attempting to save. 10 seconds ago 2024-09-27 17:35:13 guest /api/ad-review/ Attempting to save the asset page with updated content_tags. if (!empty($tags)) { $log->save('ad-review', 'Processing tags to create or find corresponding pages.'); $tagPages = processTags($tags, $log); // Log the tag pages returned from processTags $log->save('ad-review', 'Tag pages found or created: ' . implode(', ', array_map(function ($page) { return $page->title; }, $tagPages))); // Log the current state of content_tags $currentTags = $assetPage->{'content_tags'}->explode('id'); $log->save('ad-review', 'Current content_tags IDs before update: ' . implode(', ', $currentTags)); // Ensure output formatting is disabled $assetPage->of(false); // Check if the content_tags field exists and is accessible if (!$assetPage->getField('content_tags')) { $log->save('ad-review', 'Field content_tags does not exist or is not accessible on the asset page.'); return; // Exit if the field is not found } else { $log->save('ad-review', 'Field content_tags exists on the asset page and is ready for update.'); } // Add each tag page to the content_tags field using add() foreach ($tagPages as $tagPage) { if ($tagPage && $tagPage->id) { try { // Add the tag page directly using the add() method $log->save('ad-review', 'Adding tag page to content_tags: ' . $tagPage->title . ' - ' . $tagPage->id); $assetPage->{'content_tags'}->add($tagPage->id); } catch (Exception $e) { $log->save('ad-review', 'Error adding tag page to content_tags: ' . $tagPage->title . '. Error: ' . $e->getMessage()); } } else { $log->save('ad-review', 'Failed to add tag page: ' . ($tagPage ? $tagPage->title : 'Unknown')); } } // Additional logging before save attempt $log->save('ad-review', 'Attempting to save the asset page with updated content_tags.'); // Explicitly try saving the asset page try { // Another log entry just before the save method is called $log->save('ad-review', 'Inside try block - attempting to save.'); // Execute the save operation if ($assetPage->save()) { $log->save('ad-review', 'Asset page saved successfully with updated content_tags.'); // Re-fetch and log the content_tags after saving to confirm $savedTags = $assetPage->{'content_tags'}->explode('id'); $log->save('ad-review', 'Content_tags IDs after save: ' . implode(', ', $savedTags)); } else { $log->save('ad-review', 'Save method returned false, indicating save was not successful.'); } } catch (Exception $e) { $log->save('ad-review', 'Failed to save asset page with updated content_tags. Error: ' . $e->getMessage()); } } else { $log->save('ad-review', 'No tags to process.'); } Edited September 27 by Liam88 Link to comment Share on other sites More sharing options...
Jan Romero Posted September 27 Share Posted September 27 (edited) 4 hours ago, Liam88 said: $assetPage->{'content_tags'}->add($tagPage->id); Any particular reason for this curly brackets syntax? I’m far from a PHP pro but I’ve never seen that and $assetPage->content_tags->add($tagPage) should suffice. I might look at your code again later when I’m on desktop. Also, might be obvious, but make sure you save the tagPages before adding them 😅 Edited September 27 by Jan Romero 2 Link to comment Share on other sites More sharing options...
wbmnfktr Posted September 27 Share Posted September 27 Two things I would try and check: double check your page reference field allows the creation of new pages and all settings are correct - including parent and template settings try to save after the tag was added. Either with $page->save(); or $page->save('your_field_name') 2 Link to comment Share on other sites More sharing options...
Liam88 Posted September 27 Author Share Posted September 27 16 minutes ago, Jan Romero said: Any particular reason for this curly brackets syntax? I’m far from a PHP pro but I’ve never seen that and $assetPage->content_tags->add($tagPage) should suffice. I might look at your code again later when I’m on desktop. Also, might be obvious, but make sure you save the tagPages before adding them 😅 Good spot, oversight from me. I don't use them on my other section either. Yeah all the tagPages are created and saved before, but will triple check. Link to comment Share on other sites More sharing options...
Liam88 Posted September 27 Author Share Posted September 27 15 minutes ago, wbmnfktr said: Two things I would try and check: double check your page reference field allows the creation of new pages and all settings are correct - including parent and template settings try to save after the tag was added. Either with $page->save(); or $page->save('your_field_name') Yeah page reference allows creation and the tagPages are being created and saved. I was thinking should I save after each tag is added or add all then save. Will give your suggestion a go. Appreciate it. Link to comment Share on other sites More sharing options...
Jan Romero Posted September 27 Share Posted September 27 Adding all pages and then saving is the way to go. ProcessWire’s arrays have change tracking so they’ll know which items were added or removed. You can dump the field before saving and you should see a property called "added" or some such that lists them. Try TracyDebugger’s bd() method (recommended!) or just var_dump(). 1 Link to comment Share on other sites More sharing options...
da² Posted September 28 Share Posted September 28 16 hours ago, Jan Romero said: ProcessWire’s arrays have change tracking so they’ll know which items were added or removed Sometimes change tracking is not working properly and before to save the page we have to do this: $assetPage->trackChange('content_tags'); $assetPage->save(); Link to comment Share on other sites More sharing options...
Liam88 Posted September 28 Author Share Posted September 28 Stupidly I think I may have found the issue. So I did a check if field is editable and it returned no. Going to adjust access and try again. Link to comment Share on other sites More sharing options...
Liam88 Posted September 28 Author Share Posted September 28 Thank you all for the help. In the end it was two things: field was not editable the second was a silly mistake of creating the tag pages as template = content-tags, instead of content-tag Can confirm up and working correctly now. The silliest things seem to be the biggest time sucker 🤣 1 Link to comment Share on other sites More sharing options...
da² Posted September 28 Share Posted September 28 (edited) 1 hour ago, Liam88 said: field was not editable This should change nothing for the API, field is not editable in admin pages but from the API you can edit it. Edited September 28 by da² 1 Link to comment Share on other sites More sharing options...
Liam88 Posted September 29 Author Share Posted September 29 On 9/28/2024 at 4:22 PM, da² said: This should change nothing for the API, field is not editable in admin pages but from the API you can edit it. ok, interesting because when I just removed the page-edit access it wouldn't create the tags and save them. It shows as guest within the logs which is likely the issue. I'm guessing I need to ensure it has it down as the user that is making the action. Here's an example of the log, going to try make it so it's the actual user and not the guest Date/Time User URL Text 18 seconds ago 2024-09-29 21:22:23 guest /api/ad-review/ AI processing completed successfully. 18 seconds ago 2024-09-29 21:22:23 guest /api/ad-review/ content_tags field is not editable by the current user. Link to comment Share on other sites More sharing options...
da² Posted October 1 Share Posted October 1 Oh OK you are talking about permissions, I thought it was about the field "view" option (open, closed, open not editable...). 🙂 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