Content visibility control for TYPO3 based on configurable contexts
Show and hide pages and content elements based on configurable "contexts". With the use of contexts, TYPO3 is able to do multichannel output.
| Version | TYPO3 | PHP |
|---|---|---|
| 4.x | 12.4, 13.4 | 8.2 - 8.4 |
| 3.x | 11.5 | 7.4 - 8.1 |
- User IP matches a given rule
- User entered website with GET-Parameter
affID=foo - Domain the user visits
- HTTP header values (User-Agent, Accept-Language, etc.)
- A session variable is set
- A combination of any other rules
With companion extensions:
- Screen size < 500px (
contexts_wurfl) - Browser runs on a tablet or mobile phone (
contexts_wurfl) - Location is 15km around a certain place (
contexts_geolocation) - User is from one of certain countries (
contexts_geolocation)
Apart from the context rules, this extension also provides an API to use contexts in your own extensions.
- Install and activate extension
contexts - Clear TYPO3 cache
composer require netresearch/contexts
vendor/bin/typo3 extension:activate contexts
vendor/bin/typo3 cache:flushOptional Extensions:
contexts_geolocation- Location-based rules (continent, country, area)contexts_wurfl- Device-based rules (phone, tablet, TV, screen sizes)
- Log into the TYPO3 backend as administrator
- Go to Web/List view, root page (ID 0)
- Create a new record: TYPO3 contexts → Context
- Configure:
- Title: e.g., "Affiliate ID: foo"
- Type: "GET parameter"
- Parameter name:
affID - Parameter value:
foo - Enable "Store result in user session"
- Save and close
- Go to Web/Page, select a page
- Edit a content element
- Select the "Contexts" tab
- For your context, select "Visible: yes"
- Save
The content element is now only visible when the context matches.
Match based on the accessed domain name.
- One domain per line
- Without leading dot: exact match only (
www.example.org≠example.org) - With leading dot: matches all subdomains (
.example.orgmatcheswww.example.org)
Match based on URL query parameters.
- Enable "Store result in user session" to persist across pages
- Leave value empty to match any non-empty parameter value
Match the user's IP address. IPv4 and IPv6 supported.
80.76.201.32 # Full address
80.76.201.32/27 # CIDR notation
FE80::/16 # IPv6 prefix
80.76.201.* # Wildcard
80.76.*.* # Multiple wildcards
Match HTTP request headers (User-Agent, Accept-Language, X-Forwarded-For, etc.)
- Enable "Store result in user session" to persist across pages
- Leave value empty to match any non-empty header value
Match based on session data. Checks if a session variable with the given name exists.
Combine multiple contexts with logical operators:
| Operator | Description |
|---|---|
&& |
Logical AND |
| ` | |
! |
Negation |
(...) |
Grouping |
Example: mobile && !tablet
<html xmlns:contexts="http://typo3.org/ns/Netresearch/Contexts/ViewHelpers">
<f:if condition="{contexts:matches(alias:'mobile')}">
<f:then>Mobile content</f:then>
<f:else>Desktop content</f:else>
</f:if>
</html>[contextMatch("mobile")]
page.10.template = EXT:site/Resources/Private/Templates/Mobile.html
[END]
use Netresearch\Contexts\Api\ContextMatcher;
if (ContextMatcher::getInstance()->matches('mobile')) {
// Mobile-specific logic
}composer install
# Run unit tests
composer test:unit
# Run functional tests (requires database)
composer test:functional
# Run with coverage
composer test:coverage# Static analysis (level 8)
composer analyze
# Code style check
composer lint
# Code style fix
composer lint:fix| Tool | Version | Purpose |
|---|---|---|
| PHPUnit | 10/11/12 | Unit and functional tests |
| PHPStan | 2.x | Static analysis (level 8) |
| PHP-CS-Fixer | 3.x | Code style (PSR-12) |
Full documentation available at docs.typo3.org.
This project is licensed under the AGPL-3.0-or-later.
Developed and maintained by Netresearch DTT GmbH.
Contributors: Andre Hähnel, Christian Opitz, Christian Weiske, Marian Pollzien, Rico Sonntag, Benni Mack, and others.