Skip to content

Commit 561a43e

Browse files
edalzelljesseleite
andauthored
Add support for passing meta into view data for custom routes (#255)
Co-authored-by: Jesse Leite <jesseleite@gmail.com> Closes #254
1 parent 5990379 commit 561a43e

File tree

6 files changed

+73
-3
lines changed

6 files changed

+73
-3
lines changed

DOCUMENTATION.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,19 @@ If you wish to use assets in your meta, you can [publish the SEO Pro config](#pu
4444
4545
> You may disable the glide preset altogether by setting `'open_graph_preset' => false,` in your config.
4646
47+
### Custom Statamic Routes
48+
49+
In the case that you're loading a custom [Statamic Route](https://statamic.dev/routing#statamic-routes), you can pass SEO meta directly into the route data param. This allows you to define custom meta on a route-by-route basis in situations without a proper collection entry.
50+
51+
```php
52+
Route::statamic('search', 'search/index', [
53+
'title' => 'Search',
54+
'description' => 'Comprehensive Site Search.',
55+
// ...
56+
]);
57+
```
58+
59+
4760
## File Usage
4861

4962
For advanced devs, you may bypass the CP and configure your SEO settings through files. There are 3 sorts of values you may save.

src/Cascade.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Cascade
1717
{
1818
protected $data;
1919
protected $current;
20+
protected $explicitUrl;
2021
protected $model;
2122
protected $forSitemap = false;
2223

@@ -54,10 +55,18 @@ public function withCurrent($data)
5455
return $this;
5556
}
5657

58+
public function withExplicitUrl($url)
59+
{
60+
$this->explicitUrl = $url;
61+
62+
return $this;
63+
}
64+
5765
public function get()
5866
{
5967
if (! $this->current) {
6068
$this->withCurrent(Entry::findByUri('/'));
69+
$this->withExplicitUrl(request()->url());
6170
}
6271

6372
if ($this->forSitemap) {
@@ -113,7 +122,7 @@ public function value($key)
113122

114123
public function canonicalUrl()
115124
{
116-
$url = Str::trim($this->data->get('canonical_url'));
125+
$url = Str::trim($this->explicitUrl ?? $this->data->get('canonical_url'));
117126

118127
if (! app('request')->has('page')) {
119128
return $url;

src/Directives/SeoProDirective.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ class SeoProDirective extends SeoProTags
1010
public function renderTag($tag, $context)
1111
{
1212
if ($this->isMissingContext($context)) {
13-
$context = $this->getContextFromCascade();
13+
$context = array_merge(
14+
$this->getContextFromCurrentRouteData(),
15+
$this->getContextFromCascade()
16+
);
1417
}
1518

1619
return $this->setContext($context)->$tag();
@@ -25,4 +28,9 @@ protected function getContextFromCascade()
2528
{
2629
return Cascade::instance()->toArray();
2730
}
31+
32+
protected function getContextFromCurrentRouteData()
33+
{
34+
return app('router')->current()->parameter('data') ?? [];
35+
}
2836
}

src/Tags/SeoProTags.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ public function metaData()
4040
->with(SiteDefaults::load()->augmented())
4141
->with($this->getAugmentedSectionDefaults($current))
4242
->with($this->context->value('seo'))
43+
->with($current ? [] : $this->context->except('template_content'))
4344
->withCurrent($current)
4445
->get();
4546

tests/CascadeTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public function it_generates_seo_cascade_without_exception_when_no_home_entry_ex
145145
'change_frequency' => 'monthly',
146146
'compiled_title' => 'Site Name',
147147
'og_title' => 'Site Name',
148-
'canonical_url' => '',
148+
'canonical_url' => 'http://cool-runnings.com',
149149
'prev_url' => null,
150150
'next_url' => null,
151151
'home_url' => 'http://cool-runnings.com/',

tests/MetaTagTest.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Illuminate\Pagination\LengthAwarePaginator;
66
use Illuminate\Support\Facades\Artisan;
7+
use Illuminate\Support\Facades\Route;
78
use Statamic\Facades\Blink;
89
use Statamic\Facades\Collection;
910
use Statamic\Facades\Config;
1011
use Statamic\Facades\Entry;
12+
use Statamic\Statamic;
1113

1214
class MetaTagTest extends TestCase
1315
{
@@ -18,6 +20,13 @@ protected function getEnvironmentSetUp($app)
1820
parent::getEnvironmentSetUp($app);
1921

2022
$app['config']->set('view.paths', [$this->viewsPath()]);
23+
24+
Statamic::booted(function () {
25+
Route::statamic('the-view', 'page', [
26+
'title' => 'The View',
27+
'description' => 'A wonderful view!',
28+
]);
29+
});
2130
}
2231

2332
public function tearDown(): void
@@ -58,6 +67,36 @@ public function it_generates_normalized_meta($viewType)
5867
$this->assertStringContainsString($this->normalizeMultilineString($expected), $content);
5968
}
6069

70+
/**
71+
* @test
72+
* @dataProvider viewScenarioProvider
73+
*/
74+
public function it_generates_normalized_meta_when_visiting_statamic_route_with_raw_view_data($viewType)
75+
{
76+
$this->prepareViews($viewType);
77+
78+
$expected = <<<'EOT'
79+
<title>The View | Site Name</title>
80+
<meta name="description" content="A wonderful view!" />
81+
<meta property="og:type" content="website" />
82+
<meta property="og:title" content="The View" />
83+
<meta property="og:description" content="A wonderful view!" />
84+
<meta property="og:url" content="http://cool-runnings.com/the-view" />
85+
<meta property="og:site_name" content="Site Name" />
86+
<meta property="og:locale" content="en_US" />
87+
<meta name="twitter:card" content="summary_large_image" />
88+
<meta name="twitter:title" content="The View" />
89+
<meta name="twitter:description" content="A wonderful view!" />
90+
<link href="http://cool-runnings.com/" rel="home" />
91+
<link href="http://cool-runnings.com/the-view" rel="canonical" />
92+
<link type="text/plain" rel="author" href="http://cool-runnings.com/humans.txt" />
93+
EOT;
94+
95+
$content = $this->get('/the-view')->content();
96+
$this->assertStringContainsString("<h1>{$viewType}</h1>", $content);
97+
$this->assertStringContainsString($this->normalizeMultilineString($expected), $content);
98+
}
99+
61100
/**
62101
* @test
63102
* @dataProvider viewScenarioProvider

0 commit comments

Comments
 (0)