-->

How to get TYPO3 settings in the utility files?

2020-08-15 01:31发布

问题:

plugin.tx_xxx {
    setting {
        storagePid = 23
    } 
}

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

回答1:

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'];


回答2:

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.



回答3:

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



回答4:

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);


回答5:

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

$typoScriptService = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\TYPO3\CMS\Core\TypoScript\TypoScriptService::class);
$typoScriptSettingsWithoutDots = $typoScriptService->convertTypoScriptArrayToPlainArray($GLOBALS['TSFE']->tmpl->setup);

$storagePid = typoScriptSettingsWithoutDots['plugin']['tx_xxxx']['settings']['storagePid'];

Enjoy.



回答6:

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.