Falk Falk -4 years ago 110
MySQL Question

Adding rows to table during extension installation TYPO3

I'm trying to add some data rows to table tx_gridelements_backend_layout during installation of my own extension. For that I'm using the file "ext_tables_static+adt.sql" in my extension dir.

But this works only if I drop the table and create it again before inserting data. But then all data which might be allready in there, is lost. Thats definitely not what I want. I want to just add some rows additional to the existing data.

How can I achieve that?

The other question is, what if somebody clears the table, how do they realize that they have to remove my extension from the sys_registry table and install it again, so all rows will be added again?

I assume there is a better way doing stuff like this, or?

Answer Source

So the soultion is to an Signal Extension Manager is emitting after installation. For that in my ext_localconf.php I added:

if (TYPO3_MODE === 'BE') {
    $class = 'TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher';
    $dispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance($class);
    $dispatcher->connect(
    'TYPO3\\CMS\\Extensionmanager\\Service\\ExtensionManagementService',
        'hasInstalledExtensions',
        'FalkRoeder\\GridelementsFlex\\Setup\\Setup',
        'addBackendLayouts'
    );
}

and my Class FalkRoeder/gridelements_flex/Classes/Setup/Setup.php:

class Setup
{

    protected $layouts = [
       1 => [
           'title'  => 'Flexbox 50 50',
           'config'  => 'backend_layout {\r\n    colCount = 4\r\n    rowCount = 1\r\n    rows {\r\n        1 {\r\n            columns {\r\n                1 {\r\n                    name = links außen\r\n                    colPos = 1\r\n                }\r\n                2 {\r\n                    name = links innen\r\n                    colPos = 2\r\n                }\r\n                3 {\r\n                    name = rechts innen\r\n                    colPos = 3\r\n                }\r\n                4 {\r\n                    name = rechts außen\r\n                    colPos = 4\r\n                }\r\n            }\r\n        }\r\n    }\r\n}\r\n',
           'pi_flexform_ds_file'  => 'typo3conf/ext/gridelements_flex/Configuration/Flexforms/two_column_flex.xml'
       ]
    ];

    public function addBackendLayouts( $extname = null )
    {
        if( $extname !== 'gridelements_flex' ) {
           return;
        }

        foreach ($this->layouts as $layout) {

            list($row) = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('COUNT(*) AS t', 'tx_gridelements_backend_layout', "title = '" . $layout['title'] . "'");
            $count = (int)$row['t'];

            if($count == 0) {
                $GLOBALS['TYPO3_DB']->exec_INSERTquery(
                    'tx_gridelements_backend_layout',
                    $layout
                );
            }
        }

    }
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download