tractorcow/silverstripe-opengraph

Issues with 3.1 and override on custom pagetype

Closed this issue · 6 comments

Tried to override default image with this code on custom page type:

private function getOGImage() {
    $imgs = array();
        if ($this->Images()) {
        $images = $this->Images();

        foreach ($images as $value) {
            $og_img = $value->CroppedImage(600, 200);
            $imgs[] = $og_img->Url;
        }
    }
    if(empty($imgs)){
        $imgs[] = Director::absoluteURL(OpenGraphPageExtension::$default_image);
    }
    return $imgs;
}

But does not seem to work. Any suggestions?

Probably because it's a private function. :) Make it public and it should work fine.

Did not help, either. It gets and uses default image fine, but not the image inside $content.

@mediaclinic you could do something like this to return the images from content

public function getOGImage() {
        $images = array();
        $dom = new DOMDocument();
        $dom->loadHTML($this->data()->Content);
        $contentImages = $dom->getElementsByTagName("img");
        foreach($contentImages as $image) {
            $images[] = Director::absoluteBaseURL() . $image->getAttribute("src");
        }
        if(empty($images)){
            $images[] = Director::absoluteURL(OpenGraphPageExtension::$default_image);
        }
        return $images;
}

I'm not sure exactly what you were trying to do with $content images, but as @stevie-mayhew said you'd need to extract them via parsing (or, more easily, simply maintain a has_many relation of images).

getOGImage can also return literal Image objects if you want the module to generate the width and height tags for you as well. :)

Yep, this @stevie-mayhew example works great in this case!

I'm going to close this for now then. :) Let me know if you get stuck on anything else.