Exception message when trying to print an object without __toString() in a Twig template is not helpful

Created on 29 June 2013, almost 12 years ago
Updated 23 April 2025, 2 days ago

@Berdir mentioned this issue to me today on IRC.

Relevant code from twig.engine:

  if (is_object($arg)) {
    if (method_exists($arg, '__toString')) {
      return (string) $arg;
    }
    throw new Exception(t('Object of type "@class" cannot be printed.', array('@class' => get_class($arg))));
  }

If possible we should mention the variable name as well as the template where this is being called from - that would be a big improvement.

🐛 Bug report
Status

Needs work

Version

11.0 🔥

Component

theme system

Created by

🇨🇦Canada star-szr

Live updates comments and jobs are added and updated live.
  • Needs reroll

    The patch will have to be re-rolled with new suggestions/changes described in the comments in the issue.

  • Needs issue summary update

    Issue summaries save everyone time if they are kept up-to-date. See Update issue summary task instructions.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024