El Devoper
El Devoper

Reputation: 1093

Can't get Signal / Slot to work

After a Youtube ID was saved, I'd like to get some API data and write it into the database. But my function never gets called. What I've tried so far:

ext_localconf.php

\TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher')->connect(
    'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend',
    'afterInsertObject',
    'VENDOR\\MyExtension\\Slots\\SaveVideo',
    'getVideoData'
);

Classes/Slots/SaveVideo.php

namespace VENDOR\MyExtension\Slots;

class SaveVideo {
    public function getVideoData($object) {     
        echo "Yeaha";
        var_dump($object);
    }
}

Upvotes: 1

Views: 232

Answers (1)

Lasse
Lasse

Reputation: 585

When you edit records in the backend then you do it through TCE (TYPO3 Core Engine) also known under the names TCEForm and TCEMain.

TCEForm

TCEForm generates a form from the TCA (Table Configuration Array) where you can edit and create new records.

TCEMain

TCEMain takes care of processing the data and store it in the database. It also takes care of commands like move, copy, delete, undelete, localize and version. TCEMain has a new name, DataHandler. The DataHandler class can be found here:

typo3/sysext/core/Classes/DataHandling/DataHandler.php

You can read more about TCE here

How to implement the hook

The DataHandler class has a lot of hooks where you can manipulate the data before it is stored in the backend. Try searching for $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS'] and you will find something like below.

if (is_array($GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'])) {

We can use this to registre oure hook in ext_localconf.php. Be aware that there are many hooks types available but it is the processDatamapClass we are interested in.

In you ext_localconf.php add:

$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][] = 'VendorName\\ExtentionName\\Hooks\\ProcessDatamapClass';

Create the file extension_name/Classes/Hooks/ProcessDatamapClass.php and at this content.

<?php
namespace VendorName\ExtentionName\Hooks;

class ProcessDatamapClass {
    public function processDatamap_postProcessFieldArray($status, $table, $id, &$fieldArray, $obj) {
        if($table == 'tx_extentionname_domain_model_yourtable') {
            // Your code goes here!
            // Note that $fieldArray is a reference so you can now modify the fields
            // before they are stored in the database
        }
    }
}

This code will now run every time you edit or creates a record in the backend. Therefore it is important to encapsulate your code for your record type / table name only!

I hope this helped you. You can read more about hooks here.

Upvotes: 1

Related Questions