spooner-web/tcabuilder

Utility to easily maintain and create your TCA forms

Fund package maintenance!
Other

Installs: 23 230

Dependents: 0

Suggesters: 0

Security: 0

Type:typo3-cms-extension

3.3.0 2024-08-08 22:14 UTC

README

pipeline status coverage report Latest Version

What does it do?

With the TCA builder you have the possibility to create or change the fields in type list in an easy and speaking way.

Times of semicolons within field names are gone.

For example:

  1. Compose the field list for the form of a content element or any other record
  2. Change the types of existing definitions

Introducing TcaCreator in v1.6.0

With the TCA creator you have the possibility to create TCA forms from scratch and do not need to think about the configuration of the default fields like the ctrl section or the default fields in the columns section.

More to see in the Examples Section

Installation

Installation via composer

composer require spooner-web/tcabuilder

:warning: In a composer install, the package won't be in typo3conf/ext folder but in vendor

Installation via classic mode

  1. Head to https://extensions.typo3.org/extension/tcabuilder
  2. Download the ZIP file
  3. Upload it to your TYPO3 instance using non-composer mode

Installation via Extension Manager

  1. Update your extension list
  2. Search for "tcabuilder"
  3. Install

Composer fails checking out this package

As composer is not able to detect the ZIP archive of a self-hosted GitLab instance, there may occur problems when deploying or building a project with this package.

To fix this issue, you need to add the GitLab built-in packagist API into the repositories section of your project composer.json:

{
	"type": "composer",
	"url": "https://git.spooner.io/api/v4/group/8/-/packages/composer/"
}

Usage

General usage

Recommendation is to use the TcaBuilder in the php files of your Configuration/TCA/Overrides/ folder of your extension.

  1. Instantiate the TcaBuilder class
  2. Set the table and type to configure (may also be a not existing type yet)
  3. Use the methods to manipulate
  4. Save to the TCA
  5. Flush caches
  6. See result

Methods

Main methods

Method nameDescriptionParameters
setTableSets the table to load configuration fromstring $tableName
setTypeSets the type to load configuration fromstring $typeName
loadLoads configuration if it's an existing type
loadConfigurationShorter method to run setTable, setType and load at oncestring $tableName
string $typeName
useLocalLangFileSet a locallang file (beginning with EXT:) to use in labelsstring $localLangFile
saveToTcaSaves the manipulated configuration to TCAbool $resetAfterSave (optional)
returnAsArrayInstead of saving the configuration it returns it directly as array

Manipulating types

Method nameDescriptionParameters
addFieldAdds a field to selected typestring $typeName
string $position (optional)
string $alternativeLabel (optional)
removeFieldRemoves a field from selected typestring $fieldName
moveFieldMoves a field to a new position (alternatively with a new label)string $fieldName
string $newPosition
string $newLabel (optional)
addPaletteAdds an existing palette to selected typestring $paletteName
string $position (optional)
string $alternativeLabel (optional)
removePaletteRemoves a palette from selected typestring $paletteName
movePaletteMoves a palette to a new position (alternatively with a new label)string $paletteName
string $newPosition
string $newLabel (optional)
addDivAdds a div (tab) to selected typestring $divName
string $label
removeDivRemoves a div (tab) from selected type, either by position (index, beginning with 0) or by labelstring|int $positionOrLabel
addOverrideAdds a custom override of a fieldstring $fieldName
array $configuration
initializeInitializes the type with an empty list

Manipulating palettes

Method nameDescriptionParameters
addCustomPaletteCreates a new palette (and optionally inserts it directly to given position)string $paletteId
array $fields
string $label (optional)
string $position (optional)
addFieldToPaletteAdds a field to a palettestring $paletteId
string $field
string $position (optional)
removeFieldFromPaletteRemoves a field from a palettestring $paletteId
string $field
initializePaletteInitializes the palette with an empty liststring $paletteId

Helper methods

Method nameDescriptionParametersReturns
getPaletteStringFinds the complete palette string which is used in list (for using it in position strings)string $paletteNamestring The complete palette string with --palette-- and the possible label config
getDivStringFinds the complete div string which is used in list (for using it in position strings), either by position (index, beginning with 0) or by labelstring|int $positionOrLabelstring The complete palette string with --div-- and the div's label

