1
votes

I am looking for a way to simplify the creation of content elements in TYPO3.

I am following the official documentation: https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ApiOverview/ContentElements/AddingYourOwnContentElements.html

Now, in step 2 we have this daunting beauty:

// 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. 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.)
  • If this is the same for standard cases, can we get the entire array from the core, e.g. by providing a function for that? 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 (e.g. by PhpStorm plugin)?

I am aware that there is an initiative working on improving how CE are handled longterm. What I am looking for now are things we can do shortterm to simplify CE creation. 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


Disclaimer: I am not an expert on creating content elements in TYPO3. Most of the time I write extensions with plugins or other functionality. This may be a stupid question / suggestion. Just let me know.

1
What you see on your second step are the default tabs and palettes that come from TYPO3. You do not need to use them. You can just only add your own fields. You might find this helpful. stackoverflow.com/questions/62213373/… . I spent a lot of time on creating custom content elements, simple and complexAristeidis Karavas

1 Answers

0
votes

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

The TYPO3 content elements are organised in so called palettes. A palette can contain several properties.

For example the palette "header" has:

  • header
  • header_layout
  • header_position
  • date
  • header_link

So if you want to include all that default header fields, just include the header palette to have all included fields.

You can see the most of the default palettes in frontend/Configuration/TCA/tt_content.php (see array with key 'palettes').

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".

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
  • --palette--;;hidden --> Loads a new palette WITHOUT a specific label
  • --palette--;Your label;hidden --> Loads a new palette WITH a specific label

If this is the same for standard cases, can we get the entire array from the core, e.g. by providing a function for that? 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". 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...