netresearch / contexts
Multi-channel content visibility for TYPO3 - by Netresearch
Installs: 15 465
Dependents: 1
Suggesters: 0
Security: 0
Stars: 11
Watchers: 12
Forks: 11
Open Issues: 1
Type:typo3-cms-extension
pkg:composer/netresearch/contexts
Requires
- ext-pdo: *
- typo3/cms-backend: ^11.5
- typo3/cms-core: ^11.5
- typo3/cms-extbase: ^11.5
- typo3/cms-extensionmanager: ^11.5
- typo3/cms-frontend: ^11.5
Requires (Dev)
- phpro/grumphp: ^1.13.0
- phpstan/phpstan: ^1.10
- phpstan/phpstan-deprecation-rules: ^1.1
- phpstan/phpstan-phpunit: ^1.3
- phpstan/phpstan-strict-rules: ^1.5
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.7 || ^4.0
- ssch/typo3-rector: ^2.0.3
README
Multi-channel Contexts
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.
Requirements
| Version | TYPO3 | PHP |
|---|---|---|
| 4.x | 12.4, 13.4 | 8.2 - 8.4 |
| 3.x | 11.5 | 7.4 - 8.1 |
Context Examples
- 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.
Table of Contents
Setup
- Install and activate extension
contexts - Clear TYPO3 cache
composer require netresearch/contexts vendor/bin/typo3 extension:activate contexts vendor/bin/typo3 cache:flush
Optional Extensions:
contexts_geolocation- Location-based rules (continent, country, area)contexts_wurfl- Device-based rules (phone, tablet, TV, screen sizes)
Creating and Using Contexts
Creating a Context
- 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
Using a Context
- 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.
Context Types
Domain
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)
GET Parameter
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
IP Address
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
HTTP Header
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
Session Variable
Match based on session data. Checks if a session variable with the given name exists.
Logical Combination
Combine multiple contexts with logical operators:
| Operator | Description |
|---|---|
&& |
Logical AND |
| ` | |
! |
Negation |
(...) |
Grouping |
Example: mobile && !tablet
Integration
Fluid Templates
<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>
TypoScript Conditions
[contextMatch("mobile")]
page.10.template = EXT:site/Resources/Private/Templates/Mobile.html
[END]
PHP API
use Netresearch\Contexts\Api\ContextMatcher; if (ContextMatcher::getInstance()->matches('mobile')) { // Mobile-specific logic }
Development
Testing
composer install # Run unit tests composer test:unit # Run functional tests (requires database) composer test:functional # Run with coverage composer test:coverage
Code Quality
# 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) |
Documentation
Full documentation available at docs.typo3.org.
License
This project is licensed under the AGPL-3.0-or-later.
Credits
Developed and maintained by Netresearch DTT GmbH.
Contributors: Andre Hähnel, Christian Opitz, Christian Weiske, Marian Pollzien, Rico Sonntag, Benni Mack, and others.