Bug #88756
closedTypoScript – Symfony Expressions: request.getQueryParams() should do an "is defined" validation
0%
Description
[request.getQueryParams()['tx_news_pi1']['news'] > 0]
do something
[END]
This is working well – as long as the query parameter exists. If it doesn’t, the following error message will be written into the log file:
... {"expression":"request.getQueryParams()['tx_news_pi1']['news'] > 0","exception":"RuntimeException: Unable to get an item on a non-array. in ...
I have tried changing the condition as follows, hoping that it would be interpreted as “is defined” – but the results are exactly the same:
[request.getQueryParams()['tx_news_pi1']['news']]
In my view there should be an automated PHP isset() validation for such conditions. Or at least a way to actively check for “is defined
” as you can do in Symfony’s Twig templating engine for example. I couldn’t find a similar way for TypoScript – please enlighten me if I just missed it.
Updated by Frank Nägler over 5 years ago
- Status changed from New to Accepted
- Assignee set to Frank Nägler
Updated by Gerrit Code Review over 5 years ago
- Status changed from Accepted to Under Review
Patch set 1 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/61315
Updated by Susanne Moog over 5 years ago
- Tags changed from symfony, expression to symfony, expression,CodingNight
Updated by Gerrit Code Review over 5 years ago
Patch set 2 for branch master of project Packages/TYPO3.CMS has been pushed to the review server.
It is available at https://review.typo3.org/c/Packages/TYPO3.CMS/+/61315
Updated by Frank Nägler over 5 years ago
- Status changed from Under Review to Rejected
If you build your condition like this you don't have an issue:
[request.getQueryParams()['tx_news_pi1'] && request.getQueryParams()['tx_news_pi1']['news'] > 0]
The first part will create a notice, but an exception will not occur.
Updated by Hagen Gebauer over 5 years ago
Frank Naegler wrote:
If you build your condition like this you don't have an issue:
[...]
The first part will create a notice, but an exception will not occur.
Thank you! This is working indeed – but to me it isn’t quite logical: why doesn’t the way I tried ([request.getQueryParams()['tx_news_pi1']['news']]
) work as well, when with dropping the ['news']
a proper is_defined
is being performed? Besides I wasn’t able to find documentation on that behaviour and the returned exception anywhere.
I still believe the is_defined
validation should be done automatically, considering Typoscript should be simplified compared to a proper programming language.
Updated by Markus Klein about 5 years ago
- Related to Task #89176: Provide better way to traverse array in TS conditions added
Updated by Sybille Peters over 1 year ago
- Is duplicate of Bug #95781: PHP Warning: Undefined array key "uid" in /app/vendor/symfony/expression-language/Node/GetAttrNode.php added
Updated by Sybille Peters over 1 year ago
The current best practice (as documented) is to use traverse in conditions with array elements (which might be uninitialized):
(also in the future, as this may change, please look at the documentation page for your TYPO3 version, use version switcher: https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Conditions/Index.html)
# before: [page["uid"] == 2] # after [traverse(page, "uid") == 2]
https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Conditions/Index.html#page
# before -[request.getQueryParams()['tx_news_pi1'] && request.getQueryParams() ['tx_news_pi1']['news'] > 0] # after +[traverse(request.getQueryParams(), 'tx_news_pi1/news') > 0]
https://docs.typo3.org/m/typo3/reference-typoscript/main/en-us/Conditions/Index.html#traverse