如何在TYPO3后端只读展示已配置的TypoScript全局设置
实现方案
你之前尝试user类型没有成功应该是没有配套注册对应的渲染节点,以下是完整可落地的实现步骤:
- 配置TCA字段
在你的扩展对应表的TCA配置(如Configuration/TCA/tx_zhawcontinuingeducation_domain_model_course.php)中新增只读展示字段:
'aztb_terms_url' => [ 'exclude' => 1, 'label' => '当前学院条款链接', 'config' => [ 'type' => 'user', 'renderType' => 'aztbTermsUrlPreview', 'readOnly' => true, ] ],
该字段不需要写入数据库,无需在ext_tables.sql中添加字段定义。
- 注册后端表单渲染节点
在扩展的ext_localconf.php中注册自定义渲染组件的节点映射:
// 节点标识数值建议换成自己的时间戳避免冲突 $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][1695000000] = [ 'nodeName' => 'aztbTermsUrlPreview', 'priority' => 40, 'class' => \Zhaw\ZhawContinuingEducation\Form\Element\AztbTermsUrlElement::class, ];
- 实现自定义渲染类
在扩展下新建Classes/Form/Element/AztbTermsUrlElement.php文件,编写渲染逻辑,直接读取Typoscript配置和当前记录信息输出内容:
<?php namespace Zhaw\ZhawContinuingEducation\Form\Element; use TYPO3\CMS\Backend\Form\Element\AbstractFormElement; use TYPO3\CMS\Core\Context\Context; use TYPO3\CMS\Core\Utility\GeneralUtility; use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; class AztbTermsUrlElement extends AbstractFormElement { public function render(): array { $result = $this->initializeResultArray(); // 获取当前课程记录所属学院值 $schoolCode = $this->data['databaseRow']['school'][0] ?? ''; if (empty($schoolCode)) { $output = '<span class="text-muted">请先选择所属学院后查看链接</span>'; $result['html'] = $output; return $result; } // 读取Typoscript配置 $configManager = GeneralUtility::makeInstance(\TYPO3\CMS\Extbase\Configuration\ConfigurationManager::class); $extSettings = $configManager->getConfiguration( ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, 'ZhawContinuingEducation', 'Course' ); // 匹配当前语言对应的链接配置 $langUid = GeneralUtility::makeInstance(Context::class)->getPropertyFromAspect('language', 'id'); $configKey = $langUid === 1 ? 'termsAndConditionsUrlsEN' : 'termsAndConditionsUrlsDE'; $targetUrl = $extSettings[$configKey][$schoolCode] ?? ''; if (empty($targetUrl)) { $output = '<span class="text-danger">未匹配到对应条款链接</span>'; } else { // 输出可点击的链接,和截图效果一致 $output = sprintf( '<a href="%s" target="_blank" class="btn btn-link p-0">%s</a>', htmlspecialchars($targetUrl), htmlspecialchars($targetUrl) ); } $result['html'] = $output; return $result; } }
- 生效配置
清理TYPO3后端缓存后,打开课程编辑页即可看到对应只读展示的链接字段,和你预期的截图效果一致。
如果不需要点击跳转仅需展示纯文本,把渲染输出的a标签换成纯文本即可。
内容的提问来源于stack exchange,提问作者Dan
相关产品推荐
相关产品推荐

