Skip to content

Commit

Permalink
[TASK] Update assets injection to AssetCollector
Browse files Browse the repository at this point in the history
  • Loading branch information
featdd committed Dec 21, 2023
1 parent 19863e7 commit 51ee941
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions Classes/Hook/RenderPreProcessHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
use Mindshape\MindshapeCookieConsent\Utility\RenderUtility;
use Mindshape\MindshapeCookieConsent\Utility\SettingsUtility;
use TYPO3\CMS\Core\Http\ApplicationType;
use TYPO3\CMS\Core\Page\AssetCollector;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Site\Entity\SiteLanguage;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\PathUtility;

/**
* @package Mindshape\MindshapeCookieConsent\Hook
Expand Down Expand Up @@ -57,6 +57,9 @@ public function preProcess(array &$params, PageRenderer $pageRenderer): void
$settings = SettingsUtility::pluginTypoScriptSettings();

if (true === is_array($settings) && false === (bool)$settings['disableConsent']) {
/** @var \TYPO3\CMS\Core\Page\AssetCollector $assetCollector */
$assetCollector = GeneralUtility::makeInstance(AssetCollector::class);

if (true === (bool)$settings['addConfiguration']) {
$javaScriptConfiguration = [
'cookieName' => $settings['cookieName'] ?? CookieUtility::DEFAULT_COOKIE_NAME,
Expand All @@ -79,19 +82,32 @@ public function preProcess(array &$params, PageRenderer $pageRenderer): void
: $pageRenderer->getLanguage();
}

$pageRenderer->addHeaderData('<script data-ignore="1">const cookieConsentConfiguration = JSON.parse(\'' . json_encode($javaScriptConfiguration) . '\');</script>');
$assetCollector->addInlineJavaScript(
'mindshape_cookie_consent_settings',
'const cookieConsentConfiguration = JSON.parse(\'' . json_encode($javaScriptConfiguration) . '\');',
['data-ignore' => '1'],
[
'useNonce' => true,
'priority' => true
]
);
}

if (true === (bool)$settings['addJavaScript']) {
$pageRenderer->addJsFooterLibrary(
'cookie_consent',
PathUtility::getPublicResourceWebPath('EXT:mindshape_cookie_consent/Resources/Public/JavaScript/cookie_consent.js')
$assetCollector->addJavaScript(
'mindshape_cookie_consent',
'EXT:mindshape_cookie_consent/Resources/Public/JavaScript/cookie_consent.js',
[],
['useNonce' => true]
);
}

if (true === (bool)$settings['addStylesheet']) {
$pageRenderer->addCssFile(
PathUtility::getPublicResourceWebPath('EXT:mindshape_cookie_consent/Resources/Public/Stylesheet/cookie_consent.css')
$assetCollector->addStyleSheet(
'mindshape_cookie_consent',
'EXT:mindshape_cookie_consent/Resources/Public/Stylesheet/cookie_consent.css',
[],
['useNonce' => true]
);
}

Expand Down

1 comment on commit 51ee941

@emileblume
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @featdd
Thanks for your quick reply! I was able to work around this by adding the generated SHA to the csp.yaml but with this update that's not necessary anymore.

Please sign in to comment.