Feature toggles
TYPO3 provides an API class for creating so-called "feature toggles". Feature toggles provide an easy way to add new implementations of features next to their legacy version. By using a feature toggle, the integrator or site administrator can decide when to switch to the new feature.
The API checks against a system-wide option array within
$GLOBALS
which an integrator or
admininistrator can set in the config/
file. Both
TYPO3 Core and extensions can provide alternative functionality for a certain
feature.
Examples for features are:
- Throw exceptions in new code instead of just returning a string message as error message.
- Disable obsolete functionality which might still be used, but slows down the system.
- Enable alternative "page not found" handling for an installation.
Table of Contents
Naming of feature toggles
Feature names should NEVER be named "enable" or have a negation, or contain versions or years. It is recommended to use "lowerCamelCase" notation for the feature names.
Bad examples:
enable
Feature Xyz disable
Overlays scheduler
Revamped2018 use
Doctrine Queries disable
Prepared Statements disable
Hooks In FE
Good examples:
extended
Richtext Format native
Yaml Parser inline
Page Translations typo
Script Parser Includes As Xml native
Doctrine Queries
Using the API as extension author
For extension authors, the API can be used for any custom feature provided by an extension.
To register a feature and set the default state, add the following to the
ext_
file of your extension:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['myFeatureName'] ??= true; // or false;
To check if a feature is enabled, use this code:
use TYPO3\CMS\Core\Configuration\Features;
final class SomeClass {
public function __construct(
private readonly Features $features,
) {
}
public function doSomething(): void
{
if ($this->features->isFeatureEnabled('myFeatureName') {
// do custom processing
}
// ...
}
}
Attention
Currently, only the Core features can be (de-)activated in the Install Tool.
To change the setting for your extension feature either use
config/
or config/
files like:
$GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['myFeatureName'] = true;
The name can be any arbitrary string, but an extension author should prefix the feature with the extension name as the features are global switches which otherwise might lead to naming conflicts.
Core feature toggles
Some examples for feature toggles in the TYPO3 Core:
redirects.
: Enables hit statistics in the redirects backend modulehit Count security.
: If on, HTTP referrer headers are enforced for backend and install tool requests to mitigate potential same-site request forgery attacks.backend. enforce Referrer
Enable / disable feature toggle
Features can be toggled in the Admin Tools > Settings module via Feature Toggles:
Internally, the changes are written to config/
:
'SYS' => [
'features' => [
'redirects.hitCount' => true,
],
]
Note
New in version 12.1
If the config/
file is write-protected an info
box is rendered. In that case, all input fields are disabled and the save
button is not available.
Feature toggles in TypoScript
One can check whether a feature is enabled in TypoScript with the function
feature
:
[feature("unifiedPageTranslationHandling")]
# This condition matches if the feature toggle "unifiedPageTranslationHandling" is true
[END]
Feature toggles in Fluid
New in version 13.2
A new condition-based Fluid ViewHelper was added. It allows integrators to check for feature flags from within Fluid templates.
The Feature ViewHelper <f:feature> can be used to check for a feature in a Fluid template:
<f:feature name="unifiedPageTranslationHandling">
This is being shown if the flag is enabled
</f:feature>