Project

General

Profile

Actions

Bug #88756

closed

TypoScript – Symfony Expressions: request.getQueryParams() should do an "is defined" validation

Added by Hagen Gebauer over 5 years ago. Updated over 1 year ago.

Status:
Rejected
Priority:
Should have
Assignee:
Category:
TypoScript
Target version:
-
Start date:
2019-07-15
Due date:
% Done:

0%

Estimated time:
TYPO3 Version:
9
PHP Version:
Tags:
symfony, expression,CodingNight
Complexity:
Is Regression:
Sprint Focus:

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.


Related issues 2 (0 open2 closed)

Related to TYPO3 Core - Task #89176: Provide better way to traverse array in TS conditionsClosedMarkus Klein2019-09-14

Actions
Is duplicate of TYPO3 Core - Bug #95781: PHP Warning: Undefined array key "uid" in /app/vendor/symfony/expression-language/Node/GetAttrNode.phpRejected2021-10-26

Actions
Actions #1

Updated by Frank Nägler over 5 years ago

  • Status changed from New to Accepted
  • Assignee set to Frank Nägler
Actions #2

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

Actions #3

Updated by Susanne Moog over 5 years ago

  • Tags changed from symfony, expression to symfony, expression,CodingNight
Actions #4

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

Actions #5

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.

Actions #6

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.

Actions #7

Updated by Markus Klein about 5 years ago

  • Related to Task #89176: Provide better way to traverse array in TS conditions added
Actions #8

Updated by Markus Klein about 5 years ago

Find my take on this in #89176

Actions #9

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
Actions #10

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

Actions

Also available in: Atom PDF