diff --git a/CHANGELOG.md b/CHANGELOG.md index 36fc45b..178dd52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,25 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [2.4.0] - 2021-01-26 +### Added +- Add `craft.tarteaucitron.javascriptImportTag()` to output the tarteaucitron.js + import tag. See [#9][ticket-9]. +- Add `craft.tarteaucitron.javascriptConfigTag()` to output the tarteaucitron.js + configuration inline JS tag. See [#9][ticket-9]. +- Add `craft.tarteaucitron.stylesheetTag()` to output the tarteaucitron.js + stylesheet import tag. See [#9][ticket-9]. + +### Changed +- `craft.tarteaucitron.initScript()` now renders tarteaucitron.js import tag & + tarteaucitron.js configuration inline JS tag in place. + It used to render at the end of the `
` tag, no matter where it was + called in the source. + +### Fixed +- Honor the `useExternalCss` setting. See [#9][ticket-9]. + + ## [2.3.1] - 2020-12-01 ### Fixed - Fix reference error when reCaptcha service is enabled @@ -67,7 +86,8 @@ Updated tarteaucitron.js (we're currently using the ## [2.0.0] - 2020-31-01 First public release -[Unreleased]: https://github.com/la-haute-societe/craft-tarteaucitron/compare/2.3.1...HEAD +[Unreleased]: https://github.com/la-haute-societe/craft-tarteaucitron/compare/2.4.0...HEAD +[2.4.0]: https://github.com/la-haute-societe/craft-tarteaucitron/compare/2.3.1...2.4.0 [2.3.1]: https://github.com/la-haute-societe/craft-tarteaucitron/compare/2.3.0...2.3.1 [2.3.0]: https://github.com/la-haute-societe/craft-tarteaucitron/compare/2.2.0...2.3.0 [2.2.0]: https://github.com/la-haute-societe/craft-tarteaucitron/compare/2.1.1...2.2.0 @@ -77,3 +97,4 @@ First public release [2.0.1]: https://github.com/la-haute-societe/craft-tarteaucitron/compare/2.0.0...2.0.1 [2.0.0]: https://github.com/la-haute-societe/craft-tarteaucitron/releases/tag/2.0.0 [nstCactus/tarteaucitron.js]: https://github.com/nstCactus/tarteaucitron.js +[ticket-9]: https://github.com/la-haute-societe/craft-tarteaucitron/issues/9 diff --git a/README.md b/README.md index 0793ba4..330dfde 100644 --- a/README.md +++ b/README.md @@ -100,7 +100,7 @@ Example : If for some reason, you'd rather not use the `craft.tarteaucitron.xxx()` methods to instantiate your services, you can always add the tarteaucitron.js -placeceholder element by yourself. +placeholder element by yourself. These two examples will have the exact same result (you still need to enable the Vimeo service in the plugin settings): @@ -127,9 +127,9 @@ Vimeo service in the plugin settings): ### Checking if a service is enabled -You may need to check whether or not a service is enabled (as in "enabled in the -plugin settings", this has nothing to do with whether or not the user has given -its consent) : +You may need to check whether a service is enabled (as in "enabled in the +plugin settings", this has nothing to do with whether the user has given +its consent): ````twig {% if not craft.tarteaucitron.isTwitterEnabled() %} @@ -173,8 +173,26 @@ tarteaucitron.services.vimeo[tarteaucitron.state.vimeo ? 'js' : 'fallback'](); ### JS - Reacting to the user giving its consent to a service -Unfortunately, there is no clean way to do this at the moment but a PR will be -submitted to try and improve tarteaucitron.js. +Unfortunately, there is no clean way to do this at the moment, but a PR will be +submitted to try to improve tarteaucitron.js. + +### Customizing JS output in the page + +Instead of using `{{ craft.tarteaucitron.initScript() }}` to output both the +tarteaucitron.js tag and the configuation JS tag, use: + - `{{ craft.tarteaucitron.javascriptImportTag() }}` to output just the + tarteaucitron.js import tag + - `{{ craft.tarteaucitron.javascriptConfigTag() }}` to output just the + tarteaucitron.js configuration inline JS tag + +### Customizing CSS output + +By default, tarteaucitron.js imports the _css/tarteaucitron.css_ stylesheet +located next to the _tarteaucitron.js_ file. You can prevent this using the +`useCustomCss` setting of the plugin. +You can provide your own stylesheet or use +`{{ craft.tarteaucitron.stylesheetTag() }}` to output the tarteaucitron.js +stylesheet import tag ## Contribute diff --git a/composer.json b/composer.json index efa2495..624b4de 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "la-haute-societe/craft-tarteaucitron", "description": "Manage your sites' cookies with this tarteaucitron.js Craft CMS 3 integration", "type": "craft-plugin", - "version": "2.3.1", + "version": "2.4.0", "keywords": [ "craft", "cms", @@ -29,11 +29,6 @@ "lhs\\tarteaucitron\\": "src/" } }, - "config": { - "platform": { - "php": "7.1" - } - }, "support": { "email": "dev@lahautesociete.com", "issues": "https://github.com/la-haute-societe/craft-tarteaucitron/issues" diff --git a/src/bundles/FrontAsset.php b/src/bundles/FrontAsset.php index 8567f83..635095c 100644 --- a/src/bundles/FrontAsset.php +++ b/src/bundles/FrontAsset.php @@ -10,21 +10,13 @@ */ class FrontAsset extends AssetBundle { + /** + * Just publish the asset bundle but do not auto inject assets in the page + */ public function init() { - // define the path that your publishable resources live $this->sourcePath = '@lhs/tarteaucitron/resources/tarteaucitron'; - // define the relative path to CSS/JS files that should be registered with the page - // when this asset bundle is registered - $this->js = [ - 'tarteaucitron.js', - ]; - - $this->css = [ - 'css/tarteaucitron.css' - ]; - parent::init(); } } diff --git a/src/services/TarteaucitronService.php b/src/services/TarteaucitronService.php index f59bad9..103ee51 100644 --- a/src/services/TarteaucitronService.php +++ b/src/services/TarteaucitronService.php @@ -30,17 +30,31 @@ class TarteaucitronService extends Component * Call tarteaucitron init script * * @return Markup - * - * @throws \Twig\Error\LoaderError - * @throws \Twig\Error\RuntimeError - * @throws \Twig\Error\SyntaxError - * @throws \yii\base\Exception - * @throws \yii\base\InvalidConfigException */ public function renderInitScript(): Markup { - Craft::$app->getView()->registerAssetBundle(FrontAsset::class); + return new Markup($this->renderJavascriptImportTag() . $this->renderJavascriptConfigTag(), 'UTF-8'); + } + + /** + * Render tarteaucitron main JS tag + * @param array $options See [[HTML::tag()]] for details on how options are being used. + * @return Markup + * @throws \yii\base\InvalidConfigException + */ + public function renderJavascriptImportTag(array $options = []): Markup + { + $assetBundle = Craft::$app->getView()->registerAssetBundle(FrontAsset::class); + $options = array_merge(['src' => $assetBundle->baseUrl . '/tarteaucitron.js' ], $options); + return new Markup(Html::tag('script', null, $options), 'UTF-8'); + } + + /** + * Render tarteaucitron config JS tag + */ + public function renderJavascriptConfigTag(): Markup + { /** @noinspection NullPointerExceptionInspection */ $settings = Tarteaucitron::getInstance()->getSettings(); $html = $this->getInitHtml([ 'settings' => $settings ]); @@ -48,6 +62,23 @@ public function renderInitScript(): Markup return new Markup($html, 'UTF-8'); } + /** + * Render tarteaucitron stylesheet tag + * @param array $options See [[HTML::tag()]] for details on how options are being used. + * @return Markup + * @throws \yii\base\InvalidConfigException + */ + public function renderStylesheetTag(array $options = []): Markup + { + $assetBundle = Craft::$app->getView()->registerAssetBundle(FrontAsset::class); + $options = array_merge([ + 'rel' => 'stylesheet', + 'href' => $assetBundle->baseUrl . '/css/tarteaucitron.css', + ], $options); + + return new Markup(Html::tag('link', null, $options), 'UTF-8'); + } + /** * * @param array $vars diff --git a/src/templates/init.twig b/src/templates/init.twig index 9f7722a..6784b32 100755 --- a/src/templates/init.twig +++ b/src/templates/init.twig @@ -4,7 +4,7 @@ {{ settings.customCss }} {% endcss %} -{% js %} + diff --git a/src/variables/TarteaucitronVariable.php b/src/variables/TarteaucitronVariable.php index 42ccc16..8bdde13 100644 --- a/src/variables/TarteaucitronVariable.php +++ b/src/variables/TarteaucitronVariable.php @@ -19,6 +19,32 @@ public function initScript(): Markup return Plugin::getInstance()->tarteaucitron->renderInitScript(); } + /** + * @param array $options See [[HTML::tag()]] for details on how options are being used. + * @return Markup + */ + public function javascriptImportTag($options = []): Markup + { + return Plugin::getInstance()->tarteaucitron->renderJavascriptImportTag($options); + } + + /** + * @return Markup + */ + public function javascriptConfigTag(): Markup + { + return Plugin::getInstance()->tarteaucitron->renderJavascriptConfigTag(); + } + + /** + * @param array $options See [[HTML::tag()]] for details on how options are being used. + * @return Markup + */ + public function stylesheetTag($options = []): Markup + { + return Plugin::getInstance()->tarteaucitron->renderStylesheetTag($options); + } + /** * @param array $options * @return Markup