Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to fallback to a custom Opengraph image #8

Closed
wanze opened this issue Apr 19, 2019 · 3 comments
Closed

How to fallback to a custom Opengraph image #8

wanze opened this issue Apr 19, 2019 · 3 comments
Labels
enhancement New feature or request question Further information is requested

Comments

@wanze
Copy link
Owner

wanze commented Apr 19, 2019

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 #5 (comment)

@wanze
Copy link
Owner Author

wanze commented Apr 19, 2019

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

@wanze wanze added enhancement New feature or request question Further information is requested labels Apr 19, 2019
@esszett
Copy link

esszett commented Apr 25, 2019

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 added a commit that referenced this issue Jun 1, 2019
If the opengraph image is empty and references an existing image field using a default value, the image of the default
page is now substituted for the opengraph image. See issue #8 for additional details.
@wanze
Copy link
Owner Author

wanze commented Jun 1, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants