Jump to content

Adding multiple pages to page reference field within a foreach php


Liam88
 Share

Recommended Posts

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 by Liam88
Link to comment
Share on other sites

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 by Jan Romero
  • Like 2
Link to comment
Share on other sites

Two things I would try and check:

  1. double check your page reference field allows the creation of new pages and all settings are correct - including parent and template settings
  2. try to save after the tag was added.
    Either with
        $page->save();
    or
        $page->save('your_field_name')
  • Like 2
Link to comment
Share on other sites

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

15 minutes ago, wbmnfktr said:

Two things I would try and check:

  1. double check your page reference field allows the creation of new pages and all settings are correct - including parent and template settings
  2. 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

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().

  • 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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...