wanze / SeoMaestro

🧙‍♂️A ProcessWire module helping you to manage SEO related tasks like a boss.
MIT License
34 stars 9 forks source link

How to fallback to a custom Opengraph image #8

Closed wanze closed 5 years ago

wanze commented 5 years ago

Hi @wanze,

thank you … 1) I'll open an github issue 2) I have set up an exclusive images field (og_image). In the details tab @seo maestro field it is set as default for the opengraph image {og_image}. The images fieldtype offers an option to fetch a default value (tab details) from a specific page if the field is empty. Since I have a global site settings page, I would like to fetch a default og_image from that settings page (in case the content page og_image-field is emtpy).

When I fetch $page->og_image on a content page, it returns the default image from the settings-page. The seo maestro output in header however contains no image tag. I guess it's in your function getPageImage(), maybe it's related to the getUnformatted() call… that ignores the images-field options?

images-default-value

Originally posted by @esszett in https://github.com/wanze/SeoMaestro/issues/5#issuecomment-480803743

wanze commented 5 years ago

Hi @esszett,

I moved your question to a new issue to keep things organized a bit.

That is very interesting, I actually didn't know about the feature to select a default value for the an image field. I think you are right, the fallback might only work when output formatting is on. I will look how to support this scenario. Or maybe you found it out already? :) In the meantime, you could rebuild this logic by hooking into the rendering process of the meta data. There are two hooks available, I am not sure which one is easier to use, see the test cases here: https://github.com/wanze/SeoMaestro/blob/master/tests/src/SeoMaestroTest.php#L83-L132 In your hook, you would need to return the url of the default image, if no image has been specified. Hope that helps!

Cheers

esszett commented 5 years ago

Hello @wanze,

I didn't recognize the hookability… fantastico, that did it. Now I fetch the formatted output of the og_image field and processwire fetches the defined default values @field-settings. This hooking scenario also provides a solution for another usecase I recently had: inherit values from the next available parent in page tree. That's useful in a deep nested tree where you have topic nodes of which values should be reflected on their childpages.

Thank you!

My simple hook in ready.php

$this->addHookAfter('SeoMaestro::renderSeoDataValue', function (HookEvent $event) {
    $group = $event->arguments(0);
    $name = $event->arguments(1);
    $value = $event->arguments(2);
    $page = $event->wire('page');

    if ($group === 'opengraph' && $name === 'image') {
        if ($value !== "") return;

            //scenario 1: fetch default image assigned in field-settings (get formatted)
            $value = $page->og_image;

            //scenario 2: inherit from next upper parent-page in tree with assigned image (no setting for default value @og_image)
            // $selector = "og_image!=''";
            // $value = $page->og_image ?: $page->closest($selector)->og_image;

            $event->return = $value;        
    }
});
wanze commented 5 years ago

@esszett FYI, the fallback to the default page image is now respected by the module in version 0.7.0.