Possible values for positioning fields, palettes or divs

ValueDescription
before:<item>Moves the item before the given item
after:<item>Moves the item after the given item
replace:<item>Replaces the given item

Examples (TCA builder)

Add an own content element

$tcaBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\SpoonerWeb\TcaBuilder\TcaBuilder::class);
$tcaBuilder
    ->setTable('tt_content') // define table
    ->setType('test') // define type
    ->addDiv('General')
    ->addField('header', '', 'Top header!!')
    ->addField('bodytext')
    ->addField('subheader', 'after:header')
    ->addField('layout', 'before:header')
    ->addDiv('Extra')
    ->addPalette('access')
    ->addPalette('hidden', 'after:bodytext', 'Alternative label')
    ->saveToTca(); // save to TCA

Change existing configurations

$tcaBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\SpoonerWeb\TcaBuilder\TcaBuilder::class);
$tcaBuilder
    ->setTable('pages') // define table
    ->setType(\TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_LINK) // define type
    ->load() // load definitions
    ->removeField('doktype')
    ->removePalette('external')
    ->removeDiv(1)
    ->addPalette('external', 'after:--palette--;;layout')
    ->saveToTca(); // save back to TCA
$tcaBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\SpoonerWeb\TcaBuilder\TcaBuilder::class);
$tcaBuilder
    ->loadConfiguration('pages', \TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_MOUNTPOINT)
    ->removeDiv('LLL:EXT:frontend/Resources/Private/Language/locallang_tca.xlf:pages.tabs.metadata')
    ->movePalette('title', 'after:' . $tcaBuilder->getPaletteString('abstract'), 'New title')
    ->addOverride(
        'title',
        [
            'label' => 'New title',
            'config' => [
                'renderType' => 'inputLink'
            ]
        ]
    )
    ->saveToTca();

Use language file

$tcaBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\SpoonerWeb\TcaBuilder\TcaBuilder::class);
$tcaBuilder
    ->loadConfiguration('pages', \TYPO3\CMS\Frontend\Page\PageRepository::DOKTYPE_MOUNTPOINT)
    ->useLocalLangFile('EXT:my_extension/Resources/Private/Language/locallang.xlf')
    ->addField('new_field', '', 'LANG:new_field') // Used label: "LLL:EXT:my_extension/Resources/Private/Language/locallang.xlf:new_field"
    ->saveToTca();

Do minimal changes

$tcaBuilder = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(\SpoonerWeb\TcaBuilder\TcaBuilder::class);
$tcaBuilder
    ->loadConfiguration('tt_content', 'textmedia')
    ->removePalette('headers')
    ->saveToTca();

Examples (Tca Creator)

Creating TCA configuration for a new table (inside custom extension)


// Returns the default values of ctrl section
// By default language, version and sorting options are set
// These options can be unset and additional overridden fields can be set
$configuration['ctrl'] = \SpoonerWeb\TcaBuilder\TcaCreator::getControlConfiguration(
    'title',
    'label'    
);

// Returns the default columns depending on the ctrl section configuration
// Example: If ctrl section includes language, this method returns the field configuration
// for all language fields (sys_language_uid, l10n_parent, l10n_diffsource, l10n_source)
$configuration['columns'] = \SpoonerWeb\TcaBuilder\TcaCreator::getColumnsConfiguration(
    $configuration['ctrl'],
    'tx_extension_domain_model_record',
    [
        'title' => [
            'label' => 'My label',
            'config' => [
                'type' => 'input'
            ]
        ]
    ]
);

// Uses TcaBuilder class to create the configuration for the TCA form
$configuration['types'][] = \SpoonerWeb\TcaBuilder\TcaCreator::buildTypesConfiguration()
    ->addDiv('General')
    ->addField('title')
    ->addField('subtitle')
    ->addDiv('Categories')
    ->addField('categories')
    ->returnAsArray();
    
// Now return TCA configuration
return $configuration;