statamic / seo-pro

An all-in-one site reporting, metadata wrangling, Open Graph managing, Twitter card making, sitemap generating, turn-key addon for Statamic.
https://statamic.com/addons/statamic/seo-pro
53 stars 36 forks source link

How can we get the generated meta for a entry once we have the entry instance? #248

Closed tdondich closed 1 year ago

tdondich commented 1 year ago

It appears the only way to fetch the generated meta fields for an entry is by utilizing the tag in a view, but we wish to programatically fetch the generated meta fields.

Is there a way to do this? Once we have the Entry variable, is there a proper way to generate. All we can see is by using the tag, but it requires a context and when we're programatically working, we don't have a view context.

jpmaga commented 1 year ago

Came here looking for the same. This used to work:

$tags = new SeoProTags();
$tags->setContext($this->page);
return $tags->metaData();

But yesterday updated to the latest version and it doesn't seem to work anymore, just returns the entry provided as $this->page.

Haven't had the time to look much into it, and probably will not during the week, but if I do I'll let you know.

jpmaga commented 1 year ago

Turns out I needed to fix this sooner than expected. 😅

So, in my case the fix was just to wrap the entry like so:

$tags = new SeoProTags();
$tags->setContext($this->page->toAugmentedCollection());
return $tags->metaData();

On a quick inspection it doesn't seem to differ much from the previous function, but you could also do something like:

use Statamic\SeoPro\Cascade;
use Statamic\SeoPro\GetsSectionDefaults;
use Statamic\SeoPro\SiteDefaults;

class SEO {
    use GetsSectionDefaults;

    public funtion get($page) {
        return (new Cascade())
            ->with(SiteDefaults::load()->augmented())
            ->with($this->getAugmentedSectionDefaults($page))
            ->with($page->augmentedValue('seo')->value() ?? [])
            ->withCurrent($page)
            ->get();
    }
}
reppair commented 1 year ago

What would be $this and $this->page in the given example?

jpmaga commented 1 year ago

What would be $this and $this->page in the given example?

In the first code block $this->page is the entry instance. ($this->page = \Statamic\Facades\Entry::find("[id here]")). I use it inside a class, so I just copied and pasted it here, so $this is not really relevant for the example, just pass the instance as an augmented collection.

In the second code block, I suppose you are referencing to the method $this->getAugmentedSectionDefaults, that comes from the trait GetsSectionDefaults.

jesseleite commented 1 year ago

What would be $this and $this->page in the given example?

A 'page' in SEO Pro, is a collection entry or a taxonomy term, both of which can store seo data.

jesseleite commented 1 year ago

This is a good feature request. I created a more concise FR issue here, but feel free to use the above-mentioned workarounds until we have time to properly implement this 👍