103 lines
3.2 KiB
PHP
Executable File
103 lines
3.2 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Evoweb\EwBase\Hooks;
|
|
|
|
/*
|
|
* This file is developed by evoWeb.
|
|
*
|
|
* It is free software; you can redistribute it and/or modify it under
|
|
* the terms of the GNU General Public License, either version 2
|
|
* of the License, or any later version.
|
|
*
|
|
* For the full copyright and license information, please read the
|
|
* LICENSE.txt file that was distributed with this source code.
|
|
*/
|
|
|
|
use Exception;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
|
|
use TYPO3\CMS\Core\Core\Environment;
|
|
use TYPO3\CMS\Core\Site\SiteFinder;
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility;
|
|
|
|
class UsercentricsPostRenderHook
|
|
{
|
|
private array $siteArguments = [
|
|
'id' => '',
|
|
'version' => 'loader',
|
|
'useBlocker' => false,
|
|
];
|
|
|
|
public function executePostRenderHook(array $params): void
|
|
{
|
|
$pageUid = $this->getRequest()->getAttribute('frontend.controller')->page['uid'] ?? 0;
|
|
if ($pageUid < 1) {
|
|
return;
|
|
}
|
|
|
|
$siteArguments = $this->getCurrentSiteArguments($pageUid);
|
|
$id = $siteArguments['id'] ?? '';
|
|
if ($id !== '') {
|
|
$extensionConfig = $this->getExtensionConfiguration();
|
|
|
|
$preload = [];
|
|
$usercentrics = [];
|
|
|
|
// CMP 1, 2, ...
|
|
$version = $siteArguments['version'] ?? '';
|
|
if ($config = $extensionConfig[$version] ?? false) {
|
|
$preload[] = $config['preload'] ?? '';
|
|
$usercentrics[] = $config['template'] ?? '';
|
|
}
|
|
|
|
// Blocker
|
|
if (
|
|
($siteArguments['useBlocker'] ?? false)
|
|
&& $config = $extensionConfig['block'] ?? false
|
|
) {
|
|
$preload[] = $config['preload'] ?? '';
|
|
$usercentrics[] = $config['template'] ?? '';
|
|
}
|
|
|
|
$params['titleTag'] .= str_replace('###ID###', $id, implode(LF, array_merge($preload, $usercentrics)));
|
|
}
|
|
}
|
|
|
|
protected function getCurrentSiteArguments(int $pageUid): array
|
|
{
|
|
$siteArguments = $this->siteArguments;
|
|
|
|
try {
|
|
/** @var SiteFinder $siteFinder */
|
|
$siteFinder = GeneralUtility::makeInstance(SiteFinder::class);
|
|
$site = $siteFinder->getSiteByPageId($pageUid);
|
|
foreach ($site->getAttribute('usercentrics') as $usercentrics) {
|
|
if (str_contains($usercentrics['applicationContext'] ?? '', (string)Environment::getContext())) {
|
|
$siteArguments = $usercentrics;
|
|
break;
|
|
}
|
|
}
|
|
} catch (Exception) {
|
|
}
|
|
|
|
return is_array($siteArguments) ? $siteArguments : [];
|
|
}
|
|
|
|
protected function getExtensionConfiguration(): array
|
|
{
|
|
/** @var ExtensionConfiguration $extensionConfiguration */
|
|
$extensionConfiguration = GeneralUtility::makeInstance(ExtensionConfiguration::class);
|
|
try {
|
|
$configuration = $extensionConfiguration->get('ew_base', 'userCentrics');
|
|
} catch (Exception) {
|
|
$configuration = [];
|
|
}
|
|
return is_array($configuration) ? $configuration : [];
|
|
}
|
|
|
|
protected function getRequest(): ServerRequestInterface
|
|
{
|
|
return $GLOBALS['TYPO3_REQUEST'];
|
|
}
|
|
}
|