[PHP] Replace FlexForm of an extension

[PHP] Replace FlexForm of an extension

TYPO3 developers need new fields from time to time in the FlexForm settings of other extensions (such as news, powermail or tt_address). The following snippet shows you how you can completely or partially replace a FlexForm using the example of the extension tt_address or in2studyfinder.

Article revised on February 26, 2025

TYPO3 12 or newer

Since TYPO3 12 you can use an event to completely overwrite the FlexForm in TYPO3 or just add individual parts.

The EventListener to be used must be registered in the Services.yaml:

services: In2code\In2template\EventListener\ModifyTtAddressFlexForm: tags: - name: event.listener event: TYPO3\CMS\Core\Configuration\Event\AfterFlexFormDataStructureParsedEvent

Existing fields can then be modified (or the FlexForm completely overwritten):

<?php declare(strict_types=1); namespace In2code\In2template\EventListener; use TYPO3\CMS\Core\Configuration\Event\AfterFlexFormDataStructureParsedEvent; use TYPO3\CMS\Core\Utility\ArrayUtility; use TYPO3\CMS\Core\Utility\GeneralUtility; final class ModifyTtAddressFlexForm { private string $flexFormFile = 'EXT:in2template/Configuration/FlexForm/Extend/TtAddress.xml'; public function __invoke(AfterFlexFormDataStructureParsedEvent $event): void { $dataStructure = $event->getDataStructure(); if ($this->isTtAddressPlugin($event->getIdentifier())) { $configurationNew = file_get_contents(GeneralUtility::getFileAbsFileName($this->flexFormFile)); if ($configurationNew !== false) { ArrayUtility::mergeRecursiveWithOverrule($dataStructure, GeneralUtility::xml2array($configurationNew)); } } $event->setDataStructure($dataStructure); } private function isTtAddressPlugin(array $identifier): bool { return $identifier['type'] === 'tca' && $identifier['tableName'] === 'tt_content' && $identifier['dataStructureKey'] === 'ttaddress_listview,list'; } }

 

You could either configure the array directly in the EventListener or alternatively specify a FlexForm file that then overwrites parts of the original FlexForm (for example under EXT:in2template/Configuration/FlexForm/Extend/TtAddress.xml):

<T3DataStructure> <sheets> <sDISPLAY> <ROOT> <el> <settings.displayMode> <onChange>reload</onChange> </settings.displayMode> <settings.backgroundColor> <displayCond>FIELD:settings.displayMode:=:banner</displayCond> <exclude>1</exclude> <label>LLL:EXT:in2template/Resources/Private/Language/Backend/locallang.xlf:tt_content.in2template_background.label</label> <config> <type>select</type> <renderType>selectSingle</renderType> <itemsProcFunc>In2code\In2template\Tca\ColorOptions->addOptions</itemsProcFunc> </config> </settings.backgroundColor> </el> </ROOT> </sDISPLAY> </sheets> </T3DataStructure>
TYPO3 11 or older

ext_localconf.php eurer Extension (z.B. Sitepackage):

<?php defined('TYPO3_MODE') || die(); call_user_func( function () { /** * Overwrite FlexForm with a new one */ $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['postUserLookUp'][] = \Vendor\YourSitepackage\Hooks\OverwriteFlexForm::class . '->overwrite'; } );

OverwriteFlexForm.php:

<?php declare(strict_types=1); namespace Vendor\YourSitepackage\Hooks; /** * Class OverwriteFlexForm */ class OverwriteFlexForm { /** * @var string */ protected $path = 'FILE:EXT:your_sitepackage/Configuration/FlexForms/FlexformStudyfinderList.xml'; /** * @return void */ public function overwrite() { $GLOBALS['TCA']['tt_content']['columns']['pi_flexform']['config']['ds']['in2studyfinder_pi1,list'] = $this->path; } }

FlexformStudyfinderList.xml:

<T3DataStructure> <meta> <langDisable>1</langDisable> </meta> <sheets> <main> <ROOT> <TCEforms> <sheetTitle>LLL:EXT:in2studyfinder/Resources/Private/Language/locallang_db.xlf:listView</sheetTitle> </TCEforms> <type>array</type> <el> <settings.flexform.studyCourseDetailPage> <TCEforms> <exclude>1</exclude> <label>LLL:EXT:in2studyfinder/Resources/Private/Language/locallang_db.xlf:detailView</label> <config> <type>group</type> <internal_type>db</internal_type> <allowed>pages</allowed> <size>1</size> <maxitems>1</maxitems> <minitems>1</minitems> <show_thumbs>1</show_thumbs> <wizards> <suggest> <type>suggest</type> </suggest> </wizards> </config> </TCEforms> </settings.flexform.studyCourseDetailPage> <settings.flexform.select.department> <TCEforms> <label>LLL:EXT:in2studyfinder/Resources/Private/Language/locallang_db.xlf:department</label> <config> <type>select</type> <renderType>selectCheckBox</renderType> <maxitems>9999</maxitems> <size>5</size> <minitems>0</minitems> <foreign_table>tx_in2studyfinder_domain_model_department</foreign_table> <foreign_table_where>and tx_in2studyfinder_domain_model_department.sys_language_uid in (-1,0) order by tx_in2studyfinder_domain_model_department.title</foreign_table_where> </config> </TCEforms> </settings.flexform.select.department> <settings.flexform.select.graduation> <TCEforms> <label>LLL:EXT:in2studyfinder/Resources/Private/Language/locallang_db.xlf:academicDegree</label> <config> <type>select</type> <renderType>selectCheckBox</renderType> <maxitems>9999</maxitems> <size>5</size> <minitems>0</minitems> <foreign_table>tx_in2studyfinder_domain_model_graduation</foreign_table> <foreign_table_where>and tx_in2studyfinder_domain_model_graduation.sys_language_uid in (-1,0) order by tx_in2studyfinder_domain_model_graduation.title</foreign_table_where> </config> </TCEforms> </settings.flexform.select.graduation> <settings.flexform.bodytext> <TCEforms> <label>LLL:EXT:in2studyfinder_extend/Resources/Private/Language/locallang_db.xlf:flexform.bodytext</label> <config> <type>text</type> <default></default> <enableRichtext>1</enableRichtext> <richtextConfiguration>default</richtextConfiguration> </config> <defaultExtras>richtext[]:rte_transform[mode=ts_css]</defaultExtras> </TCEforms> </settings.flexform.bodytext> </el> </ROOT> </main> </sheets> </T3DataStructure>

TYPO3: Finding unused files in fileadmin

Do you want to delete unused or orphaned files in fileadmin or another storage location? Unfortunately, there's no direct core functionality for this. But a small command in your site package can...

Go to news

TYPO3: Editors with individual user_upload folders

Perhaps you're familiar with this client requirement? Editors should be able to add videos using the "Add media by URL" button. But the files shouldn't be located in fileadmin/user_upload/, but rather...

Go to news

TYPO3: Finding pages in mixed mode

In TYPO3, Mixed Mode refers to translated pages that contain content only partially related to the corresponding content in the main language. This is indicated in the backend by an error message. But...

Go to news

Extbase Extensions: Think extensibility with data, site and language

Today, I have a small request for the TYPO3 extension authors out there: Make sure your extensions are extensible. This will also promote the distribution of the corresponding plugins.

Go to news

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news