简体   繁体   English

在 TYPO3 中简化内容元素/TCA 的创建

[英]Simplify creation of content elements / TCA in TYPO3

I am looking for a way to simplify the creation of content elements in TYPO3.我正在寻找一种方法来简化 TYPO3 中内容元素的创建。

I am following the official documentation: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/ContentElements/AddingYourOwnContentElements.html我正在关注官方文档: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/ContentElements/AddingYourOwnContentElements.html

Now, in step 2 we have this daunting beauty:现在,在第 2 步中,我们有这个令人生畏的美丽:

// Configure the default backend fields for the content element
$GLOBALS['TCA']['tt_content']['types']['yourextensionkey_newcontentelement'] = [
    'showitem' => '
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
            --palette--;;general,
            --palette--;;headers,
            bodytext;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:bodytext_formlabel,
        --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.appearance,
            --palette--;;frames,
            --palette--;;appearanceLinks,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
            --palette--;;language,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access,
            --palette--;;hidden,
            --palette--;;access,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:categories,
            categories,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:notes,
            rowDescription,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:extended,
    ',
    'columnsOverrides' => [
        'bodytext' => [
            'config' => [
                'enableRichtext' => true,
                'richtextConfiguration' => 'default',
            ],
        ],
    ],
];

It looks to me, as if all that needs to be replaced is 'yourextensionkey_newcontentelement' and the rest comes from the core.在我看来,好像所有需要替换的都是“yourextensionkey_newcontentelement”,而 rest 来自核心。 Unless you know what you are doing and want to do this differently.除非您知道自己在做什么并且想以不同的方式来做。

My questions:我的问题:

  • What exactly does this do?这到底是做什么的? (I am aware, it sets up some TCA for the forms of editing the CE.) (我知道,它为编辑CE的forms设置了一些TCA。)
  • If this is the same for standard cases, can we get the entire array from the core, eg by providing a function for that?如果这对于标准情况是相同的,我们是否可以从核心获取整个数组,例如为此提供 function? Would that be a good approach?那会是一个好方法吗?
  • Do you have other ideas for simplifying this?你有其他简化这个的想法吗?
  • Are there methods available to write this human-readable and convert it or with autoexpand (eg by PhpStorm plugin)?是否有可用的方法来编写这种人类可读的并转换它或使用自动扩展(例如通过 PhpStorm 插件)?

I am aware that there is an initiative working on improving how CE are handled longterm.我知道有一项计划致力于改善 CE 的长期处理方式。 What I am looking for now are things we can do shortterm to simplify CE creation.我现在正在寻找的是我们可以在短期内做的事情来简化 CE 创建。 I am also aware there are extensions like "mask" or "dce" but we don't advertise them in the official docs, we advertise this: Create Custom Content Elements我也知道有像“mask”或“dce”这样的扩展,但我们不会在官方文档中宣传它们,我们宣传这个: Create Custom Content Elements


Disclaimer: I am not an expert on creating content elements in TYPO3. Most of the time I write extensions with plugins or other functionality.免责声明:我不是 TYPO3 中创建内容元素的专家。大部分时间我使用插件或其他功能编写扩展。 This may be a stupid question / suggestion.这可能是一个愚蠢的问题/建议。 Just let me know.请告诉我。

I understand your "problem" of the repeating code and copy / paste work, though it allows a lot of flexibility.我理解您重复代码和复制/粘贴工作的“问题”,尽管它具有很大的灵活性。

To your questions:对于您的问题:

What exactly does this do?这到底是做什么的? (I am aware, it sets up some TCA for the forms of editing the CE.) (我知道,它为编辑CE的forms设置了一些TCA。)

The TYPO3 content elements are organised in so called palettes. TYPO3 内容元素被组织在所谓的调色板中。 A palette can contain several properties.调色板可以包含多个属性。

For example the palette "header" has:例如调色板“标题”有:

  • header header
  • header_layout header_layout
  • header_position header_position
  • date日期
  • header_link header_link

