A while ago I initiated a discussion about simplifying the process to create custom content elements and it turns out, some people are working on that in general already. Yet, it’s unknown when things are finished and be integrated into the core.
In the meantime, I would like to set up a best practice, which can lead to command line tools helping to manage (create, update, delete) custom CTypes within own extensions.
A while ago, we have set up a directory structure which allows us to easily create new custom content elements. It works a lot with conventions.
Directory structure:
- Configuration
** TCA/Overrides/CType/
** TSconfig/PageTS/CType/
** TypoScript/CType/ - Resources/Language/CType
The core currently does not handle sub directories within Overrides. In order to make that work tt_content.php needs to be overridden:
<?php
if (!defined('TYPO3_MODE')) {
die('Access denied.');
}
(function () {
// T3 Uses the Finder itself to find and require the TCA/Overrides-files. Yet, not nested. That's why
// It's done here for our custom, nested directory.
$finder = \Symfony\Component\Finder\Finder::create();
$finder->files()->sortByName()->depth(0)->name('*.php')->in(__DIR__ . '/CType');
foreach ($finder as $fileInfo) {
require $fileInfo->getPathname();
}
})();
The CType php file contains following config (shortened):
// General configuration
$icon = 'registered-icon';
// Configuration by Convention
$CType = str_replace('.php', '', basename(__FILE__));
$label = 'LLL:EXT:provider/Resources/Private/Language/' . $CType . '.xlf:label';
\TYPO3\CMS\Core\Utility\ArrayUtility::mergeRecursiveWithOverrule(
$GLOBALS['TCA']['tt_content'],
[
'ctrl' => [
'typeicon_classes' => [
$CType => $icon,
],
],
[
'types' => [
$CType => [
'showitem' => '
Some Default Definition. F.e. palette general, hidden etc.
',
],
],
]
);
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addTcaSelectItem(
'tt_content',
'CType',
[
$label,
$CType,
$icon
],
' 1',
'after'
);
The CType PageTS-config definition contains a standard definition either. Same goes for TypoScript and the CType XLF file. Furthermore the CType files are included automatically.
@import 'EXT:provider/Configuration/TSconfig/PageTS/CType/*.tsconfig'
@import 'EXT:provider/Configuration/TypoScript/CType/*.typoscript'
In order to be able to place the CTypes as we want, we prefix the Page TS config files with a number. For example: 08.provider_advertisement.tsconfig
So, whenever a new CType needs to be created, we simply copy the necessary files, perform a search and replace within each CType and do the necessary adjustments.
Despite reading out the sub directory of Overriden, no new features are introduced and it can be adapted by any T3 instance. This convention also allows to create command line scripts e.g. to handle that, even modify the CType key.
What are your thoughts about that? I am looking forward to hear your opinions on that.