Jump to content
Guy Incognito

Out of memory when calling httpUrl()

Recommended Posts

I've written a very basic hook script with the aim of updating one field with the value from another on page save. I want to get the full absolute URL of an image to populate an OpenGraph image tag field.

If I change the url() call on single_image to httpUrl() I get a server memory exhausted error. I'm certainly not a PHP wizard but I don't think this should be particularly taxing. Have I accidentally created a loop somewhere? Calling url() works fine.

Running ProcessWire 3.0.141.

<?php

//Hook page save to update Open Graph Image URL with featured image
$wire->addHookAfter("Pages::saved(template=article)", function ($event) {

    //Get which page has been saved
    $page = $event->arguments(0);

        //Check featured image is set
        if ($page->single_image) {
            
            $ogImg = $page->single_image->url;
            updatePage($page,$ogImg);

        } else {

            $ogImg = 'https://examplesite.co.uk/site/templates/images/default-og-img.jpg';
            updatePage($page,$ogImg);

        }

        //$message = "Open Graph image successfully generated";
        //$this->message($message);
        
});

function updatePage($page,$ogImg) {

    $page->seo_image = $ogImg;
    $page->save();

}

 

Share this post


Link to post
Share on other sites

I think, the problem is with $page->single_image being of type Pageimages, not Pageimage.

Inside hooks the $page object's output formatting is turned off. This means that even for image fields that are defined to hold only 1 image, the field is of type Pageimages, which is an array.

You can confirm this by doing var_dump($page->single_image) (or better using Tracydebugger bd($page->single_image)).

So to get to the actual image, you need to loop over the Pageimages array like

foreach($page->single_image as $image) {
	// your logic goes in here
}

 

  • Like 1

Share this post


Link to post
Share on other sites

Why in a hook and why saving the image to another field and so on?

Why not just checking it in the template and outputting different values for OG/SEO?

  • Like 1

Share this post


Link to post
Share on other sites
42 minutes ago, gebeer said:

I think, the problem is with $page->single_image being of type Pageimages, not Pageimage.

Inside hooks the $page object's output formatting is turned off. This means that even for image fields that are defined to hold only 1 image, the field is of type Pageimages, which is an array.

You can confirm this by doing var_dump($page->single_image) (or better using Tracydebugger bd($page->single_image)).

So to get to the actual image, you need to loop over the Pageimages array like


foreach($page->single_image as $image) {
	// your logic goes in here
}

 

I'm not getting the memory error now, I've changed the page save to target the specific field and this seems to hugely improve performance.

Pageimages being an array makes sense, but looping through the image field isn't working either. In actual fact when I start logging the output on save as I go it just seems httpUrl() isn't returning anything whereas url() does.

32 minutes ago, wbmnfktr said:

Why in a hook and why saving the image to another field and so on?

Why not just checking it in the template and outputting different values for OG/SEO?

It's because the site is running markupSEO module to handle OG and meta on all other pages. Only on this specific page type I just want to auto populate the OG image field so they can go on using the rest of the module functionality elsewhere.

Share this post


Link to post
Share on other sites

Ok got it - this works!

$ogImg = $page->single_image->first()->httpUrl;

So as you said @gebeer it's something to do with the images being stored in an array, but no idea why looping through them manually wasn't working!

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

×
×
  • Create New...