Skip to content

Commit 8ba81db

Browse files
committed
Add structured data templates to terms
1 parent c2aa6c4 commit 8ba81db

File tree

14 files changed

+181
-37
lines changed

14 files changed

+181
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This Statamic addon provides a powerful and flexible way to add structured data
44

55
## Features
66

7-
- 🔄 Dynamic JSON-LD generation based on entry data
7+
- 🔄 Dynamic JSON-LD generation based on entry and term data
88
- 📝 Template-based structured data configuration
99
- 🔌 Automatic injection of structured data into your pages
1010
- 🎯 Support for multiple schemas per page
@@ -37,7 +37,7 @@ Create templates in your Statamic control panel that define your structured data
3737

3838
### 2. Assigning Templates to Entries
3939

40-
In your entry's content, you can assign one or more structured data templates using the `structured_data_templates` field. The addon will automatically process these templates and generate the appropriate JSON-LD scripts.
40+
In your entry or term's content, you can assign one or more structured data templates using the `structured_data_templates` field. The addon will automatically process these templates and generate the appropriate JSON-LD scripts.
4141

4242
### 3. Automatic Injection
4343

@@ -71,4 +71,4 @@ Here's an example of how you might structure a basic Organization schema:
7171

7272
## Configuration
7373

74-
The addon works out of the box with sensible defaults. Configuration can be customized through your entry blueprints and templates.
74+
The addon works out of the box with sensible defaults. Configuration can be customized through your entry and term blueprints and templates.

config/structured-data.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,8 @@
55
// Add collection handles here that should have structured data templates
66
// Example: 'pages', 'blog', 'products'
77
],
8+
'taxonomies' => [
9+
// Add taxonomy handles here that should have structured data objects
10+
// Example: 'categories', 'tags'
11+
],
812
];

resources/blueprints/collections/structured_data_templates/structured_data_templates.yaml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,21 @@ tabs:
3232
type: slug
3333
localizable: true
3434
validate: 'max:200'
35+
-
36+
handle: blueprint_type
37+
field:
38+
options:
39+
-
40+
key: collection
41+
value: Collection
42+
-
43+
key: taxonomy
44+
value: Taxonomy
45+
max_items: 1
46+
default: collection
47+
type: select
48+
display: 'Blueprint type'
49+
localizable: true
3550
-
3651
handle: use_for_collection
3752
field:
@@ -40,7 +55,22 @@ tabs:
4055
display: 'Use for collection'
4156
localizable: true
4257
validate:
58+
- sometimes
59+
- required
60+
if:
61+
blueprint_type: 'equals collection'
62+
-
63+
handle: use_for_taxonomy
64+
field:
65+
max_items: 1
66+
type: taxonomies
67+
display: 'Use for taxonomy'
68+
localizable: true
69+
validate:
70+
- sometimes
4371
- required
72+
if:
73+
blueprint_type: 'equals taxonomy'
4474
-
4575
handle: structured_data_available_variables
4676
field:

resources/dist/build/assets/statamic-structured-data-33d7377e.css renamed to resources/dist/build/assets/statamic-structured-data-05ad0e35.css

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/dist/build/assets/statamic-structured-data-08e9c173.js renamed to resources/dist/build/assets/statamic-structured-data-0787df7f.js

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/dist/build/manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
"src": "resources/css/statamic-structured-data.css"
66
},
77
"resources/js/statamic-structured-data.css": {
8-
"file": "assets/statamic-structured-data-33d7377e.css",
8+
"file": "assets/statamic-structured-data-05ad0e35.css",
99
"src": "resources/js/statamic-structured-data.css"
1010
},
1111
"resources/js/statamic-structured-data.js": {
1212
"css": [
13-
"assets/statamic-structured-data-33d7377e.css"
13+
"assets/statamic-structured-data-05ad0e35.css"
1414
],
15-
"file": "assets/statamic-structured-data-08e9c173.js",
15+
"file": "assets/statamic-structured-data-0787df7f.js",
1616
"isEntry": true,
1717
"src": "resources/js/statamic-structured-data.js"
1818
}

resources/js/components/fieldtypes/AvailableVariables.vue

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export default {
6868
variables() {
6969
return this.meta?.variables || {
7070
config: {},
71-
entry: []
71+
entry: [],
72+
term: []
7273
};
7374
},
7475

src/Fieldtypes/AvailableVariablesFieldtype.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function getAvailableVariables(): array
3636
],
3737
'globals' => $this->getGlobalVariables(),
3838
'entry' => $this->getEntryFields(),
39+
'term' => $this->getTermFields(),
3940
];
4041
}
4142

