Asset fields left empty fail fatally
Bug description
Blueprint has an asset field which is optional. If left blank by the author, frontend throws 500 error.
TypeError
array_merge(): Argument #1 must be of type array, null given
Expected
To reference an asset variable and if it doesn’t exist for it to gracefully fail (eg: show nothing).
How to reproduce
Blueprint
fields:
-
handle: portrait
field:
type: assets
container: main
folder: /
restrict: false
max_files: 1
width: 50
display: 'Portrait'
_NB: Also tested with adding required: false
Template
{{ assets:portrait }}
<img ... >
{{ /assets:portrait }}
Content
Field not used and therefore portrait isn’t present with a value.
Logs
No response
Versions
Statamic 3.2.30 Solo Laravel 7.30.6 PHP 8.0.10 statamic/migrator dev-master
Installation
Fresh statamic/statamic site via CLI
Additional details
Caused by augmentation? Regression from the Antlers rewrite?
Workaround
Wrap {{ assets }} tag in a {{ if }} statement
Why are you using the assets tag? {{ portrait }} should work for you.
That's very good point! I've written them that way for years. Possibly an old convention or it is completely possible that I was wrong to ever do it that way. I was lucky in that it worked until now.
I've adjusted my templating to do it without the assets tag.
I defer to you all on whether assets should result in a 500 error if that variable doesn't exist.
I've just tested this again and can only reproduce when using the Assets tag ({{ assets:portrait }} instead of {{ portrait }}) and when using the Regex parser.
It doesn't fail when I switch to just looping through the field itself OR switch to the new Runtime parser.
Closing.
Apologies, this should have been closed back in Oct after @duncanmcclean's comment.