TYPO3 Edit $GLOBALS at runtime in controller

75 Views Asked by At

Since TYPO3 itself does not support custom feature toggles in the backend, I wanted to build my own implementation for this. I looked at the ConfigurationManager.php and it says in the comment at the top: 'This class is intended for internal core use ONLY.

  • Extensions should usually use the resulting $GLOBALS['TYPO3_CONF_VARS'] array,
  • do not try to modify settings in LocalConfiguration.php with an extension.'

But if I now try to edit the globals in my action: $GLOBALS['TYPO3_CONF_VARS']['SYS']['features'][$feature] = !$GLOBALS['TYPO3_CONF_VARS']['SYS']['features'][$feature]; The value only changes for a short time and after I reload the page, the value is reset again.

Am I missing something or is there another value I could go for?

2

There are 2 best solutions below

0
On

I made my own feature toggle in this way.

An on/off switch as an extension configuration in ext_conf_template.txt

# cat=feature toggle/enable/10; type=boolean; label=use feature
myFeature = 0

Save it, ext_localconf.php

// Register feature toggle
if (!isset($GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['myFeature'])) {
    $myFeature = GeneralUtility::makeInstance(ExtensionConfiguration::class)
        ->get('myext', 'myFeature');
    if ($myFeature) {
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['myFeature'] = true;
    } else {
        $GLOBALS['TYPO3_CONF_VARS']['SYS']['features']['myFeature'] = false;
    }
}

Use it in controller

    if (GeneralUtility::makeInstance(Features::class)->isFeatureEnabled('myFeature')) {
        $this->view->assign('myFeature', true);
    }

In templates

<f:if condition="{myFeature} == true">
0
On

Edit Your typoscript with this

plugin.tx_myextension {
    settings {
        enableFeature = 1
    }
}

yourfile.php

    $featureToggle = $GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_myextension.']['settings.']['enableFeature'];

// ext_localconf.php
$featureToggle = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ExtensionConfiguration::class)
    ->get('myextension')['enableFeature'];

if ($featureToggle) {
    // Your feature is enabled
} else {
    // Your feature is disabled
}