PHP API

Retrieving TSconfig settings

The PHP API to retrieve page and user TSconfig in a backend module can be used as follows:

EXT:my_sitepackage/Classes/Controller/MyBackendController.php
<?php

declare(strict_types=1);

use TYPO3\CMS\Backend\Utility\BackendUtility;
use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;

final class MyBackendController
{
    public function someMethod(int $currentPageId): void
    {
        // Retrieve user TSconfig of currently logged in user
        $userTsConfig = $this->getBackendUser()->getTSConfig();

        // Retrieve page TSconfig of the given page id
        $pageTsConfig = BackendUtility::getPagesTSconfig($currentPageId);
    }

    private function getBackendUser(): BackendUserAuthentication
    {
        return $GLOBALS['BE_USER'];
    }
}
Copied!

Both methods return the entire TSconfig as a PHP array. The former retrieves the user TSconfig while the latter retrieves the page TSconfig.

All imports, overrides, modifications, etc. are already resolved. This includes page TSconfig overrides by user TSconfig.

Similar to other TypoScript-related API methods, properties that contain sub-properties return their sub-properties using the property name with a trailing dot, while a single property is accessible by the property name itself. The example below gives more insight on this.

If accessing TSconfig arrays, the PHP null coalescing operator ?? is useful: TSconfig options may or not be set, accessing non-existent array keys in PHP would thus raise PHP notice level warnings.

Combining the array access with a fallback using ?? helps when accessing these optional array structures.

Incoming (user) TSconfig:
options.someToggle = 1
options.somePartWithSubToggles = foo
options.somePartWithSubToggles.aValue = bar
Copied!
Parsed array returned by getTSConfig(), note the dot if a property has sub keys:
<?php

return [
    'options.' => [
        'someToggle' => '1',
        'somePartWithSubToggles' => 'foo',
        'somePartWithSubToggles.' => [
            'aValue' => 'bar',
        ],
    ],
];
Copied!
Example how to read user TSconfig
<?php

declare(strict_types=1);

final class _MyBackendLoggedInController
{
    public function isUserToggleEnabled(): bool
    {
        // Typical call to retrieve a sanitized value:
        return $isToggleEnabled = (bool)($this->getUserTsConfig()['options.']['someToggle'] ?? false);
    }

    public function getSomePartWithSubToggles(): array
    {
        // Retrieve a subset, note the dot at the end:
        return $this->getUserTsConfig()['options.']['somePartWithSubToggles.'] ?? [];
    }

    private function getUserTsConfig(): array
    {
        return $GLOBALS['BE_USER']->getTSConfig();
    }
}
Copied!

Changing or adding page TSconfig

The PSR-14 event ModifyLoadedPageTsConfigEvent is available to modify or add page TSconfig entries.