Sen*_*reT 6 signals-slots extbase typo3-6.2.x
我在TYPO3 V6.2中有一个可用的Extbase扩展,它存储了产品.现在我想学习使用Signal/Slot(Hooks的Extbase变体).我想知道为什么这个例子不起作用.当我在TYPO3后端的列表模块中更新产品时,它会正确保存,但不会显示任何消息.
文件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
);
Run Code Online (Sandbox Code Playgroud)
文件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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
更新15.06.2015
Patrick Lobacher的一个提示说,我们不能在这种情况下使用die().相反,我们应该编写一个日志文件.但这对我也不起作用.没有写入文件:
文件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);
});
Run Code Online (Sandbox Code Playgroud)
更新29.06.2015
在 https://forge.typo3.org/issues/61979上, Francois写道,"对象管理器只能在Extbase上下文中使用,而不能在ext_localconf.php中使用".但是,给定的答案甚至对我不起作用.但也许它可以帮助别人.
目前没有官方文档,但在问题中你可以找到非官方文档: https: //forge.typo3.org/issues/59089
问题是您在列表模块中使用 Extbase 的信号槽。在 6.2 中,列表模块不是使用 Extbase 实现的。所以没有可以使用的插槽。相反,您需要遵循使用 Hooks 的旧的、记录的方式: https: //docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Hooks/Concept/Index.html
在您的情况下,以下代码应该作为入口点:
ext_localconf.php:
$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tcemain.php']['processDatamapClass'][$_EXTKEY]
= 'Vendor\ExtName\Hook\DataMapHook';
Run Code Online (Sandbox Code Playgroud)
在 TYPO3 6.2 之前,您可以将类配置为t3lib_tcemain旧类名的 Hook,处理的不仅仅是列表视图的数据。
在你的类中,你可以像你已经完成的那样实现你的代码:
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, e.g. 'update'.
* @param string $table The table affected by action, e.g. '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;
}
}
Run Code Online (Sandbox Code Playgroud)
是的,您可以die在这种情况下使用,用于调试等。因为 TYPO3 只会迭代配置的钩子并调用方法。所以这里没什么特别的。您可以获得一些由实现定义的参数并可以使用它们。
在上面的示例中,有一项检查仅在表和操作匹配时才执行挂钩。由于此代码被调用的原因有很多,因此您请确保将其列入白名单,以便仅在您了解的环境中执行。出于安全和性能原因。