Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.0.2]: Error outputting value of money field in templates #11255

Closed
richhayler opened this issue May 18, 2022 · 1 comment
Closed

[4.0.2]: Error outputting value of money field in templates #11255

richhayler opened this issue May 18, 2022 · 1 comment

Comments

@richhayler
Copy link

richhayler commented May 18, 2022

What happened?

Description

I have a field named 'price' that uses the new 'money' field type. When attempting to display the value of the field in a template I get an error "Object of class Money\Money could not be converted to string"

According to the documentation I should be able to output the value of the field price as{{ entry.price }}
https://craftcms.com/docs/4.x/money-fields.html

Steps to reproduce

  1. Create a new money field, add to an entry type for a section.
  2. Create a new entry, add a value into the money field.
  3. Add the code to output the value in your template

In my case I have a channel section with the handle "artworks" and an entry type that has a money field with the handle "price"

{% set entries = craft.entries()
	.section('artworks')
	.all() %}

{% for entry in entries %}
	{{ entry.price }}
{% endfor %}

Expected behavior

The value saved to the price field for the entry should be displayed.

Actual behavior

Stack Trace

An Error occurred while handling another error:
Error: Object of class Money\Money could not be converted to string in craft/storage/runtime/compiled_templates/1b/...
Stack trace:
#0 craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_a37c2bc4cf7233594390eac0698f6c9c->doDisplay()
#1 craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling()
#2 craft/vendor/twig/twig/src/Template.php(379): Twig\Template->display()
#3 craft/vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render()
#4 craft/vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render()
#5 craft/vendor/craftcms/cms/src/web/View.php(414): Twig\Environment->render()
#6 craft/vendor/craftcms/cms/src/web/View.php(467): craft\web\View->renderTemplate()
#7 craft/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(52): craft\web\View->renderPageTemplate()
#8 craft/vendor/yiisoft/yii2/web/Response.php(1095): craft\web\TemplateResponseFormatter->format()
#9 craft/vendor/craftcms/cms/src/web/Response.php(275): yii\web\Response->prepare()
#10 craft/vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#11 craft/vendor/yiisoft/yii2/web/ErrorHandler.php(136): yii\web\Response->send()
#12 craft/vendor/craftcms/cms/src/web/ErrorHandler.php(185): yii\web\ErrorHandler->renderException()
#13 craft/vendor/yiisoft/yii2/base/ErrorHandler.php(135): craft\web\ErrorHandler->renderException()
#14 craft/vendor/craftcms/cms/src/web/ErrorHandler.php(64): yii\base\ErrorHandler->handleException()
#15 [internal function]: craft\web\ErrorHandler->handleException()
#16 {main}
Previous exception:
Error: Object of class Money\Money could not be converted to string in craft/storage/runtime/compiled_templates/1b/...
Stack trace:
#0 craft/vendor/twig/twig/src/Template.php(394): __TwigTemplate_a37c2bc4cf7233594390eac0698f6c9c->doDisplay()
#1 craft/vendor/twig/twig/src/Template.php(367): Twig\Template->displayWithErrorHandling()
#2 craft/vendor/twig/twig/src/Template.php(379): Twig\Template->display()
#3 craft/vendor/twig/twig/src/TemplateWrapper.php(40): Twig\Template->render()
#4 craft/vendor/twig/twig/src/Environment.php(277): Twig\TemplateWrapper->render()
#5 craft/vendor/craftcms/cms/src/web/View.php(414): Twig\Environment->render()
#6 craft/vendor/craftcms/cms/src/web/View.php(467): craft\web\View->renderTemplate()
#7 craft/vendor/craftcms/cms/src/web/TemplateResponseFormatter.php(52): craft\web\View->renderPageTemplate()
#8 craft/vendor/yiisoft/yii2/web/Response.php(1095): craft\web\TemplateResponseFormatter->format()
#9 craft/vendor/craftcms/cms/src/web/Response.php(275): yii\web\Response->prepare()
#10 craft/vendor/yiisoft/yii2/web/Response.php(339): craft\web\Response->prepare()
#11 craft/vendor/yiisoft/yii2/base/Application.php(390): yii\web\Response->send()
#12 craft/web/index.php(19): yii\base\Application->run()
#13 {main}

Craft CMS version

4.0.2

PHP version

8.1.5

@richhayler richhayler changed the title [4.x]: Error outputting value of money field in templates [4.0.2]: Error outputting value of money field in templates May 18, 2022
@brandonkelly
Copy link
Member

This was a docs bug. Money field values are Money\Money objects, which don’t have a __toString() method so can’t be output directly.

Docs are now updated to show that their values can be output using the |money filter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants