如何在实用程序文件中获取TYPO3设置


How to get TYPO3 settings in the utility files?

plugin.tx_xxx {
    setting {
        storagePid = 23
    } 
}

我希望实用程序文件中的此TYPO3设置。请帮助我。

上述方法仅适用于控制器或服务类,请尝试以下方法,它将适用于扩展中的任何PHP文件。

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

仅适用于 TYPO3 后端

对于获取配置之前的多域设置根

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

注意:不要忘记通过'TYPO3'CMS'Extbase'Configuration'BackendConfigurationManager扩展您的课程 为了获得对其受保护变量的访问权限

您可以在控制器中添加以下行。

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

我认为这对你有帮助。您也可以在服务文件中使用此 typo3 设置。

现在,在 Typo3 8.X 中,当前PageId 受到保护,因此我们无法直接设置它,并且在核心类中不会定义任何 set 方法。以下是新版本的正确代码,可能会对您有所帮助。感谢您的正确方向。

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

在任何 TYPO3 版本中(包括 10),可以使用以下单行代码:

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

要去除这些点,请使用 TypoScriptService ,因此

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

您也可以只加载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'];

恕我直言,这更有效,因为它不会加载整个 TS 树。

派对有点晚了,但也有 TYPO3 扩展的 99° 助手,其中包含获取 TS 的方法。

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

示例单行代码如下所示(注意:没有尾随点!
$mySettings = 'nn't3::Settings()->getFullTyposcript()
                      ['plugin']['tx_myext_myplugin']['settings'];

甚至,单个值:

$storagePid = 'nn't3::Settings()->getFullTyposcript()
                      ['plugin']['tx_myext_myplugin']['settings']['storagePid'];
事实上

此方法只是其他答案中建议ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT方法的包装器,您无需为一件简单的事情安装整个大型工具箱,但如果有人已经使用 99° 助手(恕我直言)可以在几秒钟内使用它。额外提示:上述方法使用内部缓存来使事情更快。