|
| 1 | +.. _formDataProvider: |
| 2 | + |
| 3 | +================ |
| 4 | +FormDataProvider |
| 5 | +================ |
| 6 | + |
| 7 | +`FormDataProvider <https://docs.typo3.org/permalink/t3coreapi:formengine-datacompiling>`_ |
| 8 | +can be used to offer form-handling beyond the options provided by `displayCond` and `eval` in the TCA. |
| 9 | +Among other things, a FormDataProvider allows dynamic TCA changes while records are loaded in the TYPO3 backend. |
| 10 | + |
| 11 | +Example |
| 12 | +------- |
| 13 | + |
| 14 | +This example explains how to make at least one image mandatory if the record is flagged as top news. |
| 15 | + |
| 16 | +1) Update TCA |
| 17 | +~~~~~~~~~~~~~ |
| 18 | + |
| 19 | +Create the file `Configuration/TCA/Overrides/tx_news_domain_model_news.php` in your custom extension. |
| 20 | +See `here <ext-based-on-news>`_ how to create that extension. |
| 21 | + |
| 22 | +.. code-block:: php |
| 23 | +
|
| 24 | + <?php |
| 25 | +
|
| 26 | + defined('TYPO3') or die(); |
| 27 | +
|
| 28 | + $GLOBALS['TCA']['tx_news_domain_model_news']['columns']['istopnews']['onChange'] = 'reload'; |
| 29 | +
|
| 30 | +With `'onChange' => 'reload'` you trigger a reload of the backend interface when the "Top news" toggle is clicked. |
| 31 | + |
| 32 | +2) Register FormDataProvider class |
| 33 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 34 | + |
| 35 | +Update file `ext_localconf.php` in your extension: |
| 36 | + |
| 37 | +.. code-block:: php |
| 38 | +
|
| 39 | + <?php |
| 40 | +
|
| 41 | + defined('TYPO3') or die(); |
| 42 | +
|
| 43 | + use Vendor\CustomExtension\Backend\Form\FormDataProvider\ImageMinItems; |
| 44 | + use TYPO3\CMS\Backend\Form\FormDataProvider\DatabaseRowInitializeNew; |
| 45 | +
|
| 46 | + $GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['formDataGroup']['tcaDatabaseRecord'][ImageMinItems::class] = [ |
| 47 | + 'depends' => [ |
| 48 | + DatabaseRowInitializeNew::class, |
| 49 | + ], |
| 50 | + ]; |
| 51 | +
|
| 52 | +3) Add your FormDataProvider class |
| 53 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 54 | + |
| 55 | +.. code-block:: php |
| 56 | +
|
| 57 | + <?php |
| 58 | +
|
| 59 | + declare(strict_types=1); |
| 60 | +
|
| 61 | + namespace Vendor\CustomExtension\Backend\Form\FormDataProvider; |
| 62 | +
|
| 63 | + use TYPO3\CMS\Backend\Form\FormDataProviderInterface; |
| 64 | +
|
| 65 | + final class ImageMinItems implements FormDataProviderInterface |
| 66 | + { |
| 67 | + public function addData(array $result): array |
| 68 | + { |
| 69 | + if ($result['tableName'] == 'tx_news_domain_model_news' && $result['databaseRow']['istopnews'] == 1) { |
| 70 | + $result['processedTca']['columns']['fal_media']['config']['minitems'] = 1; |
| 71 | + } |
| 72 | + return $result; |
| 73 | + } |
| 74 | + } |
| 75 | +
|
| 76 | +This class modifies the TCA when the current record is `tx_news_domain_model_news` and `istopnews` is set. |
0 commit comments