TYPO3 Extbase - how to use the main signals / slots

I have an Extbase extension in TYPO3 V6.2 that stores products. Now I want to learn about using Signal / Slot (option Extbase Hooks). I wonder why this example does not work. When I update a product in the List module in TYPO3 Backend, it saves correctly, but a message does not appear.

File typo3conf / ext / myext / ext_localconf.php

$signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance( 'TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher' ); $signalSlotDispatcher->connect( 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', 'afterUpdateObject', 'MyVendor\\MyExt\\Service\\Signalservice', 'myAfterUpdate', FALSE ); 

File typo3conf / ext / myext / Service / Signalservice.php

 namespace MyVendor\MyExt\Service; class Signalservice implements \TYPO3\CMS\Core\SingletonInterface { /** * @param \TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object */ public function myAfterUpdate(\TYPO3\CMS\Extbase\DomainObject\DomainObjectInterface $object){ if ($object instanceof \MyVendor\MyExt\Domain\Model\Products) { // check if we come to this point \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump('Successfully hooked - I am a slot.'); die(); } } } 


Update 06.15.2015
A hint from Patrick Lobacher noted that we cannot use die () in this context. Instead, we should write a log file. But this also does not work for me. The file was not written:

File typo3conf / ext / myext / ext_localconf.php

 /** * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher * */ $signalSlotDispatcher = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance('TYPO3\\CMS\\Extbase\\Object\\ObjectManager')->get('TYPO3\\CMS\\Extbase\\SignalSlot\\Dispatcher'); $signalSlotDispatcher->connect( 'TYPO3\\CMS\\Extbase\\Persistence\\Generic\\Backend', 'afterUpdateObject', function ($payload) { $logfile = "fileadmin/test/logfile.txt"; $handle = fopen($logfile, "a+"); fwrite ($handle, 'Hi. I was written by ext_localconf.php. ' . time()); fclose ($handle); }); 


Update 06.29.2015
At https://forge.typo3.org/issues/61979, Franรงois wrote that "Object Manager can only be used in Extbase Context, and not in ext_localconf.php." However, this answer does not even work for me. But maybe this helps someone else.

+6
source share
1 answer

There is currently no official documentation, but in this release you can find unofficial documentation: https://forge.typo3.org/issues/59089

The problem is that you are using Extbase signal slots in the Module list. In 6.2, the list module is not implemented using Extbase. Thus, there are no slots that you can use. Instead, you need to follow the old documented way to use Hooks: https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Hooks/Concept/Index.html

In your case, the following code should work as an entry point:

ext_localconf.php :

 $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][$_EXTKEY] = 'Vendor\ExtName\Hook\DataMapHook'; 

Here you set up the class to use as the t3lib_tcemain for the t3lib_tcemain old class, before TYPO3 6.2 processes more than just the data to represent the list.

Inside your class, you can implement your code, as already done by you:

Classes/Hook/DataMapHook.php :

 <?php namespace Vendor\ExtName\Hook; /** * Hook to process updated records. * * @author Daniel Siepmann < d.siepmann@web-vision.de > */ class DataMapHook { /** * Hook to add latitude and longitude to locations. * * @param string $action The action to perform, eg 'update'. * @param string $table The table affected by action, eg 'fe_users'. * @param int $uid The uid of the record affected by action. * @param array $modifiedFields The modified fields of the record. * * @return void */ public function processDatamap_postProcessFieldArray( $action, $table, $uid, array &$modifiedFields ) { if(!$this->executeHook($table, $action)) { return; } // check if we come to this point \TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump('Successfully hooked - I am a slot.'); die(); } /** * Check whether to execute hook or not. * * @param string $table * @param string $action * @param array $modifiedFields * * @return bool */ protected function executeHook($table, $action) { // Do not process if foreign table, unintended action, // or fields were changed explicitly. if ($table !== 'tx_extname_domain_model_modelname' || $action !== 'update') { return false; } return false; } } 

And yes, you can use die in this context for debugging, etc. Because TYPO3 will just iterate over the configured hooks and invoke methods. So nothing out of the ordinary here. You get some parameters defined by the implementation, and you can work with them.

In the above example, there is one check only to execute the hook if the table and action match. Since this code is called for many reasons, you must whitelist it so that it can only be executed in environments that you know about. For security and performance reasons.

+3
source

Source: https://habr.com/ru/post/988214/


All Articles