nystudio107 / craft-seomatic

SEOmatic facilitates modern SEO best practices & implementation for Craft CMS 3. It is a turnkey SEO system that is comprehensive, powerful, and flexible.
https://nystudio107.com/plugins/seomatic
Other
165 stars 70 forks source link

Fetching data via Element-API not working as expected (Same as SEO Titel) #1389

Closed ThomasBiniasch closed 10 months ago

ThomasBiniasch commented 10 months ago

Question

We are facing some strange behavior for the following setup:

on the General tab we set "SEO Title Source" to "Custom Text", activate the Override checkbox, and type in something like "My Seo Title"

on the Facebook tab we set "Facebook OpenGraph Title Source" to "Same as SEO Title" and activate the Override checkbox too.

Now if we try to retrieve the data via our element-api:

      /** @var MetaBundle $seoSettings */
      $seoSettings = $entry->seosettings;
      /** @var MetaSiteVars $siteVars */
      $siteVars = $seoSettings->metaGlobalVars;
      $siteVars->parseProperties();

the value for the ogTitle is not our seoTitle, instead it returns the fallback title configured in the settings.

The question is now: is this a bug? not supported? or do we have some issue in the way we try to retrieve the data?

Thank you in advance!

khalwat commented 10 months ago

Doing it this way isn't going to work, because it's not coalescing all of the various SEOmatic settings together. All you are doing here is looking at the values in the field itself, which is only a small part of the meta cascade:

https://nystudio107.com/docs/seomatic/overview.html#the-meta-cascade

Probably you need to do something like:

Seomatic::$plugin->helper->loadMetadataForUri($uri);

...first to make sure the correct meta information is loaded for all of the containers, and then look whatever you need to.

However, if you're already using the element API, is there a reason you're not just using the existing headless endpoint for such information?

https://nystudio107.com/docs/seomatic/advanced.html#headless-spa-api