Skip to content

Commit

Permalink
Merge pull request #247 from statikbe/issue-239
Browse files Browse the repository at this point in the history
#239: hide cookiebanner for search engines
  • Loading branch information
janhenckens authored Feb 1, 2024
2 parents 562e70f + 16ed7a1 commit 1d9c495
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 23 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"craftcms/postmark": "3.0.0",
"hybridinteractive/craft-position-fieldtype": "4.0.0",
"hybridinteractive/craft-width-fieldtype": "^4.0",
"jaybizzle/crawler-detect": "^1.2",
"mikehaertl/php-shellcommand": "^1.6",
"mmikkel/cp-field-inspect": "1.4.4",
"nystudio107/craft-imageoptimize": "4.0.5",
Expand Down
54 changes: 53 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 13 additions & 17 deletions modules/statik/src/web/twig/StatikExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

namespace modules\statik\web\twig;

use Jaybizzle\CrawlerDetect\CrawlerDetect;
use Twig\Extension\AbstractExtension;
use Twig\Extension\GlobalsInterface;
use Twig\TwigFilter;
use Twig\TwigFunction;
use Twig\TwigTest;
use craft\elements\Entry;
use craft\helpers\ElementHelper;

class StatikExtension extends AbstractExtension
class StatikExtension extends AbstractExtension implements GlobalsInterface
{
private const SECTIONS_NO_INDEX_NO_FOLLOW = [
'searchResults',
Expand All @@ -25,18 +26,17 @@ class StatikExtension extends AbstractExtension
'setPasswordConfirmation',
];

private CrawlerDetect $crawlerDetect;

public function getFilters(): array
public function __construct()
{
return [
new TwigFilter('slugify', [$this, 'slugify']),
];
$this->crawlerDetect = new CrawlerDetect();
}

public function getTests(): array
public function getFilters(): array
{
return [
new TwigTest('isBot', [$this, 'isBot']),
new TwigFilter('slugify', [$this, 'slugify']),
];
}

Expand All @@ -47,19 +47,15 @@ public function getFunctions(): array
];
}


public function isBot(string $userAgent = '/bot|crawl|facebook|google|slurp|spider|mediapartners/i'): bool
public function getGlobals(): array
{
if (isset($_SERVER['HTTP_USER_AGENT'])) {
return $_SERVER['HTTP_USER_AGENT'] && preg_match($userAgent, $_SERVER['HTTP_USER_AGENT']);
}
return false;
return [
'isBot' => $this->crawlerDetect->isCrawler(),
];
}

/**
* Create slugs from titles in contentbuilder for the anchor link
* @param $string
* @return string
* remove all non-alphanumeric characters and replace spaces with dashes
*/
public function slugify(string $string): string
{
Expand Down
11 changes: 6 additions & 5 deletions templates/_site/_layout.twig
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,12 @@
{% endif %}

{# Cookiebanner #}
{{ craft.cookieBanner.render({
supportIE: true,
showCookieBanner: (entry.type is defined and entry.type.handle != 'cookiePolicy')
}) }}

{% if not isBot %}
{{ craft.cookieBanner.render({
supportIE: true,
showCookieBanner: (entry.type is defined and entry.type.handle != 'cookiePolicy'),
}) }}
{% endif %}
<div class="flex flex-col min-h-screen page-wrap " id="mainContentBlock">

<div class="sr-only skip-to-main ie-sr-only">
Expand Down

0 comments on commit 1d9c495

Please sign in to comment.