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 can we get the generated meta for a entry once we have the entry instance? #248

Closed
tdondich opened this issue Dec 22, 2022 · 7 comments

Comments

@tdondich
Copy link

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
Copy link

jpmaga commented Feb 13, 2023

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
Copy link

jpmaga commented Feb 13, 2023

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
Copy link

reppair commented Feb 13, 2023

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

@jpmaga
Copy link

jpmaga commented Feb 14, 2023

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
Copy link
Member

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
Copy link
Member

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 👍

@ssyberg
Copy link

ssyberg commented Nov 14, 2024

Whoa I'm super surprised this isn't supported (or in the REST API), very bummer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants