typo3typo3-13.x

How to use site settings in Page TSConfig?


I'd like to use a setting from my TYPO3 v13.4 site settings in Page TSConfig:

tx_loginlink.fe.loginPage = {$foo.bar}

But apparently that doesn't work. But maybe there is some kind of workaround?


Solution

  • Note: Unfortunately, the questioner made a typo when accessing the site settings. It turns out that access worked as described. Here's the description in the documentation:

    https://docs.typo3.org/m/typo3/reference-coreapi/main/en-us/ApiOverview/SiteHandling/SiteSettings.html#sitehandling-settings-access

    But if you need access to other Data in TSconfig you can use the below example to access data i.e. from an external source:

    With the EvaluateModifierFunctionEvent in the AbstractAstBuilder you can easily implement a custom function that you can call in the TSconfig.

    https://github.com/TYPO3/typo3/blob/2597f1b19692cb94596c6423d97f302981ec5abb/typo3/sysext/core/Classes/TypoScript/AST/AbstractAstBuilder.php#L273

    For that register an EventListener in:
    your_extension/Configuration/Services.yaml

    VENDOR\YourExtension\EventListener\EvaluateValueModifier:
      tags:
        - name: event.listener
          event: TYPO3\CMS\Core\TypoScript\AST\Event\EvaluateModifierFunctionEvent
          identifier: 'vendor-your_extension-evaluate-value-modifier'
    

    And implement the following Eventlistener:
    your_extension/Classes/EventListener/EvaluateValueModifier.php

    <?php
    namespace VENDOR\YourExtension\EventListener;
    
    use TYPO3\CMS\Core\Site\SiteFinder;
    use TYPO3\CMS\Core\TypoScript\AST\Event\EvaluateModifierFunctionEvent;
    use TYPO3\CMS\Core\Utility\GeneralUtility;
    
    class EvaluateValueModifier
    {
        public function __invoke(EvaluateModifierFunctionEvent $event): void
        {
            $functionName = $event->getFunctionName();
            if($functionName == 'SiteSettings' || $functionName == 'SiteConfig')
            {
                $functionArguments = explode(':', $event->getFunctionArgument());
                $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
                $site = $siteFinder->getSiteByIdentifier($functionArguments[0]);
                if($functionName == 'SiteConfig')
                {
                    $configuration = $site->getConfiguration();
                    $event->setValue((string)$configuration[$functionArguments[1]]);
                }
                if($functionName == 'SiteSettings')
                {
                    $settings = $site->getSettings();
                    $event->setValue((string)$settings->get($functionArguments[1]));
                }
            }
        }
    }
    

    Let's now assume you have a Sites config in:
    config/sites/your-site/config.yaml

    base: 'https://www.mycoolwebsite.de'
    baseVariants:
      ....
      ....
    websiteTitle: 'My cool website'
    

    And let's further assume you have some Site settings in:
    config/sites/your-site/settings.yaml

    foo:
      bar: 123
    

    Then you can access these configs or settings in TSconfig like:

    tx_loginlink.fe.websitetitle := SiteConfig(your-site:websiteTitle)
    tx_loginlink.fe.loginPage := SiteSettings(your-site:foo.bar)
    

    For production use you should maybe add some error-handling to the eventlistener.