Jump to content

Out of memory when calling httpUrl()


JayGee
 Share

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

}

 

Link to comment
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
Link to comment
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.

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