Skip to content

Commit

Permalink
refactored hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
koertho committed Sep 13, 2023
1 parent c13a481 commit 7eae14f
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 113 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All notable changes to this project will be documented in this file.

## [1.5.0] - 2023-09-13
- Added: missing license file
- Changed: refactored hooks to current contao standard
- Fixed: issues with hook ordering

## [1.4.0] - 2023-08-23
- Added: support for additional url parameter for vimeo ([#18], [@zonky2])

Expand Down
30 changes: 7 additions & 23 deletions src/EventListener/InitializeSystemListener.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,31 @@
<?php

/*
* Copyright (c) 2020 Heimrich & Hannot GmbH
* Copyright (c) 2023 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\VideoBundle\EventListener;

use HeimrichHannot\UtilsBundle\Container\ContainerUtil;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use HeimrichHannot\UtilsBundle\Util\Utils;

/**
* @Hook("initializeSystem")
*/
class InitializeSystemListener
{
/**
* @var ContainerUtil
*/
protected $containerUtil;
private Utils $utils;

public function __construct(ContainerUtil $containerUtil)
public function __construct(Utils $utils)
{
$this->containerUtil = $containerUtil;
$this->utils = $utils;
}

public function __invoke(): void
{
$this->addBackendAssets();
$hookKeys = array_keys($GLOBALS['TL_HOOKS']['loadDataContainer']);

if (($hookPosition = array_search('huh_video', $hookKeys, true)) > array_search('multiColumnEditor', $hookKeys, true)) {
$tmp = $GLOBALS['TL_HOOKS']['loadDataContainer']['multiColumnEditor'];
unset($GLOBALS['TL_HOOKS']['loadDataContainer']['multiColumnEditor']);
array_insert($GLOBALS['TL_HOOKS']['loadDataContainer'], $hookPosition, ['multiColumnEditor' => $tmp]);

return;
}
}

protected function addBackendAssets(): void
{
if ($this->containerUtil->isBackend()) {
if ($this->utils->container()->isBackend()) {
$GLOBALS['TL_CSS']['be_videobundle'] = 'bundles/heimrichhannotvideo/assets/contao-video-bundle-be.css|static';
$GLOBALS['TL_JAVASCRIPT']['be_videobundle'] = 'bundles/heimrichhannotvideo/assets/contao-video-bundle-be.js|static';
}
Expand Down
23 changes: 9 additions & 14 deletions src/EventListener/LoadDataContainerListener.php
Original file line number Diff line number Diff line change
@@ -1,40 +1,35 @@
<?php

/*
* Copyright (c) 2022 Heimrich & Hannot GmbH
* Copyright (c) 2023 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\VideoBundle\EventListener;

use Contao\CoreBundle\DataContainer\PaletteManipulator;
use Contao\CoreBundle\ServiceAnnotation\Hook;
use HeimrichHannot\VideoBundle\EventListener\Dca\ConfigElementListener;
use HeimrichHannot\VideoBundle\EventListener\Dca\PageContainer;
use HeimrichHannot\VideoBundle\Generator\DcaFieldGenerator;

/**
* @Hook("loadDataContainer", priority=1)
*/
class LoadDataContainerListener
{
const PALETTE_VIDEO = 'videoProvider';
const PALETTE_PLAYER = 'videoFullsize,videoAutoplay';
public const PALETTE_VIDEO = 'videoProvider';
public const PALETTE_PLAYER = 'videoFullsize,videoAutoplay';

/**
* @var array
*/
private $bundleConfig;
private array $bundleConfig;

/**
* LoadDataContainerListener constructor.
*/
public function __construct(array $bundleConfig)
{
$this->bundleConfig = $bundleConfig;
}

/**
* Hook loadDataContainer.
*/
public function onLoadDataContainer(string $table): void
public function __invoke(string $table): void
{
switch ($table) {
case 'tl_news':
Expand Down
31 changes: 10 additions & 21 deletions src/EventListener/ParseArticlesListener.php
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
<?php

/*
* Copyright (c) 2020 Heimrich & Hannot GmbH
* Copyright (c) 2023 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\VideoBundle\EventListener;

use Contao\CoreBundle\ServiceAnnotation\Hook;
use Contao\FrontendTemplate;
use Contao\Module;
use HeimrichHannot\VideoBundle\Asset\FrontendAsset;
use HeimrichHannot\VideoBundle\Collection\VideoProviderCollection;
use HeimrichHannot\VideoBundle\Generator\VideoGenerator;
use HeimrichHannot\VideoBundle\Video\VideoInterface;

/**
* @Hook("parseArticles")
*/
class ParseArticlesListener
{
/**
* @var array
*/
private $bundleConfig;
/**
* @var VideoProviderCollection
*/
private $videoProviderCollection;
/**
* @var VideoGenerator
*/
private $videoGenerator;
/**
* @var FrontendAsset
*/
private $frontendAsset;
private array $bundleConfig;
private VideoProviderCollection $videoProviderCollection;
private VideoGenerator $videoGenerator;
private FrontendAsset $frontendAsset;

public function __construct(array $bundleConfig, VideoProviderCollection $videoProviderCollection, VideoGenerator $videoGenerator, FrontendAsset $frontendAsset)
{
Expand All @@ -42,10 +34,7 @@ public function __construct(array $bundleConfig, VideoProviderCollection $videoP
$this->frontendAsset = $frontendAsset;
}

/**
* Hook("parseArticles").
*/
public function onParseArticles(FrontendTemplate $template, array $newsEntry, Module $module): void
public function __invoke(FrontendTemplate $template, array $newsEntry, Module $module): void
{
if (!isset($this->bundleConfig['enable_news_support']) || true !== $this->bundleConfig['enable_news_support']) {
return;
Expand Down
20 changes: 8 additions & 12 deletions src/EventListener/PrivacyCenterListener.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php

/*
* Copyright (c) 2022 Heimrich & Hannot GmbH
* Copyright (c) 2023 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/
Expand All @@ -13,27 +13,24 @@
use HeimrichHannot\PrivacyCenterBundle\Generator\ProtectedCodeConfiguration;
use HeimrichHannot\PrivacyCenterBundle\Generator\ProtectedCodeGenerator;
use HeimrichHannot\PrivacyCenterBundle\Generator\SplashImage;
use HeimrichHannot\UtilsBundle\Model\ModelUtil;
use HeimrichHannot\UtilsBundle\Util\Utils;
use HeimrichHannot\VideoBundle\Event\AfterRenderPlayerEvent;
use Psr\Container\ContainerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Contracts\Service\ServiceSubscriberInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Environment;

class PrivacyCenterListener implements EventSubscriberInterface, ServiceSubscriberInterface
{
private ModelUtil $modelUtil;
private TranslatorInterface $translator;
private Environment $twig;
private ContainerInterface $container;
private Utils $utils;

public function __construct(ContainerInterface $container, ModelUtil $modelUtil, TranslatorInterface $translator, Environment $twig)
public function __construct(ContainerInterface $container, TranslatorInterface $translator, Utils $utils)
{
$this->modelUtil = $modelUtil;
$this->translator = $translator;
$this->twig = $twig;
$this->container = $container;
$this->utils = $utils;
}

public static function getSubscribedEvents()
Expand All @@ -54,10 +51,10 @@ public function afterRenderPlayer(AfterRenderPlayerEvent $event)
$videoProviderLocalStorage = [];

foreach ($localStorageAttributes as $item) {
if (null === $this->modelUtil->findModelInstancesBy('tl_tracking_object', 'id', $item['localStorageAttribute'])->localStorageAttribute) {
if (null === $this->utils->model()->findModelInstancesBy('tl_tracking_object', 'id', $item['localStorageAttribute'])->localStorageAttribute) {
continue;
}
$videoProviderLocalStorage[$item['videoProvider']] = $this->modelUtil->findModelInstancesBy('tl_tracking_object', 'id', $item['localStorageAttribute'])->localStorageAttribute;
$videoProviderLocalStorage[$item['videoProvider']] = $this->utils->model()->findModelInstancesBy('tl_tracking_object', 'id', $item['localStorageAttribute'])->localStorageAttribute;
}

if (!empty($event->getContext()['previewImage']) && isset($event->getContext()['previewImage']['src'])) {
Expand All @@ -78,8 +75,7 @@ public function afterRenderPlayer(AfterRenderPlayerEvent $event)
->setShowSplashImage(true)
->setShowPreview(true)
->setSplashImage($splashImage)
->setUnlockButtonText($this->translator->trans('huh_video.template.privacy.showContent'))
;
->setUnlockButtonText($this->translator->trans('huh_video.template.privacy.showContent'));

$event->setBuffer($this->container->get(ProtectedCodeGenerator::class)->generateProtectedCode(
$event->getBuffer(),
Expand Down
29 changes: 10 additions & 19 deletions src/EventListener/SqlGetDataListener.php
Original file line number Diff line number Diff line change
@@ -1,37 +1,28 @@
<?php

/*
* Copyright (c) 2020 Heimrich & Hannot GmbH
* Copyright (c) 2023 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\VideoBundle\EventListener;

use Contao\CoreBundle\ServiceAnnotation\Hook;

/**
* @Hook("sqlGetFromDca")
*/
class SqlGetDataListener
{
/**
* @var array
*/
private $bundleConfig;

/**
* LoadDataContainerListener constructor.
*/
private array $bundleConfig;

public function __construct(array $bundleConfig)
{
$this->bundleConfig = $bundleConfig;
}

/**
* Hook loadDataContainer.
*/
public function onSqlGetFromDca($sqlDcaData)
{
return $this->preparePageTable($sqlDcaData);
}

protected function preparePageTable($sqlDcaData)
public function __invoke($sqlDcaData)
{
return $this->enablePrivacyCenterSupport($sqlDcaData);
}
Expand All @@ -45,7 +36,7 @@ protected function enablePrivacyCenterSupport($sqlDcaData)
if (!class_exists('HeimrichHannot\MultiColumnEditorBundle\HeimrichHannotContaoMultiColumnEditorBundle')) {
trigger_error(
'HeimrichHannotContaoMultiColumnEditorBundle not found. Multi Column Editor bundle is needed for privacy center integration.',
E_USER_WARNING);
\E_USER_WARNING);

return $sqlDcaData;
}
Expand Down
19 changes: 2 additions & 17 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ services:
$bundleConfig: '%huh_video%'

HeimrichHannot\VideoBundle\:
resource: '../../{Asset,Controller}/*'
resource: '../../{Asset,Controller,EventListener}/*'
autoconfigure: true

HeimrichHannot\VideoBundle\Asset\FrontendAsset:
Expand All @@ -23,20 +23,5 @@ services:
resource: ../../EventListener/Dca/*
public: true

HeimrichHannot\VideoBundle\EventListener\PrivacyCenterListener:
autoconfigure: true

HeimrichHannot\VideoBundle\EventListener\LoadDataContainerListener:
public: true

HeimrichHannot\VideoBundle\EventListener\SqlGetDataListener:
public: true

HeimrichHannot\VideoBundle\EventListener\ParseArticlesListener:
public: true

HeimrichHannot\VideoBundle\ConfigElementType\VideoConfigElementType:
tags: ['huh.list.config_element_type', 'huh.reader.config_element_type']

HeimrichHannot\VideoBundle\EventListener\InitializeSystemListener:
public: true
tags: ['huh.list.config_element_type', 'huh.reader.config_element_type']
10 changes: 3 additions & 7 deletions src/Resources/contao/config/config.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
<?php

/*
* Copyright (c) 2020 Heimrich & Hannot GmbH
* Copyright (c) 2023 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

$GLOBALS['TL_CTE']['media'][\HeimrichHannot\VideoBundle\ContentElement\VideoElement::TYPE] = \HeimrichHannot\VideoBundle\ContentElement\VideoElement::class;
use HeimrichHannot\VideoBundle\ContentElement\VideoElement;

$GLOBALS['TL_HOOKS']['loadDataContainer']['huh_video'] = [\HeimrichHannot\VideoBundle\EventListener\LoadDataContainerListener::class, 'onLoadDataContainer'];
$GLOBALS['TL_HOOKS']['parseArticles'][] = [\HeimrichHannot\VideoBundle\EventListener\ParseArticlesListener::class, 'onParseArticles'];
$GLOBALS['TL_HOOKS']['sqlGetFromDca']['videoBundle'] = [\HeimrichHannot\VideoBundle\EventListener\SqlGetDataListener::class, 'onSqlGetFromDca'];

$GLOBALS['TL_HOOKS']['initializeSystem']['huh_video'] = [\HeimrichHannot\VideoBundle\EventListener\InitializeSystemListener::class, '__invoke'];
$GLOBALS['TL_CTE']['media'][VideoElement::TYPE] = VideoElement::class;

0 comments on commit 7eae14f

Please sign in to comment.