Skip to content

Commit

Permalink
Add cascade hydration for custom route/controller/blade implementatio…
Browse files Browse the repository at this point in the history
…ns (#303)
  • Loading branch information
jesseleite committed Nov 16, 2023
1 parent fadca17 commit 939a609
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Directives/SeoProDirective.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ protected function isMissingContext($context)

protected function getContextFromCascade()
{
return Cascade::instance()->toArray();
$cascade = Cascade::instance();

// If the cascade has not yet been hydrated, ensure it is hydrated.
// This is important for people using custom route/controller/view implementations.
if (empty($cascade->toArray())) {
$cascade->hydrate();
}

return $cascade->toArray();
}

protected function getContextFromCurrentRouteData()
Expand Down
27 changes: 27 additions & 0 deletions tests/MetaTagTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use Facades\Statamic\View\Cascade as StatamicViewCacade;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Route;
Expand All @@ -27,6 +28,14 @@ protected function getEnvironmentSetUp($app)
'title' => 'The View',
'description' => 'A wonderful view!',
]);

Route::get('custom-get-route', function () {
StatamicViewCacade::hydrated(function ($cascade) {
$cascade->set('title', 'Custom Route Entry Title');
});

return view('page');
});
});
}

Expand Down Expand Up @@ -746,6 +755,24 @@ public function it_generates_normalized_meta_from_custom_site_defaults_path($vie
$this->assertStringContainsString($this->normalizeMultilineString($expected), $content);
}

/**
* @test
*
* @dataProvider viewScenarioProvider
*/
public function it_hydrates_cascade_on_custom_routes_using_blade_directive($viewType)
{
if ($viewType === 'antlers') {
$this->markTestSkipped();
}

$this->prepareViews($viewType);

$content = $this->get('/custom-get-route')->content();

$this->assertStringContainsString('<title>Custom Route Entry Title | Site Name</title>', $content);
}

protected function setCustomGlidePresetDimensions($app)
{
$app->config->set('statamic.seo-pro.assets', [
Expand Down

0 comments on commit 939a609

Please sign in to comment.