vaersaagod/seomate

[FR] Add support for pulling values from a nested entry in a CKEditor field (similar to Matrix sub fields)

Closed this issue · 10 comments

Now that CKEditor has baked in support for nested entries which are full-blown elements, similar to the erstwhile Matrix blocks, it'll be useful to specify using field values from nested entries for the SEO values.

Eg: We have a CKEditor field body which supports a few nested entry types (image, video, etc) and we’d like to pull in the first image from the body field to be used as the SEO image. Sticking with the existing matrix field syntax, this could perhaps be expressed as:

'image' => ['seoImage', 'mainImage', 'body.image:assetField']

What do you think?

Maybe we'll add support for CKEditor entries, have to give it some thought. In the meantime, you should be able to pull it off using either object templates or closures; support for both of which was added to SEOmate 3 for Craft 5. Check out the docs for fieldProfiles for more info!

@mmikkel Sweet! This will certainly do for now. I completely missed these new features and didn’t realise the Craft 5 docs/release were on a different branch. 🙈

…yeah, we should probably move SEOmate 3.0 out of beta at this point 🙃

@mmikkel So what is the expected value that the object template / closure should return for the 'image' key — an Asset object, or an image URL?

@mmikkel So what is the expected value that the object template / closure should return for the 'image' key — an Asset object, or an image URL?

For 'image' it should be an asset, which means that object templates aren’t actually viable for that meta key (a closure returning an asset or null will work fine, though).

Perhaps it’d be useful if we made it possible to output an asset ID from an object template, for the 'image' meta key 🤔 I also realize we should probably document these features in a bit more detail, with a few more examples.

I just tagged SEOMate v. 3.0.0-beta.7 with that change – for image meta tags, object templates can now be used to render an asset ID that SEOMate will use to query for the actual image. Here's how that could look:

'fieldProfiles' => [
    'default' => [
        'image' => ['{seoImage.one().id}'],
    ],
],

...the above isn't necessarily something I'd recommend doing (for images, field handle references or closures will a better approach in almost all cases) – but at least this makes object templates not completely unusable for image tags, which admittedly felt a bit awkward.

Also took the time to update the docs with a few more examples re: object templates and closures, for the defaultMeta and fieldProfiles config settings!

Looks good @mmikkel, thanks! I ended up going with a closure and figured that it would expect an asset object. Perhaps you could make the object template return either an asset ID or an image URL? (I feel like the image URL return also worked out-of-the-box in beta.6, probably due to Imager accepting both assets and URLs as inputs.)

Perhaps you could make the object template return either an asset ID or an image URL

Imager would treat that URL as an external image, which could open a new can of worms. More importantly, Craft core isn't able to transform images by URL, so having that as an option at all, would feel kinda squishy to me.

Makes sense!

Also, you're right that using a URL + Imager probably worked in beta.6 – that was not intended, but was due to value sanitation missing for object template and closure generated values; I fixed that as part of beta.7 :)