Skip to content

Commit

Permalink
Locale banner tag
Browse files Browse the repository at this point in the history
  • Loading branch information
afonic committed Feb 11, 2024
1 parent 5ba6cb0 commit 202a508
Show file tree
Hide file tree
Showing 6 changed files with 144 additions and 1 deletion.
1 change: 0 additions & 1 deletion config/locale-lander.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
return [
'enable_redirection' => true,

'type' => 'redirect',
];
31 changes: 31 additions & 0 deletions resources/views/_banner.antlers.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div id="sticky-banner" tabindex="-1"
class="fixed top-0 start-0 z-50 flex justify-between w-full p-4 border-b border-gray-200 bg-gray-50">
<div class="flex items-center mx-auto">
<p class="flex items-center text-sm font-normal text-gray-500">
<span
class="inline-flex p-1 me-3 bg-gray-200 rounded-full w-6 h-6 items-center justify-center flex-shrink-0">
<svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor"
class="w-6 h-6">
<path stroke-linecap="round" stroke-linejoin="round"
d="M3 3v1.5M3 21v-6m0 0 2.77-.693a9 9 0 0 1 6.208.682l.108.054a9 9 0 0 0 6.086.71l3.114-.732a48.524 48.524 0 0 1-.005-10.499l-3.11.732a9 9 0 0 1-6.085-.711l-.108-.054a9 9 0 0 0-6.208-.682L3 4.5M3 15V4.5" />
</svg>
<span class="sr-only">Flag</span>
</span>
<span>This page is also available in
<a href="{{ url }}" title="Page in {{site:name }}">
{{ site:name }}
</a>
</span>
</p>
</div>
<div class="flex items-center">
<button data-dismiss-target="#sticky-banner" type="button"
class="flex-shrink-0 inline-flex justify-center w-7 h-7 items-center text-gray-400 hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm p-1.5 dark:hover:bg-gray-600 dark:hover:text-white">
<svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14">
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6" />
</svg>
<span class="sr-only">Close banner</span>
</button>
</div>
</div>
11 changes: 11 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ class ServiceProvider extends AddonServiceProvider
],
];

protected $tags = [
Tags\LocaleBanner::class,
];

public function register()
{
$this->app->singleton('localehelper', function ($app) {
return new LocaleHelper();
});

$this->loadViewsFrom(__DIR__.'/../resources/views', 'locale-lander');

$this->publishes([
__DIR__.'/../resources/views' => resource_path('views/vendor/locale-lander'),
], 'locale-lander-views');

}
}
8 changes: 8 additions & 0 deletions src/Support/LocaleHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public function isSafe($content)
&& $content->url();
}

public function hasCookie(): bool
{
// Workaround because $request->hasCookie() is not working
$cookies = collect(request()->cookies->all());

return $cookies->has('locale_banner_closed');
}

public function setCompleted(): void
{
session(['locale_lander' => 'completed']);
Expand Down
42 changes: 42 additions & 0 deletions src/Tags/LocaleBanner.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Reach\LocaleLander\Tags;

use Reach\LocaleLander\Facades\LocaleHelper;
use Statamic\Tags\Tags;

class LocaleBanner extends Tags
{
public function index()
{
$helper = LocaleHelper::boot(request());

if ($helper->hasCookie() ||
$helper->isCurrentLocaleCorrect()) {

return $this->notFound();
}

$site = $helper->getMatchingSite();
if (! $site) {
return $this->notFound();
}

$content = $helper->findContentInLocale($site);
if ($content) {
return [
'entry' => $content->toAugmentedArray(['title', 'url']),
'site' => $site,
];
}

return $this->notFound();
}

protected function notFound()
{
return [
'entry' => false,
];
}
}
52 changes: 52 additions & 0 deletions tests/Feature/LocaleBannerTagTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

use Reach\LocaleLander\Tags\LocaleBanner;
use Reach\LocaleLander\Tests\CreatesEntries;
use Reach\LocaleLander\Tests\FakesViews;
use Reach\LocaleLander\Tests\PreventSavingStacheItemsToDisk;
use Reach\LocaleLander\Tests\TestCase;
use Statamic\Facades;
use Statamic\Facades\Antlers;

class LocaleBannerTagTest extends TestCase
{
use CreatesEntries, FakesViews, PreventSavingStacheItemsToDisk;

public $collection;

public $tag;

public function setUp(): void
{
parent::setUp();

$this->collection = Facades\Collection::make('pages')
->defaultPublishState('published')
->sites(['en', 'fr', 'de', 'gr'])
->routes('{parent_uri}/{slug}')
->structureContents([
'root' => true,
'max_depth' => 1,
])
->save();

$this->tag = (new LocaleBanner)
->setParser(Antlers::parser())
->setContext([]);
}

/** @test */
public function the_tag_returns_the_entry_if_locale_exists()
{
$this->createMultisiteEntries();

Facades\Stache::clear();

ray($this->tag->index());
}

private function setTagParameters($parameters)
{
$this->tag->setParameters($parameters);
}
}

0 comments on commit 202a508

Please sign in to comment.