From fc554927993815ba3819a1359414ede4bd1f8d00 Mon Sep 17 00:00:00 2001 From: Joel Alphonso Date: Wed, 8 May 2024 16:33:54 -0400 Subject: [PATCH] fix(cookie-consent): fix cookie-consent error when consent is disabled changes: - add active check - deactivated consent settings no longer throws an error - add a template trait to help with front-end formatting --- .../CookieConsent/CookieConsentManager.php | 16 +++- .../CookieConsent/HasCookieConsentTrait.php | 74 +++++++++++++++++++ 2 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 src/Charcoal/CookieConsent/HasCookieConsentTrait.php diff --git a/src/Charcoal/CookieConsent/CookieConsentManager.php b/src/Charcoal/CookieConsent/CookieConsentManager.php index a03d85a..4052c56 100644 --- a/src/Charcoal/CookieConsent/CookieConsentManager.php +++ b/src/Charcoal/CookieConsent/CookieConsentManager.php @@ -3,6 +3,7 @@ namespace Charcoal\CookieConsent; use Charcoal\CookieConsent\Exception\InvalidArgumentException; +use Charcoal\CookieConsent\Exception\ModelNotFoundException; use Charcoal\CookieConsent\Model; use Charcoal\CookieConsent\Model\Repository\DisclosureRepository; use Charcoal\CookieConsent\Config\PluginConfig; @@ -42,6 +43,14 @@ public function __construct( $this->translator = $translator; } + /** + * @return bool + */ + public function isCookieConsentActive(): bool + { + return !!$this->getPluginOptions(); + } + /** * @return string */ @@ -67,7 +76,12 @@ public function getPluginOptions(): array */ public function createPluginOptions(): array { - $disclosureInstance = $this->disclosureRepository->getDisclosure(); + try { + $disclosureInstance = $this->disclosureRepository->getDisclosure(); + } catch (ModelNotFoundException $e) { + return []; + } + if (!$disclosureInstance) { return []; } diff --git a/src/Charcoal/CookieConsent/HasCookieConsentTrait.php b/src/Charcoal/CookieConsent/HasCookieConsentTrait.php new file mode 100644 index 0000000..0bc04b2 --- /dev/null +++ b/src/Charcoal/CookieConsent/HasCookieConsentTrait.php @@ -0,0 +1,74 @@ +cookieConsent->getPluginOptionsAsJson(); + } + + public function hasCookieConsent(): bool + { + return $this->cookieConsent->isCookieConsentActive(); + } + + public function cookieConsentScriptTag(): string + { + if (!$this->hasCookieConsent()) { + return 'type=text/javascript'; + } + + return 'type=text/plain'; + } + + /** + * twig syntax + * + * @return string + */ + public function getCookieConsentConfigAsJson(): string + { + return $this->cookieConsentConfigAsJson(); + } + + /** + * twig syntax + * + * @return string + */ + public function getCookieConsentScriptTag(): string + { + return $this->cookieConsentScriptTag(); + } + + /** + * twig syntax + * + * @return string + */ + public function getHasCookieConsent(): bool + { + return $this->hasCookieConsent(); + } + + /** + * @param Container|CookieConsentManager $cookieConsent + * @return void + */ + public function setCookieConsent($cookieConsent) + { + $this->cookieConsent = $cookieConsent instanceof Container ? $cookieConsent['cookie-consent'] : $cookieConsent; + } +}