Search code examples
typo3typo3-extensionstypo3-8.xtypo3-8.7.x

TYPO3 automatic page creation based on TCA record


I've special requirement on my project and I need help. I am using TYPO3 8.7.8. I've a custom extension to render tag labels in frontend. We can add the tags as TCA record in backend storage folder. In the TCA record, you can tag name. My requirement is, when I save the TCA record I want to create a TYPO3 page automatically with the same name as tag in a specific position. Everytime when I add a TCA record, I need to create corresponding page automatically. Is this possible? I can use hook while saving TCA. But is there any function to create pages automatically?

After automatic page creation, I want to insert a plugin content element in that page with a specific flexform value automatically. I know this is a strange requirement, but I would like to know if it is possible or not.


Solution

  • Exactly, you'd trigger a hook on saving and then as next step you can use the data handler to generate the new page (and possible content).

    To create the page and content, use something like the following data structure

    $data = [
       'pages' => [
           'NEW_1' => [
               'pid' => 456,
               'title' => 'Title for page 1',
           ],
        ],
        'tt_content' => [
            'NEW_123' => [
               'pid' => 'NEW_1',
               'header' => 'My content element',
            ],
        ],
    ];
    

    Then call the datahandler with that structure:

    $tce = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\DataHandling\\DataHandler');
    $tce->stripslashes_values = 0;
    $tce->start($data, []);
    $tce->process_datamap();
    

    Find out more in the docs at https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/Database/Index.html#data-array and https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Typo3CoreEngine/UsingDataHandler/Index.html