nystudio107 / seomatic

DEPRECATED A turnkey SEO implementation for Craft CMS 2.x that is comprehensive, powerful, and flexible
Other
357 stars 45 forks source link

Key "twitter" for array with keys "seoTitle, seoDescription, seoKeywords, seoImage, robots, canonicalUrl, og, breadcrumbs" does not exist. #314

Closed outline4 closed 6 years ago

outline4 commented 6 years ago

Hi,

I discovered that the following code will not work unless you have specified a twitter handle in the social media section of seomatic.

{% set metaTitle = entry.title ~ " Ingwerer Blog Eintrag" %}
{% set metaDescription = entry.cf_blog_matrix.type('text').first()|length ? entry.cf_blog_matrix.type('text').first().richText|split('</p>')|first|striptags : seomaticMeta.seoDescription %}
{% set metaImage = entry.cf_blog_matrix.type('image').first()|length ? entry.cf_blog_matrix.type('image').first().image.first() : null %}
{% set seomaticMeta = seomaticMeta|merge({
    'seoTitle': metaTitle,
    'seoDescription': metaDescription ?: seomaticMeta.seoDescription,
    'seoKeywords': seomaticMeta.seoKeywords,
    'seoImage': metaImage.url('landscape') ?? seomaticMeta.seoImage,
    'robots': seomaticMeta.robots,
    'canonicalUrl': seomaticMeta.canonicalUrl,
    'twitter': {
        'card': seomaticMeta.twitter.card,
        'site': seomaticMeta.twitter.site,
        'creator': seomaticMeta.twitter.creator,
        'title': metaTitle,
        'description': metaDescription ?: seomaticMeta.twitter.description,
        'image': metaImage.url('twoToOne') ?? seomaticMeta.twitter.image,
        'image:alt': entry.title ~ ' ' ~ gl_meta.cf_altText
    },
    'og': {
        'type': seomaticMeta.og.type,
        'locale': seomaticMeta.og.locale,
        'url': entry.url,
        'title': "Ingwerer Blog Eintrag " ~ entry.title,
        'description': metaDescription ?: seomaticMeta.og.description,
        'image': metaImage.url('landscape') ?? seomaticMeta.og.image,
        'image:secure_url': metaImage.url('landscape') ?? seomaticMeta.og.image,
        'image:width': metaImage.width('landscape') ?? seomaticMeta.og['image:width'],
        'image:height': metaImage.height('landscape') ?? seomaticMeta.og['image:height'],
        'site_name': seomaticMeta.og.site_name,
        'see_also': seomaticMeta.og.see_also
    },
    'breadcrumbs': seomaticMeta.breadcrumbs,
}) %}

This will output the following error:

Key "twitter" for array with keys "seoTitle, seoDescription, seoKeywords, seoImage, robots, canonicalUrl, og, breadcrumbs" does not exist. 

When I've specified a twitter handle everything works as expected.

Why exactly is a twitter handle required?
I am using this for these share buttons. you could share a blog entry on twitter without having a twitter handle, that should be possible, not?

Thanks for looking into this.
Stefan

khalwat commented 6 years ago

The Twitter handle is required because it does not attempt to generate any Twitter card information without it.

At one point in time, Twitter required the twitter:site tag, but it appears they don't anymore:

https://developer.twitter.com/en/docs/tweets/optimize-with-cards/overview/summary-card-with-large-image

Still, it's probably best practice to include it.

outline4 commented 6 years ago

But what do you do when you don't have a twitter account but still want the twitter card links?

Sorry, it might be obvious to you, but I am in a situation where the client does not have a twitter account, but still want's the blog links to be sharable in twitter.

The same situation goes for facebook. It's possible that you don't have an account but still want the links to be properly formatted by the sharer, not?

looking at the link you've provided, it does not seem that you need twitter:creator at all...