So if you want to include all that default header fields, just include the header palette to have all included fields.因此,如果您想要包含所有默认的 header 字段,只需包含 header 调色板即可包含所有包含的字段。

You can see the most of the default palettes in frontend/Configuration/TCA/tt_content.php (see array with key 'palettes').您可以在 frontend/Configuration/TCA/tt_content.php 中看到大部分默认调色板(请参阅带有键“调色板”的数组)。

Within the column overrides you can easily override specific values / properties / settings, which are already defined in the core.在列覆盖中,您可以轻松覆盖已在核心中定义的特定值/属性/设置。 In your example, it overrides the RTE settings of the field "bodytext".在您的示例中,它会覆盖字段“bodytext”的 RTE 设置。

The string itself looks a bit cryptic.字符串本身看起来有点神秘。 The placeholders and what they do:占位符及其作用:

  • --div--;Label of the tab --> Starts a new tab with given label --div--;Label of the tab --> 用给定的 label 开始一个新的标签
  • --palette--;;hidden --> Loads a new palette WITHOUT a specific label --palette--;;hidden --> 加载没有特定 label 的新调色板
  • --palette--;Your label;hidden --> Loads a new palette WITH a specific label --palette--;Your label;hidden --> 使用特定的 label 加载新调色板

If this is the same for standard cases, can we get the entire array from the core, eg by providing a function for that?如果这对于标准情况是相同的,我们是否可以从核心获取整个数组,例如为此提供 function? Would that be a good approach?那会是一个好方法吗?

Unfortunately, this is not an array, but a string.不幸的是,这不是一个数组,而是一个字符串。 So you can't merge, except you want to split strings and combine them again.所以你不能合并,除非你想拆分字符串并再次组合它们。 Somewhere within that string, you need to include your own fields / palettes.在该字符串的某处,您需要包含自己的字段/调色板。 Also, not every new content element needs all fields.此外,并非每个新内容元素都需要所有字段。 So in my opinion, it is better readable to have it fully implemented for every new content element.所以在我看来,为每个新的内容元素完全实现它更具可读性。

Example:例子:

$GLOBALS['TCA']['tt_content']['types']['alert'] = array(
    'showitem' => '
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
            --palette--;;general,
            --palette--;LLL:EXT:your_ext/Resources/Private/Language/backend.locallang.xlf:tt_content.alert.palettes.general.title;alert,
        --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.appearance,
            --palette--;;frames,
            --palette--;;appearanceLinks,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
            --palette--;;language,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access,
            --palette--;;hidden,
            --palette--;;access,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:notes,
            rowDescription,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:extended,
    ',
    'columnsOverrides' => [
        'bodytext' => [
            'config' => [
                'enableRichtext' => true
            ]
        ],
    ]
);

In this example, I load a custom palette directly after the core palette "general".在这个例子中,我在核心调色板“general”之后直接加载了一个自定义调色板。 This could also be done by a helper method.这也可以通过辅助方法来完成。 I don't like it:-)我不喜欢它:-)

function merge($yourDefinition): string
{
    return '
    --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:general,
            --palette--;;general,
            '.$yourDefinition.'
        --div--;LLL:EXT:frontend/Resources/Private/Language/locallang_ttc.xlf:tabs.appearance,
            --palette--;;frames,
            --palette--;;appearanceLinks,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:language,
            --palette--;;language,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:access,
            --palette--;;hidden,
            --palette--;;access,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:notes,
            rowDescription,
        --div--;LLL:EXT:core/Resources/Private/Language/Form/locallang_tabs.xlf:extended
        ';
}

Do you have other ideas for simplifying this?你有其他简化这个的想法吗?

Like mentioned, with a helper method or elsewhere string combining.如前所述,使用辅助方法或其他地方的字符串组合。 In my opinion this will lead to less flexibility and also poor readability.在我看来,这将导致灵活性降低和可读性差。

I don't like code repeating, but in this case, I define every new content element with it's own, uncombined / unmerged string...我不喜欢代码重复,但在这种情况下,我用它自己的、未组合/未合并的字符串定义每个新的内容元素……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM