Skip to content

Commit 939a609

Browse files
authored
Add cascade hydration for custom route/controller/blade implementations (#303)
1 parent fadca17 commit 939a609

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

src/Directives/SeoProDirective.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,15 @@ protected function isMissingContext($context)
2626

2727
protected function getContextFromCascade()
2828
{
29-
return Cascade::instance()->toArray();
29+
$cascade = Cascade::instance();
30+
31+
// If the cascade has not yet been hydrated, ensure it is hydrated.
32+
// This is important for people using custom route/controller/view implementations.
33+
if (empty($cascade->toArray())) {
34+
$cascade->hydrate();
35+
}
36+
37+
return $cascade->toArray();
3038
}
3139

3240
protected function getContextFromCurrentRouteData()

tests/MetaTagTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Facades\Statamic\View\Cascade as StatamicViewCacade;
56
use Illuminate\Pagination\LengthAwarePaginator;
67
use Illuminate\Support\Facades\Artisan;
78
use Illuminate\Support\Facades\Route;
@@ -27,6 +28,14 @@ protected function getEnvironmentSetUp($app)
2728
'title' => 'The View',
2829
'description' => 'A wonderful view!',
2930
]);
31+
32+
Route::get('custom-get-route', function () {
33+
StatamicViewCacade::hydrated(function ($cascade) {
34+
$cascade->set('title', 'Custom Route Entry Title');
35+
});
36+
37+
return view('page');
38+
});
3039
});
3140
}
3241

@@ -746,6 +755,24 @@ public function it_generates_normalized_meta_from_custom_site_defaults_path($vie
746755
$this->assertStringContainsString($this->normalizeMultilineString($expected), $content);
747756
}
748757

758+
/**
759+
* @test
760+
*
761+
* @dataProvider viewScenarioProvider
762+
*/
763+
public function it_hydrates_cascade_on_custom_routes_using_blade_directive($viewType)
764+
{
765+
if ($viewType === 'antlers') {
766+
$this->markTestSkipped();
767+
}
768+
769+
$this->prepareViews($viewType);
770+
771+
$content = $this->get('/custom-get-route')->content();
772+
773+
$this->assertStringContainsString('<title>Custom Route Entry Title | Site Name</title>', $content);
774+
}
775+
749776
protected function setCustomGlidePresetDimensions($app)
750777
{
751778
$app->config->set('statamic.seo-pro.assets', [

0 commit comments

Comments
 (0)