You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TYPO3技术需求:基于现有CType扩展新标识的自定义类型

在TYPO3中基于Textpic创建自定义CType的实现步骤

1. 注册新CType(核心配置)

在你的自定义扩展的Configuration/TCA/Overrides/tt_content.php文件中添加以下代码,直接继承Textpic的现有配置并生成新CType:

<?php
defined('TYPO3') or die();

// 注册新CType选项到后端选择器
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTcaSelectItem(
    'tt_content',
    'CType',
    [
        'LLL:EXT:your_extension/Resources/Private/Language/locallang_db.xlf:tt_content.CType.textpic_custom',
        'textpic_custom',
        'content-textpic' // 复用Textpic的默认图标
    ],
    'textpic',
    'after'
);

// 完全继承Textpic的TCA配置
$GLOBALS['TCA']['tt_content']['types']['textpic_custom'] = $GLOBALS['TCA']['tt_content']['types']['textpic'];

// 可选:自定义新CType的后端字段标签
$GLOBALS['TCA']['tt_content']['types']['textpic_custom']['showitem'] = str_replace(
    'header;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:header_formlabel,',
    'header;LLL:EXT:your_extension/Resources/Private/Language/locallang_db.xlf:tt_content.header.textpic_custom,',
    $GLOBALS['TCA']['tt_content']['types']['textpic_custom']['showitem']
);

2. 配置后端TSconfig(优化选择体验)

在Configuration/page.tsconfig中添加配置,让新CType在后端内容元素选择器中合理分组显示:

mod.wizards.newContentElement.wizardItems.common.elements.textpic_custom {
    iconIdentifier = content-textpic
    title = LLL:EXT:your_extension/Resources/Private/Language/locallang_db.xlf:tt_content.CType.textpic_custom
    description = LLL:EXT:your_extension/Resources/Private/Language/locallang_db.xlf:tt_content.CType.textpic_custom.description
    tt_content_defValues {
        CType = textpic_custom
    }
}

// 将新元素加入"Text & Media"分组
mod.wizards.newContentElement.wizardItems.common.show := addToList(textpic_custom)

3. 配置前端渲染模板

在Configuration/TypoScript/setup.typoscript中为新CType指定自定义渲染逻辑,继承Textpic的基础配置但替换模板文件:

// 继承Textpic的渲染配置
tt_content.textpic_custom < tt_content.textpic
// 指定自定义模板名称
tt_content.textpic_custom.templateName = TextpicCustom

// 配置自定义模板路径(Fluid模板场景)
lib.contentElement.templateRootPaths.100 = EXT:your_extension/Resources/Private/Templates/ContentElements/

之后在你的扩展目录Resources/Private/Templates/ContentElements/TextpicCustom.html中编写差异化渲染逻辑,可直接复用Textpic的原有代码片段,只修改需要定制的部分。

4. 后续扩展后端字段(按需操作)

如果之后需要为新CType添加专属字段,可在tt_content.php中补充以下代码:

// 添加新字段到tt_content表
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTCAcolumns(
    'tt_content',
    [
        'tx_yourextension_custom_field' => [
            'label' => 'LLL:EXT:your_extension/Resources/Private/Language/locallang_db.xlf:tt_content.tx_yourextension_custom_field',
            'config' => [
                'type' => 'input',
                'size' => 30,
                'eval' => 'trim'
            ]
        ]
    ]
);

// 将字段添加到新CType的后端显示项中
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addToAllTCAtypes(
    'tt_content',
    'tx_yourextension_custom_field',
    'textpic_custom',
    'after:image' // 指定字段显示位置
);

内容的提问来源于stack exchange,提问作者peni4142

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 14:32:56