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

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