@@ -89,6 +90,38 @@ protected function getEntryFields(): array
8990
return array_merge($baseFields, $collectionFields);
9091
}
9192

93+
protected function getTermFields(): array
94+
{
95+
$dataTemplate = $this->field->parent();
96+
97+
if (! $dataTemplate?->use_for_taxonomy?->termBlueprints()?->first()) {
98+
return [];
99+
}
100+
101+
$fields = collect($dataTemplate?->use_for_taxonomy?->termBlueprints()?->reduce(function($carry, $blueprint) {
102+
return array_merge($carry, $blueprint->fields()->items()->toArray());
103+
}, []) ?? []);
104+
105+
if(class_exists(OnPageSeoBlueprint::class)) {
106+
$fields = $fields->merge(OnPageSeoBlueprint::requestBlueprint()->fields()?->items());
107+
}
108+
109+
if (!$fields) {
110+
return [];
111+
}
112+
113+
$baseFields = [['name' => 'absolute_url', 'description' => 'Full URL']];
114+
115+
$collectionFields = $fields->map(function ($field) {
116+
return $this->setFieldData($field);
117+
})
118+
->filter()
119+
->values()
120+
->all();
121+
122+
return array_merge($baseFields, $collectionFields);
123+
}
124+
92125
protected function getGlobalVariables(): array
93126
{
94127
$variables = collect();

src/Http/Controllers/StructuredDataController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Justbetter\StatamicStructuredData\Parser\StructuredDataParser;
77
use Statamic\Facades\Entry;
88
use Statamic\Facades\Site;
9+
use Statamic\Facades\Term;
910
use Statamic\Http\Controllers\CP\CpController;
1011

1112
class StructuredDataController extends CpController
@@ -36,6 +37,10 @@ public function getTemplates(Request $request)
3637
$templateIds = $request->input('ids', []);
3738
$contentEntry = Entry::find($request->input('entry_id'));
3839

40+
if (! $contentEntry) {
41+
$contentEntry = Term::find($request->input('entry_id'));
42+
}
43+
3944
if (! $contentEntry) {
4045
return response()->json(['error' => 'Content entry not found'], 404);
4146
}

src/Http/Middleware/InjectStructuredData.php

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
use Statamic\Facades\Site;
1212
use Statamic\Facades\URL;
1313
use Statamic\Structures\Page;
14+
use Statamic\Facades\Term;
15+
use Statamic\Taxonomies\LocalizedTerm;
1416

1517
class InjectStructuredData
1618
{
@@ -31,10 +33,21 @@ public function handle(Request $request, Closure $next)
3133

3234
$entry = $this->getCurrentEntry();
3335

34-
if (! $entry) {
35-
return $response;
36+
if ($entry) {
37+
return $this->handleEntry($response, $entry);
38+
}
39+
40+
$term = $this->getCurrentTerm();
41+
42+
if ($term) {
43+
return $this->handleTaxonomy($response, $term);
3644
}
3745

46+
return $response;
47+
}
48+
49+
protected function handleEntry($response, $entry)
50+
{
3851
if ($entry instanceof Page) {
3952
$entry = $entry->entry();
4053
}
@@ -45,7 +58,23 @@ public function handle(Request $request, Closure $next)
4558
return $response;
4659
}
4760

48-
$scripts = $this->structuredDataService->getJsonLdScripts($entry);
61+
return $this->handleScripts($response, $entry);
62+
}
63+
64+
protected function handleTaxonomy($response, $term)
65+
{
66+
$enabledTaxonomies = config('justbetter.structured-data.taxonomies', []);
67+
68+
if (! in_array($term?->taxonomy()?->handle(), $enabledTaxonomies)) {
69+
return $response;
70+
}
71+
72+
return $this->handleScripts($response, $term);
73+
}
74+
75+
protected function handleScripts($response, $item)
76+
{
77+
$scripts = $this->structuredDataService->getJsonLdScripts($item);
4978

5079
if (!$scripts ?? false) {
5180
return $response;
@@ -74,4 +103,10 @@ protected function getCurrentEntry(): Page|Entry|null
74103
$url = URL::getCurrent();
75104
return EntryFacade::findByUri($url, Site::current()->handle());
76105
}
106+
107+
protected function getCurrentTerm(): LocalizedTerm|null
108+
{
109+
$url = URL::getCurrent();
110+
return Term::findByUri($url, Site::current()->handle());
111+
}
77112
}

0 commit comments

Comments
 (0)