Skip to content

Commit

Permalink
[TASK] Check request object in conditions (#808)
Browse files Browse the repository at this point in the history
Related: https://typo3.slack.com/archives/C025BQLFA/p1695726759085809
Releases: main, 12.4, 11.5

Co-authored-by: Chris Müller <[email protected]>
  • Loading branch information
github-actions[bot] and brotkrueml authored Sep 30, 2023
1 parent dc70350 commit ffb22a2
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions Documentation/Conditions/Index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -779,8 +779,16 @@ traverse()
:caption: EXT:site_package/Configuration/TypoScript/setup.typoscript
# Traverse query parameters of current request along tx_news_pi1[news]
[traverse(request.getQueryParams(), 'tx_news_pi1/news') > 0]
[request && traverse(request.getQueryParams(), 'tx_news_pi1/news') > 0]
.. tip::
Checking for the :ref:`request object <t3coreapi:typo3-request>` to be
available before using :typoscript:`traverse()` may be necessary, for
example, when using :ref:`Extbase <t3coreapi:extbase>` repositories in
:ref:`CLI <t3coreapi:symfony-console-commands>` context (as Extbase
depends on TypoScript and on the command line is no request object
available). This avoids the error
`Unable to call method "getQueryParams" of non-object "request"`.

.. index:: Conditions; compatVersion
.. _condition-function-compatVersion:
Expand Down Expand Up @@ -1087,6 +1095,15 @@ request()
:aspect:`Description`
Allows to fetch information from current request.

.. tip::
Checking for the :ref:`request object <t3coreapi:typo3-request>` before
using in a condition may be necessary, for example, when using
:ref:`Extbase <t3coreapi:extbase>` repositories in
:ref:`CLI <t3coreapi:symfony-console-commands>` context (as Extbase
depends on TypoScript and on the command line is no request object
available). This avoids, for example, the error
`Unable to call method "getQueryParams" of non-object "request"`.


.. index:: Conditions; request.getQueryParams()
.. _condition-function-request-getQueryParams():
Expand Down Expand Up @@ -1126,7 +1143,7 @@ request.getQueryParams()
# Safely check the query parameter array to avoid error logs in case key
# is not defined. This will check if the GET parameter
# tx_news_pi1[news] in the URL is greater than 0:
[traverse(request.getQueryParams(), 'tx_news_pi1/news') > 0]
[request && traverse(request.getQueryParams(), 'tx_news_pi1/news') > 0]
.. index:: Conditions; request.getParsedBody()
Expand All @@ -1152,7 +1169,7 @@ request.getParsedBody()
.. code-block:: typoscript
:caption: EXT:site_package/Configuration/TypoScript/setup.typoscript
[request.getParsedBody()['foo'] == 1]
[request && request.getParsedBody()['foo'] == 1]
.. index:: Conditions; request.getHeaders()
Expand All @@ -1177,11 +1194,11 @@ request.getHeaders()
.. code-block:: typoscript
:caption: EXT:site_package/Configuration/TypoScript/setup.typoscript
[request.getHeaders()['Accept'] == 'json']
[request && request.getHeaders()['Accept'] == 'json']
page.10.value = Accepts json
[END]
[request.getHeaders()['host'][0] == 'www.example.org']
[request && request.getHeaders()['host'][0] == 'www.example.org']
page.20.value = The host is www.example.org
[END]
Expand All @@ -1208,7 +1225,7 @@ request.getCookieParams()
.. code-block:: typoscript
:caption: EXT:site_package/Configuration/TypoScript/setup.typoscript
[request.getCookieParams()['foo'] == 1]
[request && request.getCookieParams()['foo'] == 1]
.. index:: Conditions; request.getNormalizedParams()
Expand Down Expand Up @@ -1236,11 +1253,11 @@ request.getNormalizedParams()
.. code-block:: typoscript
:caption: EXT:site_package/Configuration/TypoScript/setup.typoscript
[request.getNormalizedParams().isHttps()]
[request && request.getNormalizedParams().isHttps()]
page.10.value = HTTPS is being used
[END]
[request.getNormalizedParams().getHttpHost() == "example.org"]
[request && request.getNormalizedParams().getHttpHost() == "example.org"]
page.10.value = The host is "example.org"
[END]
Expand Down Expand Up @@ -1269,10 +1286,10 @@ request.getPageArguments()
.. code-block:: typoscript
:caption: EXT:site_package/Configuration/TypoScript/setup.typoscript
[request.getPageArguments().get('foo_id') > 0]
[request && request.getPageArguments().get('foo_id') > 0]
# True, if current page type is 98
[request.getPageArguments()?.getPageType() == 98]
[request && request.getPageArguments()?.getPageType() == 98]
.. index:: Conditions; session
Expand Down

0 comments on commit ffb22a2

Please sign in to comment.