Skip to content

Commit 45ee65b

Browse files
Support custom JSON-LD schema in site defaults (#540)
1 parent b1001a2 commit 45ee65b

4 files changed

Lines changed: 53 additions & 1 deletion

File tree

lang/en/fieldsets/defaults.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
'json_ld_person_name' => 'Person Name',
3737
'json_ld_person_name_instruct' => 'The full name of the person this site represents.',
3838

39+
'json_ld_custom_section' => 'Custom Schema',
40+
'json_ld_custom_section_instruct' => 'Add custom JSON-LD schema that will be included on every page across your site.',
41+
'json_ld_schema' => 'Schema',
42+
'json_ld_schema_instruct' => 'Paste your custom schema objects here (`LocalBusiness`, `SoftwareApplication`, etc). You can use Antlers to output data from the item. Will be wrapped in the appropriate script tag.',
43+
3944
'json_ld_breadcrumbs_section' => 'Breadcrumbs',
4045
'json_ld_breadcrumbs' => 'Breadcrumbs',
4146
'json_ld_breadcrumbs_instruct' => 'Enable breadcrumb structured data to show this page\'s location in your site hierarchy in search results. [Learn more](https://developers.google.com/search/docs/appearance/structured-data/breadcrumb)',

src/Fields.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public function getConfig()
166166
'type' => 'seo_pro_source',
167167
'from_field' => false,
168168
'disableable' => true,
169-
'inherit' => $this->isContent,
169+
'inherit' => true,
170170
'localizable' => true,
171171
'full_width_setting' => true,
172172
'field' => [

src/SiteDefaults/Blueprint.php

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

33
namespace Statamic\SeoPro\SiteDefaults;
44

5+
use Statamic\SeoPro\Fieldtypes\Rules\ValidJsonLd;
56
use Statamic\SeoPro\HasAssetField;
67

78
class Blueprint
@@ -147,6 +148,29 @@ public static function get(): \Statamic\Fields\Blueprint
147148
],
148149
],
149150
],
151+
[
152+
'display' => __('seo-pro::fieldsets/defaults.json_ld_custom_section'),
153+
'instructions' => __('seo-pro::fieldsets/defaults.json_ld_custom_section_instruct'),
154+
'fields' => [
155+
[
156+
'handle' => 'json_ld_schema',
157+
'field' => [
158+
'display' => __('seo-pro::fieldsets/defaults.json_ld_schema'),
159+
'instructions' => __('seo-pro::fieldsets/defaults.json_ld_schema_instruct'),
160+
'type' => 'code',
161+
'mode' => 'javascript',
162+
'mode_selectable' => false,
163+
'show_mode_label' => false,
164+
'localizable' => true,
165+
'full_width_setting' => true,
166+
'fullscreen' => false,
167+
'validate' => [
168+
new ValidJsonLd,
169+
],
170+
],
171+
],
172+
],
173+
],
150174
[
151175
'display' => __('seo-pro::fieldsets/defaults.json_ld_breadcrumbs_section'),
152176
'fields' => [

tests/CascadeTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -417,6 +417,29 @@ public function it_generates_json_ld_data()
417417
], $data['json_ld']->all());
418418
}
419419

420+
#[Test]
421+
public function it_generates_json_ld_data_with_custom_schema_from_site_defaults()
422+
{
423+
$siteDefaults = SiteDefaults::in('default')->set([
424+
'site_name' => 'Cool Writings',
425+
'json_ld_entity' => 'organization',
426+
'json_ld_organization_name' => 'Cool Runnings Ltd',
427+
'json_ld_schema' => '{"@context":"https://schema.org","@type":"LocalBusiness","name":"Cool Runnings Ltd"}',
428+
]);
429+
430+
$data = (new Cascade)
431+
->with($siteDefaults->all())
432+
->with([
433+
'title' => 'Home',
434+
])
435+
->get();
436+
437+
$this->assertEquals([
438+
'{"@context":"https://schema.org","@type":"Organization","name":"Cool Runnings Ltd","@id":"http://cool-runnings.com#organization","url":"http://cool-runnings.com"}',
439+
'{"@context":"https://schema.org","@type":"LocalBusiness","name":"Cool Runnings Ltd"}',
440+
], $data['json_ld']->all());
441+
}
442+
420443
#[Test]
421444
public function glide_url_is_returned_for_json_ld_organization_logo()
422445
{

0 commit comments

Comments
 (0)