Josef Glatz
Josef Glatz

Reputation: 565

TypoScript applicationContext condition with wildcards or sub-contexts not possible

Since TYPO3 9.5 LTS suggests to use the Symfony Expression Language for TypoScript conditions.

I have some troubles migrating them:

Old Syntax:

// Matches any applicationContext with a rootContext of "Production", for example "Production/Live" or "Production/Staging":
[applicationContext = Development*]

// Matches any applicationContext starting with "Production/Dev"
[applicationContext = /^Production\/Dev/]

to the new expression language:

I've tried the following without success:

[applicationContext == "/^Development\/Docker/"]

[applicationContext == "Development*"]

[applicationContext == "Development/*"]

I didn't found any examples. I'm not sure, if the tests for conditions are already based on the expression language. (https://review.typo3.org/#/c/57787/)

Would be nice if someone has an advice how to use the feature to add conditions like before

Upvotes: 2

Views: 1618

Answers (4)

Masi
Masi

Reputation: 9

This regexp matches the exact string "Production" and any string that starts with "Production/", eg "Production/Staging".

Match an applicationContext is "Production" or is starting with "Production/"

[applicationContext matches '#^Production(/|$)#']

Upvotes: -1

Josef Glatz
Josef Glatz

Reputation: 565

The following condition matches any applicationContext starting with "Production/Dev"

[applicationContext matches '/^Production\\\\\\\\/Dev/']

Why so many backslashes? A backslash (\) must be escaped by 4 backslashes (\\\\) in a string and 8 backslashes (\\\\\\\\) in a regex in the Symfony Expression Language

(Symfony Expression Language acts as the base for TypoScript conditions since TYPO3 9 LTS)


EDIT 2020-02: Using a different regex delimiter makes the readability much better for applicationContext conditions within TYPO3:

Instead of / we can use # as regular expression delimiters.

Here are some examples:

Match the "Development" applicationContext

[applicationContext matches "#^Development#"]

Match an applicationContext starting with "Production/Dev"

[applicationContext matches "#^Production/Dev#"]

Match an applicationContext starting with "Production/Dev or Production/Staging"

[applicationContext matches "#^Production/(Dev|Staging)#"]

Match an applicationContext starting with "Production/Live

[applicationContext matches "#^Production/Live#"]

Match an applicationContext starting with "Production/Live and an active TYPO3 backend user session

[applicationContext matches "#^Production/Live#" && getTSFE().isBackendUserLoggedIn()]

Match any applicationContext except those starting with "Production"

[not (applicationContext matches "#Production#")]

Match an applicationContext with exactly the value "Production/Live/ClusterServer1"

[applicationContext == "Production/Live/ClusterServer1"]

Upvotes: 8

StatiX
StatiX

Reputation: 874

In TYPO3 9 you can do [applicationContext = Development/*]

Upvotes: -3

Mathias Brodala
Mathias Brodala

Reputation: 6460

You need to use the matches comparison operator of the Symfony Expression Language. This way you can use regular expressions for partial matches:

[applicationContext matches "/^Development/"]

This will match any Development context.

Upvotes: 6

Related Questions