This repository has been archived by the owner on Oct 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparsely_tag.module
228 lines (208 loc) · 6.66 KB
/
parsely_tag.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
/**
* @file
* Parse.ly Tag module file.
*/
use Drupal\Component\Utility\Html;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Datetime\DrupalDateTime;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\Entity\NodeType;
/**
* Implements hook_ENTITY_TYPE_view().
*/
function parsely_tag_node_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($view_mode == 'full') {
$config = \Drupal::config('parsely_tag.settings');
$bundle = NodeType::load($entity->bundle());
$settings = $bundle->getThirdPartySettings('parsely_tag');
if (!isset($settings['enable'])) {
$settings['enable'] = $config->get('default_enable');
}
if ($settings['enable']) {
if (empty($build['#attached'])) {
$build['#attached'] = [];
}
// Add the Parse.ly Javascript file.
if (!isset($build['#attached']['library'])) {
$build['#attached']['library'] = [];
}
$build['#attached']['library'][] = 'parsely_tag/tag';
// Add JSONLD properties tag to <head>.
foreach (_parsely_tag_properties() as $property => $info) {
if (!isset($settings[$property])) {
$settings[$property] = $config->get('default_' . $property);
}
}
$json = parsely_tag_generate_json($settings, $entity);
$tag = [
'#type' => 'html_tag',
'#tag' => 'script',
'#attributes' => [
'type' => 'application/ld+json',
],
'#value' => $json,
];
if (!isset($build['#attached']['html_head'])) {
$build['#attached']['html_head'] = [];
}
$build['#attached']['html_head'][] = [$tag, 'parsely_tag_tag'];
}
}
}
/**
* Generate final property values and json for Parse.ly's JSONLD tag.
*
* @param array $settings
* Parse.ly Tags settings for the node type.
* @param \Drupal\Core\Entity\EntityInterface $entity
* The node that the properties are being generated for.
*
* @return string
* The final properties for JSONLD as a json-encoded string.
*
* @see parsely_tag_node_view()
*/
function parsely_tag_generate_json(array $settings, EntityInterface $entity) {
$properties = ['@context' => 'http://schema.org'];
foreach (_parsely_tag_properties() as $property => $info) {
// These properties need to be tokenized individually to allow for final
// formatting of specific properties.
$value = \Drupal::token()->replace(
$settings[$property],
['node' => $entity],
['clear' => TRUE]
);
$value = strip_tags($value);
// These values will often have html entity encoding. Json::encode() will do
// it's own encoding so any HTML entities are decoded before processing.
$value = Html::decodeEntities($value);
switch ($property) {
case 'article_section':
case 'headline':
case 'thumbnail_url':
case 'type':
case 'url':
$properties[$info['json_property']] = $value;
break;
case 'date_created':
// Attempt to format any type of date.
if (!is_numeric($value)) {
$value = strtotime($value);
}
if (!empty($value)) {
$date = new DrupalDateTime('@' . $value);
$utc = new DateTimeZone('UTC');
$date->setTimezone($utc);
$value = $date->format('Y-m-d\TH:i:s\Z');
$properties[$info['json_property']] = $value;
}
break;
case 'creator':
case 'keywords':
// Explode comma-separated lists so Json::encode() turns them in to
// lists.
if (strpos($value, ',')) {
$properties[$info['json_property']] = explode(', ', $value);
}
else {
$properties[$info['json_property']] = $value;
}
break;
}
}
return Json::encode($properties);
}
/**
* Implements hook_page_bottom().
*/
function parsely_tag_page_bottom(array &$page_bottom) {
if (!\Drupal::service('router.admin_context')->isAdminRoute()) {
/* @todo Only print this for node views with Parse.ly enabled. */
$config = \Drupal::config('parsely_tag.settings');
if ($site_id = $config->get('site_id')) {
$page_bottom['parsely_tag_root'] = [
'#type' => 'container',
'#attributes' => [
'id' => 'parsely-root',
'style' => 'display: none',
],
];
$page_bottom['parsely_tag_root']['config'] = [
'#type' => 'html_tag',
'#tag' => 'span',
'#attributes' => [
'id' => 'parsely-cfg',
'data-parsely-site' => $site_id,
],
];
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function parsely_tag_form_node_type_form_alter(array &$form, FormStateInterface $form_state) {
// Load the real code only when needed.
module_load_include('inc', 'parsely_tag', 'parsely_tag.admin');
_parsely_tag_form_node_type_form_alter($form, $form_state);
}
/**
* Configurable properties for Parse.ly's JSONLD tag.
*
* @return array
* All available configurable tag names.
*
* @url https://www.parse.ly/help/integration/jsonld/
*/
function _parsely_tag_properties() {
return [
'type' => [
'title' => t('Type'),
'description' => t('The specific schema being used. For post pages
that Parse.ly should track, this must be <code>NewsArticle</code>.'),
'json_property' => '@type',
],
'headline' => [
'title' => t('Headline'),
'description' => t('Post or page title.'),
'json_property' => 'headline',
],
'url' => [
'title' => t('URL'),
'description' => t('Canonical URL for post/page.'),
'json_property' => 'url',
],
'thumbnail_url' => [
'title' => t('Thumbnail URL'),
'description' => t('URL of the image associated with the
post/page.'),
'json_property' => 'thumbnailUrl',
],
'date_created' => [
'title' => t('Date Created'),
'description' => t('Publication date. Will be converted to ISO 8601
per Parse.ly documentation.'),
'json_property' => 'dateCreated',
],
'article_section' => [
'title' => t('Section'),
'description' => t('Primary section/category of the post/page.'),
'json_property' => 'articleSection',
],
'creator' => [
'title' => t('Creator(s)'),
'description' => t('Author(s) of the post. For multiple authors, a
comma-separated list should be provided.'),
'json_property' => 'creator',
],
'keywords' => [
'title' => t('Keywords'),
'description' => t('A comma-separated list of arbitrary data to
attach to post.'),
'json_property' => 'keywords',
],
];
}