Issue Bug Closed

#107553BeforeFlexFormDataStructureParsedEvent not triggered for certain content creation actions

Theo Langstraat 09/27/25, 06:43 updated 09/28/25, 19:03

Description

Description:*
In the TYPO3 backend, new content elements can be added using various buttons, such as “+ Create new content” in the Border section, Before Content, Normal, or via the “+ New” button in the toolbar above the content list. Upon testing, it appears that only the very first “+ Create new content” button triggers the BeforeFlexFormDataStructureParsedEvent. All other buttons — including those placed between existing content elements and the “+ New” button — do not trigger the event.
*Impact:*
As a result, the FlexForm is correctly initialized only when using the first button. In all other cases, the default, unmodified FlexForm is loaded, leading to inconsistent behavior and incorrect configuration of content elements.
*Steps to reproduce:

  1. Navigate to a page in the backend with existing content elements.
  2. Add a new content element using:
    • The first “+ Create new content” button → Event is triggered.
    • Any button between existing content elements → Event is not triggered.
    • The “+ New” button in the toolbar → Event is not triggered.
  1. Observe the FlexForm behavior and verify whether BeforeFlexFormDataStructureParsedEvent is triggered.

Expected behavior:
The BeforeFlexFormDataStructureParsedEvent should be triggered consistently for all methods of creating new content elements, ensuring proper FlexForm initialization.
*TYPO3 version:*
13.4.17
*PHP version:*
8.3.24
*Additional context:*
This issue affects custom content elements that rely on FlexForm manipulation via the mentioned event. It may also impact extensions or integrations that depend on dynamic FlexForm structures.

Reviews 0

No change resolves or names this issue in its commit message, and no comment links one.

History 4

  1. Theo Langstraat commented 09/27/25, 06:50

    *Temporary workaround:*
    As a workaround, the issue can be avoided by following these steps:
    1. Create the content element using any of the buttons (even if the event is not triggered).
    2. Save the newly created element.
    3. Reopen it for editing.

    When editing an existing content element, the BeforeFlexFormDataStructureParsedEvent is consistently triggered, and the FlexForm behaves as expected. This workaround ensures that the correct FlexForm structure is applied, even if it wasn’t initialized properly during creation.

  2. Theo Langstraat commented 09/27/25, 07:32

    After new tests, I found out that the event is triggered consistently, but pageid is not filled in consistently.
    For now I try to solve this problem myself. I have to fill the values in FlexFormContextStorage earlier in the process.

        public function setDataStructure(BeforeFlexFormDataStructureParsedEvent $event): void
        {
            $identifier = $event->getIdentifier();
    
            $contentType = FlexFormContextStorage::get('contentType');
            $pageId = FlexFormContextStorage::get('pageId');
    
            // The first part is the list_type for which the flexform should be used and the second part the CType. 
            // If one of the parts is empty or a '*' then it matches any value. 
            // In this case we only care about the CType which should be 'dailyverses_dailyverses'.
    
            // Filter UIDno out '*,dailyverses_dailyverses' to check exact CType
            $haystack = $identifier['dataStructureKey'] ?? '';
            $needle = 'dailyverses_dailyverses';
    
            if (str_contains($haystack, $needle)) {
    
                if ($pageId > 0) {
                    try {
    
                        $siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
                        $site = $siteFinder->getSiteByPageId($pageId);
                        $siteIdentifier = $site->getIdentifier();
                        $event->setDataStructure('FILE:EXT:dailyverses/Configuration/FlexForms/'. $siteIdentifier . '.xml');
    
                    } catch (\TYPO3\CMS\Core\Exception\SiteNotFoundException $e) {
                    }
                }
            }
        }
        public function setDataStructureIdentifier(BeforeFlexFormDataStructureIdentifierInitializedEvent $event): void
        {
            $identifier = $event->getIdentifier();
            $row = $event->getRow();
            FlexFormContextStorage::set('pageId', (int)($row['pid'] ?? 0));
            FlexFormContextStorage::set('languageUid', $row['sys_language_uid'] ?? 0);
            FlexFormContextStorage::set('contentType', $row['CType'] ?? '');
        }
    <?php declare(strict_types=1);
    
    namespace Theolangstraat\Dailyverses\Configuration\EventListener;
    
    class FlexFormContextStorage
    {
        protected static array $storage = [];
    
        public static function set(string $key, mixed $value): void
        {
            self::$storage[$key] = $value;
        }
    
        public static function get(string $key): mixed
        {
            return self::$storage[$key] ?? null;
        }
    
        public static function reset(): void
        {
            self::$storage = [];
        }
    }
  3. Theo Langstraat commented 09/28/25, 18:54

    Event listener rewritten*
    I rewrote my event listener, and this solves my problem.
    Everything is now working properly. *This ticket is therefore no longer relevant and can be closed.

    Instead of using $row[‘pid’] directly, I found out that it is not always a valid PID and you have to know how TYPO3 handles new records.

    public function setDataStructureIdentifier(BeforeFlexFormDataStructureIdentifierInitializedEvent $event): void
        {
            $identifier = $event->getIdentifier();
            $row = $event->getRow();
            $uidPid = $row['pid'];
    
            // $row['pid'] contains a real pid if the value is positive
            // and a negative value if a temporary uid has been created
            // example:
            // "pid": -85, (= uid of the previous content element)
            // "uid": "NEW68d97908397bc949195186",
    
            if ($uidPid > 0) {
                // It's a real pid
                $pid = $uidPid;
            } else {
                // It's a negative uid, so we need to retrieve the actual pid
                $uid = abs($uidPid);
    
                $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
                    ->getQueryBuilderForTable('tt_content');
    
                $pid = $queryBuilder
                    ->select('pid')
                    ->from('tt_content')
                    ->where(
                        // $queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uid, \PDO::PARAM_INT))
                        $queryBuilder->expr()->eq(
                            'uid',
                            $queryBuilder->createNamedParameter($uid, ParameterType::INTEGER)
                        )
                    )
                    ->executeQuery()
                    ->fetchOne();
            }
    
            FlexFormContextStorage::set('pageId', (int)$pid ?? 0);
            FlexFormContextStorage::set('languageUid', $row['sys_language_uid'] ?? 0); // future use
            FlexFormContextStorage::set('contentType', $row['CType'] ?? ''); // future use
        }
  4. Garvin Hicking commented and changed status New Closed 09/28/25, 19:03

    Thanks for your very good report and understandable issue – I’m happy you found a solution (which also sounds right to me). Closing as requested :-)