How to get TYPO3 settings in the utility files?
Asked Answered
M

7

10
plugin.tx_xxx {
    setting {
        storagePid = 23
    } 
}

I want this TYPO3 settings in utility file. Please help me.

Magalimagallanes answered 15/6, 2015 at 7:50 Comment(0)
L
18

The above method works only in controller or services class try below it will work in any PHP files in Extension.

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\Extbase\\Object\\ObjectManager');
$configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
$extbaseFrameworkConfiguration = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
$storagePid = $extbaseFrameworkConfiguration['plugin.']['tx_guesthouse_guesthouse.']['settings.']['storagePid'];
Lacilacie answered 20/4, 2017 at 12:8 Comment(1)
Or use ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK for only the configuration of the current plugin.Monamonachal
D
7

Only for TYPO3 Backend

For multi domain set root before obtaining configuration

$configurationManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager');
$configurationManager->currentPageId = ROOT_PID_OF_YOUR_DOMAIN;
$extbaseFrameworkConfiguration = $configurationManager->getTypoScriptSetup();

//Following will be resultant array, find your required stuff from it
print_r($extbaseFrameworkConfiguration);

Note: Don't forget to extend your class with \TYPO3\CMS\Extbase\Configuration\BackendConfigurationManager in order to obtain access for it's protected variables

Dyna answered 5/9, 2017 at 8:18 Comment(3)
Your hint about the rootpage just saved me a lot of time. ThxScuff
FYI: Currently currentPageId is protected and has no public setter for this.Resilience
yes, so the 2nd code-line should be omitted. It also means that this code can be used only for the current page, which is fetched automatically. Arbitrary pages can't be processed like this.Braille
V
6

You can add below line in the your controller.

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager');    
$configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
$setting = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);   
$ts_config = $setting['plugin.']['tx_xxxx.']['settings.']['storagePid'];

I think it will helpful to you. You can also used this typo3 settings in the services files as well.

Virus answered 15/6, 2015 at 7:54 Comment(3)
I think this is overly complicated. In an extbase context, you can let the framework inject you a ConfigurationManager directly. Then you can use $settings = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS);, you don't need the full TS.Mellisa
How is this different from the accepted answer? https://mcmap.net/q/1039797/-how-to-get-typo3-settings-in-the-utility-filesFronnia
Within controller this is not a case at all you don't even need to do it yourself, as controller those it itself and returns in its $settings property. The question is about Utlity classes, or Scheduler tasks, etc, there it is much more complicated.Resilience
W
2

Now,In Typo3 8.X, currentPageId is protected so, we could not set it directly, and there would not be any set method defined in core class. Following is correct code as per new version that may help you. Thanks for the correct direction.

$configurationManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Configuration\\BackendConfigurationManager');
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($configurationManager);
$configurationManager->getDefaultBackendStoragePid(); 
$extbaseFrameworkConfiguration = $configurationManager->getTypoScriptSetup();

//Following will be resultant array, find your required stuff from it
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($extbaseFrameworkConfiguration);
Warhorse answered 27/9, 2017 at 5:24 Comment(0)
R
2

In any TYPO3 version including 10, one may use this one-liner:

$GLOBALS['TSFE']->tmpl->setup['plugin.']['tx_xxxx.']['settings.']['storagePid'];

To get rid of the dots, use TypoScriptService, thus

use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\TypoScript\TypoScriptService;

$typoScriptService = GeneralUtility::makeInstance(TypoScriptService::class);
$typoScriptSettings = $typoScriptService->convertTypoScriptArrayToPlainArray($GLOBALS['TSFE']->tmpl->setup);
    
$storagePid = $typoScriptSettings['plugin']['tx_xxxx']['settings']['storagePid'];

Recycle answered 5/1, 2020 at 9:16 Comment(0)
Z
0

You can also only load the CONFIGURATION_TYPE_SETTINGS:

  $objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\Extbase\\Object\\ 
ObjectManager');
  $configurationManager = $objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
  $pluginSettings = $configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, null, 'tx_guesthouse_guesthouse');
  $storagePid = $pluginSettings['storagePid'];

IMHO this is more effective because it does not load the whole TS tree.

Zachariah answered 8/6, 2020 at 1:25 Comment(0)
R
0

A little bit late to the party, but there is also 99° Helpers for TYPO3 extension, which contains methods for fetching TS. i.e.

\nn\t3::Settings()->getFullTyposcript($pid = NULL);

Sample one-liners would look like (note: without trailing dots!):
$mySettings = \nn\t3::Settings()->getFullTyposcript()
                      ['plugin']['tx_myext_myplugin']['settings'];

or even, single value:

$storagePid = \nn\t3::Settings()->getFullTyposcript()
                      ['plugin']['tx_myext_myplugin']['settings']['storagePid'];
de facto

this method is just a wrapper for ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT approach suggested in other answer, and you don't need to install a whole large toolbox for one, simple thing, but if someone already uses 99° Helpers (really nice thing IMHO) can use it within seconds. Bonus tip: mentioned method uses internal caching to make things even faster.

Resilience answered 19/5, 2023 at 20:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.