-
Hook überschreiben
Hi zusammen,
ich möchte einen bestimmten Hook eines Packages überschreiben. Im genauen geht es um diesen Hook hier, aber das spielt glaube ich keine Rolle:
https://github.com/richardhj/contao-...tion/Hooks.php (checkBeforeAddToCollection)
Dieser ist so konfiguriert:
PHP-Code:
$GLOBALS['ISO_HOOKS']['addProductToCollection'][] = [
\Richardhj\Isotope\SimpleStockManagement\FrontendIntegration\Hooks::class,
'checkBeforeAddToCollection',
];
Kann ich einfach in der lokalen Anwendung diesen Hook überschreiben und meine eigene Klasse dazu schreiben? Gibt es dazu best practices oder ist das schon der richtige Ansatz?
Danke,
LG
Daniel
-
PHP-Code:
// contao/config.php
$GLOBALS['ISO_HOOKS']['addProductToCollection'] = array_filter($GLOBALS['ISO_HOOKS']['addProductToCollection'], function($hook) {
return \Richardhj\Isotope\SimpleStockManagement\FrontendIntegration\Hooks::class !== $hook[0] ?? '';
});
$GLOBALS['ISO_HOOKS']['addProductToCollection'][] = [/* your hook */];
ungetestet
Falls der andere Hook ein Service ist, könntest du ihn vielleicht auch decoraten.
-
@Spooky: Danke, das hat funktioniert - hat nur ein Semikolon nach dem return gefehlt, jetzt sieht der erste Teil so aus:
PHP-Code:
$GLOBALS['ISO_HOOKS']['addProductToCollection'] = array_filter(
$GLOBALS['ISO_HOOKS']['addProductToCollection'],
function($hook) {
return \Richardhj\Isotope\SimpleStockManagement\FrontendIntegration\Hooks::class !== $hook[0] ?? '';
}
);
-
Stand auch schon vor dem Problem. Eine Idee wäre es für die Zukunft, dass man Hooks von Anfang an so baut, dass sie deaktivierbar sind und dass es wie zum Standard wird.
In der Export Table Erweiterung bin ich diesen Weg gegangen.
https://github.com/markocupic/export...e-hook-steuern
PHP-Code:
// App/EventListener/ExportTable/FormatDateListener.php
declare(strict_types=1);
namespace App\EventListener\ExportTable;
use Contao\CoreBundle\Framework\ContaoFramework;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\Date;
use Markocupic\ExportTable\Config\Config;
use Markocupic\ExportTable\Listener\ContaoHooks\ExportTableFormatDateListener;
/**
* @Hook(MyCustomFormatDateListener::HOOK, priority=MyCustomFormatDateListener::PRIORITY)
*/
class MyCustomFormatDateListener
{
public const HOOK = 'exportTable';
public const PRIORITY = 100;
public static $disableHook = false;
private $framework;
public function __construct(ContaoFramework $framework)
{
$this->framework = $framework;
}
/**
* @param $varValue
*
* @return mixed
*/
public function __invoke(string $strFieldname, $varValue, string $strTablename, array $arrDataRecord, array $arrDca, Config $objConfig)
{
if (static::$disableHook) {
return false;
}
// Disable original Hook that is shipped with the export table extension.
ExportTableFormatDateListener::$disableHook = true;
$dateAdapter = $this->framework->getAdapter(Date::class);
$dca = $arrDca['fields'][$strFieldname] ?? null;
if ($dca) {
$strRgxp = $dca['eval']['rgxp'];
if ('' !== $varValue && $strRgxp && \in_array($strRgxp, ['date', 'datim', 'time'], true)) {
$dateFormat = $dateAdapter->getFormatFromRgxp($strRgxp);
$varValue = $dateAdapter->parse($dateFormat, $varValue);
}
}
return $varValue;
}
}