Skip to content

Commit c62462a

Browse files
committed
Add SEO values to pages
1 parent f8b13e7 commit c62462a

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

user/config/site.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
title: Drupal Commerce docs
1+
title: Drupal Commerce documentation
22
metadata:
33
description: 'Drupal Commerce documentation'
44

user/localhost/config/system.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ assets:
33
js_pipeline: false
44

55
twig:
6-
cache: true
6+
cache: false
77
debug: true
88

99
debugger:
10-
enabled: false
10+
enabled: true

user/themes/commerce/commerce.php

+51
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,60 @@
11
<?php
22
namespace Grav\Theme;
33

4+
use Grav\Common\Page\Page;
45
use Grav\Common\Theme;
56

67
class Commerce extends Theme
78
{
89

10+
public static function getSubscribedEvents()
11+
{
12+
return [
13+
'onThemeInitialized' => ['onThemeInitialized', 0]
14+
];
15+
}
16+
17+
public function onThemeInitialized()
18+
{
19+
if ($this->isAdmin()) {
20+
$this->active = false;
21+
return;
22+
}
23+
24+
$this->enable([
25+
'onPageInitialized' => ['onPageInitialized', 0]
26+
]);
27+
}
28+
29+
/**
30+
* Add content after page content was read into the system.
31+
*
32+
*/
33+
public function onPageInitialized()
34+
{
35+
/** @var \Grav\Common\Page\Page $page */
36+
$page = $this->grav['page'];
37+
$config = $this->mergeConfig($page);
38+
$metadata = $page->metadata();
39+
$summary = trim(strip_tags($page->summary(null, true)));
40+
41+
$metadata_values = [
42+
'og:sitename' => $config->get('site.title'),
43+
'og:title' => $page->title(),
44+
'og:description' => $summary,
45+
'twitter:card' => 'summary',
46+
'twitter:title' => $page->title(),
47+
'twitter:description' => $summary,
48+
'twitter:site' => '@drupalcommerce',
49+
'twitter:label1' => 'Filed under',
50+
'twitter:data1' => $page->parent()->title(),
51+
];
52+
foreach ($metadata_values as $property => $value) {
53+
$metadata[$property]['name'] = $property;
54+
$metadata[$property]['property'] = $property;
55+
$metadata[$property]['content'] = $value;
56+
}
57+
58+
$page->metadata($metadata);
59+
}
960
}

0 commit comments

Comments
 (0)