1

I managed to make an entry type that won't let me create any entries.

The site is for a rock band, and the entry type in question is called "Shows". It contains five fields:

  • Venue
  • Lineup
  • Date
  • Time
  • Body

Date and Time are Date/Time fields, Lineup and Body are standard text fields. Venue is the field that I think is causing the trouble.

Venue is an Entries field that lets you select one from several venues, which are entries in a separate section (a Structure) called Venues.

When I enter data for a show, I'm able to select from the list of venues. When I try to save the Show, however, I get an error:

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

The most meaningful looking highlighted line in the debug output is:

echo $this->getAttribute((isset($context["object"]) ? $context["object"] : $this->getContext($context, "object")), "venue", array());

All these pages are in the admin section, not in my templates, thus they are pages I can't edit, so I don't know what I can do to fix this error. Is it possible that I have created an entry type that Craft can't handle? There doesn't seem to be anything that unusual about the Venue entry type.

Have I done something obviously wrong? If not, how do I debug this?

  • Are you on the latest version of Craft? Try manually replacing your craft/app folder to ensure you don't have any missing or corrupt files. – Lindsey D Jul 5 '17 at 0:17
  • Thanks for the suggestion, but manually replacing the app folder with the latest version (and clearing cache and reloading) made no change. – David Rhoden Jul 5 '17 at 0:32
  • The file causing the error is in /craft/storage/runtime/compiled_templates/. Can I force Craft to recompile templates somehow? – David Rhoden Jul 5 '17 at 0:38
  • You can safely delete the entire contents of the runtime folder. Not sure if that'll make a difference though. – Lindsey D Jul 5 '17 at 0:46
  • If I click "Show the Title field" on, the admin page operates correctly. I really didn't want it on but I guess I'm formatting the Craft-generated title fields incorrectly, and that's where the error is really coming from. – David Rhoden Jul 5 '17 at 0:48
2

It sounds like you're trying to have a dynamic title for that section, based on an Entries field (Venue). Working out the nuances in this situation can be a little tricky, but I think this would do the trick for you:

{{ object.venue.first().title ?? '(no venue)' }}

Let's break down what's happening here...

In this use-case, object is your Entry. Which means that object.venue is your Venue field (which happens to be an Entries field type).

Since Venue is an Entries field type, that means it's going to return an Element Criteria Model. When dealing with an ECM, you need to append something like find() (get all) or first() (get one) to end up with an actual, useful result.

Once we've established that you only need a single Entry (object.venue.first()), let's go ahead and grab the title of that Entry (object.venue.first().title). The good news... that can all be chained together! The bad news... it'll throw an error if the Venue field is left empty.

Which is where ?? '(no venue)' comes in. The double question mark is called a null-coalescing operator. It provides a graceful fallback, suppressing error messages and allowing you to provide a safe alternative.

Put it all together, and you'll have a nice, safe dynamic title.

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.