Skip to content

Commit a1529ae

Browse files
Fall back to section/site default when "From Field" value is empty (#456)
1 parent 0e916ec commit a1529ae

File tree

11 files changed

+89
-28
lines changed

11 files changed

+89
-28
lines changed

src/Cascade.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
class Cascade
2525
{
2626
protected $data;
27+
protected $siteDefaults;
28+
protected $sectionDefaults;
2729
protected $current;
2830
protected $explicitUrl;
2931
protected $model;
@@ -50,6 +52,24 @@ public function with($array)
5052
return $this;
5153
}
5254

55+
public function withSiteDefaults($data)
56+
{
57+
$this->with($data);
58+
59+
$this->siteDefaults = $data;
60+
61+
return $this;
62+
}
63+
64+
public function withSectionDefaults($data)
65+
{
66+
$this->with($data);
67+
68+
$this->sectionDefaults = $data;
69+
70+
return $this;
71+
}
72+
5373
public function withCurrent($data)
5474
{
5575
if (is_null($data)) {
@@ -210,6 +230,8 @@ protected function nextUrl()
210230

211231
protected function parse($key, $item)
212232
{
233+
$original = $item;
234+
213235
if (is_array($item)) {
214236
return array_map(function ($item) use ($key) {
215237
return $this->parse($key, $item);
@@ -243,6 +265,17 @@ protected function parse($key, $item)
243265
$item = $item->value();
244266
}
245267
}
268+
269+
// When the field is empty, attempt to fall back to the section or site defaults.
270+
if (! $item) {
271+
if (isset($this->sectionDefaults[$key]) && $this->sectionDefaults[$key] !== $original) {
272+
return $this->parse($key, $this->sectionDefaults[$key]);
273+
}
274+
275+
if (isset($this->siteDefaults[$key]) && $this->siteDefaults[$key] !== $original) {
276+
return $this->parse($key, $this->siteDefaults[$key]);
277+
}
278+
}
246279
}
247280

248281
// If we have a method here to perform additional parsing, do that now.

src/Fields.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,11 +326,11 @@ protected function getPlaceholder($handle)
326326
}
327327

328328
$cascade = Blink::once('seo-pro::placeholder.cascade', function () {
329-
$cascade = (new Cascade)->with(SiteDefaults::load()->all());
329+
$cascade = (new Cascade)->withSiteDefaults(SiteDefaults::load()->all());
330330

331331
if ($this->data) {
332332
$cascade = $cascade
333-
->with($this->getSectionDefaults($this->data))
333+
->withSectionDefaults($this->getSectionDefaults($this->data))
334334
->with($this->data->value('seo', []))
335335
->withCurrent($this->data);
336336
}

src/Fieldtypes/SeoProFieldtype.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ public function augment($data)
100100
$content = $this->field()->parent();
101101

102102
return (new Cascade)
103-
->with(SiteDefaults::load()->augmented())
104-
->with($this->getAugmentedSectionDefaults($content))
103+
->withSiteDefaults(SiteDefaults::load()->augmented())
104+
->withSectionDefaults($this->getAugmentedSectionDefaults($content))
105105
->with($augmented)
106106
->withCurrent($content)
107107
->get();

src/Http/Controllers/HumansController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public function show()
1313
abort_unless(config('statamic.seo-pro.humans.enabled'), 404);
1414

1515
$cascade = (new Cascade)
16-
->with(SiteDefaults::load()->all())
16+
->withSiteDefaults(SiteDefaults::load()->all())
1717
->get();
1818

1919
$contents = view('seo-pro::humans', $cascade);

src/Reporting/Chunk.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,8 @@ protected function createPage($content)
8080
}
8181

8282
$data = (new Cascade)
83-
->with(SiteDefaults::load()->augmented())
84-
->with($this->getAugmentedSectionDefaults($content))
83+
->withSiteDefaults(SiteDefaults::load()->augmented())
84+
->withSectionDefaults($this->getAugmentedSectionDefaults($content))
8585
->with($content->augmentedValue('seo')->value())
8686
->withCurrent($content)
8787
->get();

src/Reporting/Report.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ public function isGenerated()
461461
public function defaults()
462462
{
463463
return collect((new Cascade)
464-
->with(SiteDefaults::load()->all())
464+
->withSiteDefaults(SiteDefaults::load()->all())
465465
->get());
466466
}
467467

src/ServiceProvider.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ protected function bootAddonGraphQL()
150150
'type' => GraphQL::type('SeoPro'),
151151
'resolve' => function ($item) {
152152
return (new Cascade)
153-
->with(SiteDefaults::load()->augmented())
154-
->with($this->getAugmentedSectionDefaults($item))
153+
->withSiteDefaults(SiteDefaults::load()->augmented())
154+
->withSectionDefaults($this->getAugmentedSectionDefaults($item))
155155
->with($item->seo)
156156
->withCurrent($item)
157157
->get();

src/Sitemap/Sitemap.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ protected function getPages($items)
120120

121121
$data = (new Cascade)
122122
->forSitemap()
123-
->with($this->getSiteDefaults())
124-
->with($this->getSectionDefaults($content))
123+
->withSiteDefaults($this->getSiteDefaults())
124+
->withSectionDefaults($this->getSectionDefaults($content))
125125
->with($cascade ?: [])
126126
->withCurrent($content)
127127
->get();

src/Tags/SeoProTags.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ public function metaData()
4040
$current = optional($this->context->get('seo'))->augmentable();
4141

4242
$metaData = (new Cascade)
43-
->with(SiteDefaults::load()->augmented())
44-
->with($this->getAugmentedSectionDefaults($current))
43+
->withSiteDefaults(SiteDefaults::load()->augmented())
44+
->withSectionDefaults($this->getAugmentedSectionDefaults($current))
4545
->with($this->context->value('seo'))
4646
->with($current ? [] : $this->context->except('template_content'))
4747
->withCurrent($current)

tests/CascadeTest.php

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

55
use PHPUnit\Framework\Attributes\DataProvider;
66
use PHPUnit\Framework\Attributes\Test;
7+
use Statamic\Facades\Blueprint;
78
use Statamic\Facades\Config;
89
use Statamic\Facades\Entry;
910
use Statamic\Facades\Site;
1011
use Statamic\SeoPro\Cascade;
12+
use Statamic\SeoPro\Fields;
1113
use Statamic\SeoPro\SiteDefaults;
1214

1315
class CascadeTest extends TestCase
@@ -25,7 +27,7 @@ protected function tearDown(): void
2527
public function it_generates_seo_cascade_from_site_defaults_and_home_entry()
2628
{
2729
$data = (new Cascade)
28-
->with(SiteDefaults::load()->all())
30+
->withSiteDefaults(SiteDefaults::load()->all())
2931
->get();
3032

3133
$expected = [
@@ -58,7 +60,7 @@ public function it_generates_seo_cascade_from_site_defaults_and_home_entry()
5860
public function it_overwrites_data_in_cascade()
5961
{
6062
$data = (new Cascade)
61-
->with(SiteDefaults::load()->all())
63+
->withSiteDefaults(SiteDefaults::load()->all())
6264
->with([
6365
'site_name' => 'Cool Writings',
6466
'description' => 'Bob sled team',
@@ -97,7 +99,7 @@ public function it_overwrites_data_in_cascade()
9799
public function it_generates_compiled_title_from_cascaded_parts()
98100
{
99101
$data = (new Cascade)
100-
->with(SiteDefaults::load()->all())
102+
->withSiteDefaults(SiteDefaults::load()->all())
101103
->with([
102104
'site_name' => 'Cool Writings',
103105
'site_name_position' => 'after',
@@ -112,6 +114,32 @@ public function it_generates_compiled_title_from_cascaded_parts()
112114
$this->assertEquals('Cool Writings >>> Jamaica', $data['compiled_title']);
113115
}
114116

117+
#[Test]
118+
public function it_falls_back_to_site_default_when_from_field_value_is_empty()
119+
{
120+
$entry = Entry::findByUri('/about')->entry();
121+
122+
$sectionDefaults = Blueprint::make()
123+
->setContents([
124+
'fields' => Fields::new()->getConfig(),
125+
])
126+
->fields()
127+
->addValues([
128+
'image' => '@seo:cover',
129+
])
130+
->augment()
131+
->values()
132+
->only(['image']);
133+
134+
$data = (new Cascade)
135+
->withSiteDefaults(SiteDefaults::load(['image' => 'seo/default.jpg'])->all())
136+
->withSectionDefaults($sectionDefaults)
137+
->withCurrent($entry)
138+
->get();
139+
140+
$this->assertEquals('seo/default.jpg', $data['image']);
141+
}
142+
115143
#[Test]
116144
public function it_parses_antlers()
117145
{
@@ -120,7 +148,7 @@ public function it_parses_antlers()
120148
$entry->data(['favourite_colour' => 'Red'])->save();
121149

122150
$data = (new Cascade)
123-
->with(SiteDefaults::load()->all())
151+
->withSiteDefaults(SiteDefaults::load()->all())
124152
->with([
125153
'description' => '{{ favourite_colour | upper }}',
126154
])
@@ -159,7 +187,7 @@ public function it_doesnt_parse_php_in_antlers($antlers, $output)
159187
$entry = Entry::findByUri('/about')->entry();
160188

161189
$data = (new Cascade)
162-
->with(SiteDefaults::load()->all())
190+
->withSiteDefaults(SiteDefaults::load()->all())
163191
->with([
164192
'description' => $antlers,
165193
])
@@ -178,7 +206,7 @@ public function it_parses_field_references()
178206
$entry->data(['favourite_colour' => 'Red'])->save();
179207

180208
$data = (new Cascade)
181-
->with(SiteDefaults::load()->all())
209+
->withSiteDefaults(SiteDefaults::load()->all())
182210
->with([
183211
'description' => '@seo:favourite_colour',
184212
])
@@ -194,7 +222,7 @@ public function it_generates_seo_cascade_without_exception_when_no_home_entry_ex
194222
Entry::findByUri('/')->delete();
195223

196224
$data = (new Cascade)
197-
->with(SiteDefaults::load()->all())
225+
->withSiteDefaults(SiteDefaults::load()->all())
198226
->get();
199227

200228
$expected = [
@@ -225,7 +253,7 @@ public function it_generates_seo_cascade_without_exception_when_no_home_entry_ex
225253
public function it_generates_404_title_with_404_in_response_code_in_context()
226254
{
227255
$data = (new Cascade)
228-
->with(SiteDefaults::load()->all())
256+
->withSiteDefaults(SiteDefaults::load()->all())
229257
->with([
230258
'response_code' => 404,
231259
])
@@ -253,7 +281,7 @@ public function it_generates_seo_cascade_from_custom_site_defaults_path()
253281
Config::set('statamic.seo-pro.site_defaults.path', base_path('custom_seo.yaml'));
254282

255283
$data = (new Cascade)
256-
->with(SiteDefaults::load()->all())
284+
->withSiteDefaults(SiteDefaults::load()->all())
257285
->get();
258286

259287
$expected = [
@@ -284,7 +312,7 @@ public function it_generates_seo_cascade_from_custom_site_defaults_path()
284312
public function it_overwrites_og_title()
285313
{
286314
$data = (new Cascade)
287-
->with(SiteDefaults::load()->all())
315+
->withSiteDefaults(SiteDefaults::load()->all())
288316
->with([
289317
'site_name' => 'Cool Writings',
290318
'description' => 'Bob sled team',
@@ -323,7 +351,7 @@ public function it_overwrites_og_title()
323351
public function it_overwrites_twitter_title_and_description()
324352
{
325353
$data = (new Cascade)
326-
->with(SiteDefaults::load()->all())
354+
->withSiteDefaults(SiteDefaults::load()->all())
327355
->with([
328356
'site_name' => 'Cool Writings',
329357
'description' => 'Bob sled team',

0 commit comments

Comments
 (0)