After running Craft update I'm getting error on the meta author, meta description and meta keywords fields. Any idea how to resolve issue?

Recoverable error
Object of class Craft\ElementCriteriaModel could not be converted to string

Error Output: enter image description here

Template Code:

{% set site_title = 'site title here' %}

    Not all pages have entries associated with them, e.g. search results, login page, etc.
{% if entry.id is not defined %}
    {% set entry = craft.entries.id(2) %}
{% endif %}

   Used as a fallback for when meta values don't exist in this entry
{% set homepage_entry = craft.entries.id(2) %}

<!doctype html>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>{{ entry.id == '2' ? site_title : entry.title ~ ' > ' ~ site_title }}</title>
    <meta name="author" content="{{ entry.metaAuthor ? entry.metaAuthor : homepage_entry.metaAuthor }}" />
    <meta name="description" content="{{ entry.metaDescription ? entry.metaDescription : homepage_entry.metaDescription }}" />
    <meta name="keywords" content="{{ entry.metaKeywords ? entry.metaKeywords|join(',') : homepage_entry.metaKeywords|join(',') }}" />
    <link rel="shortcut icon" href="images/favicon.ico" />
    <link rel="stylesheet" type="text/css" href="/css/all.css">
    <link rel="stylesheet" type="text/css" href="/css/nav.css">

Any idea what issue is, how to resolve and why this broke?

  • Is metaAuthor an entries field? – Brad Bell Dec 1 '17 at 22:30
  • @BradBell, yes, it is. It was working fine before I updated to latest ver of Craft. – frshjb373 Dec 4 '17 at 18:54
  • 1
    You don't fetch the entries. You just prepare queries but you have to execute them with .first() – Robin Schambach Dec 21 '17 at 21:50
  • yes, that did the trick. – frshjb373 Dec 22 '17 at 18:39

Try something like this:

{% set author = entry.metaAuthor|length ? entry.metaAuthor[0] : homepage_entry.metaAuthor[0] }}

<meta name="author" content="{{ author }}" />
  • Hey Brad just a quick question. I know the ElementCriteriaModel has an iterable interface, that's why we can loop it to get the entries (by the way, why did you remove that in Craft 3?) but as far as I know the length filter does not change the type of the object (just caches element) and it stays an object. Could you please explain why this is possible since you would pull the first index of an object in your code. I know models have a toString to get the title but how can you even fetch the first element? I just want to learn how that works php wise. Thank you very much – Robin Schambach Dec 5 '17 at 6:54
  • 1
    Calling |length, triggers ElementCriteriaModel's count() method, which calls total(), which ultimately calls the database and it's no longer an ElementCriteriaModel, but an array of results from the database. Relational fields (entries, users, categories, tags, etc.), will always return an array of results, even if their is only one selected, which is why metaAuthor[0] works. Of course, this is all pointless in Craft 3 as ElementCriteriaModel is no longer a thing, too. :) – Brad Bell Dec 6 '17 at 1:05
  • on Homepage getting error: Impossible to access a key ("0") on a string variable ("Author Name"). – frshjb373 Dec 7 '17 at 18:53
  • @frshjb373 Can you update your original question with the code you're using when you get that error? – Brad Bell Dec 7 '17 at 23:37
  • @BradBell, I have updated my question to include additional information. – frshjb373 Dec 21 '17 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.