- Issue created by @nicolas bouteille
- 🇷🇺Russia Chi
Formatted text field consists of two properties,
format
andvalue
. However, when you print something like{{ node.body.value }} for empty field Twig does not get the <code>value
property as you might expected. Instead it falls back to generalTypedDataInterface::getValue()
method that is supposed to return all field properties as array.I would suggest to check the emptiness of the field explicitly as follows.
{% if not node.body.isEmpty() %} {{ node.body.value|check_markup('restricted_html') }} {% endif %}
There is a general question for such cases. Should a library thrown an error when it is misused or try to handle mistakes silently.
I think, hiding such issues is a disservice. - Status changed to Closed: works as designed
about 1 year ago 3:58pm 24 October 2023 - 🇫🇷France nicolas bouteille
Hey! Thanks for the quick response :)
Ok I respect your opinion and I get that we are not on the same page on the subject ;)
The solution you suggest{% if not node.body.isEmpty() %} {{ node.body.value|check_markup('restricted_html') }} {% endif %}
is too heavy for my liking to be added everywhere in my code. I prefer my custom filter that handles it all in one line.
As far as throwing errors is concerned, my belief is that not being able to display one piece of information somewhere on a page should not break the whole page.
So for example if while developing you don't think about testing check_markup with an empty field, and someday in production there is an empty field, the page will be completely broken.
With the solution I came up with, only the field in error is missing from the page, but the rest of the page is ok. But the error is logged in the system, and I actually send myself an alert whenever a new error is logged, so I can know about it right away and investigate and solve it even without nobody telling me about the issue.