-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordproof.module
More file actions
163 lines (146 loc) · 5.37 KB
/
wordproof.module
File metadata and controls
163 lines (146 loc) · 5.37 KB
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
<?php
use Drupal\Core\Render\BubbleableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityPublishedInterface;
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\wordproof\Timestamp\TimestampInterface;
/**
* Stamp the configured entities on create.
*
* @see hook_entity_create()
*/
function wordproof_entity_insert(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface || $entity instanceof TimestampInterface) {
return FALSE;
}
if ($entity instanceof EntityPublishedInterface && $entity->isPublished() === FALSE) {
return FALSE;
}
$timestampBuilder = \Drupal::service('wordproof.timestamp_builder_service');
$isStamped = $timestampBuilder->stamp($entity);
$timestampBuilder->stampWatchedEntities($entity);
if ($isStamped) {
\Drupal::messenger()->addMessage(t('WordProof timestamp for ":title" is queued and the timestamp will appear after approval on the blockchain.', [':title' => $entity->label()]), 'status');
}
}
/**
* Stamp the configured entities on update.
*
* @see hook_entity_update()
*/
function wordproof_entity_update(EntityInterface $entity) {
if (!$entity instanceof ContentEntityInterface || $entity instanceof TimestampInterface) {
return;
}
if ($entity instanceof EntityPublishedInterface && $entity->isPublished() === FALSE) {
return;
}
$timestampBuilder = \Drupal::service('wordproof.timestamp_builder_service');
$isStamped = $timestampBuilder->stamp($entity);
$timestampBuilder->stampWatchedEntities($entity);
if ($isStamped) {
\Drupal::messenger()->addMessage(t('WordProof timestamp for ":title" is queued and the timestamp will appear after approval on the blockchain.', [':title' => $entity->label()]), 'status');
}
}
/**
* Add the wordproof certificate theme to display a certificate.
*
* @see hook_theme()
*/
function wordproof_theme($existing, $type, $theme, $path) {
return [
'wordproof_certificate' => [
'variables' => ['timestamp' => NULL, 'settings' => NULL],
],
];
}
/**
* Add the [entity].wordproof-timestamp token.
*
* @see hook_token_info_alter()
*/
function wordproof_token_info_alter(&$info) {
$definitions = \Drupal::entityTypeManager()->getDefinitions();
foreach ($definitions as $entity_type_id => $entity_type) {
if (!$entity_type->entityClassImplements(ContentEntityInterface::class) || $entity_type_id === 'timestamp') {
continue;
}
// Make sure a token type exists for this entity.
$token_type = \Drupal::service('token.entity_mapper')->getTokenTypeForEntityType($entity_type_id);
if (empty($token_type) || !isset($info['types'][$token_type])) {
continue;
}
$info['types'][$token_type]['wordproof-timestamp'] = [
'name' => t('WordProof timestamp'),
'description' => t('Token for the JSON-LD timestamp of an entity.'),
'needs-data' => [
$entity_type_id,
'timestamp',
],
];
$info['tokens'][$token_type]['wordproof-timestamp'] = [
'name' => t("Wordproof JSON-LD timestamp"),
'description' => t("The node's JSON-LD timestamp"),
'module' => 'token',
];
}
}
/**
* Add the [entity].wordproof-timestamp token.
*
* @see hook_tokens()
*/
function wordproof_tokens($type, $tokens, array $data, array $options, BubbleableMetadata $bubbleable_metadata) {
$replacements = [];
if ($type == 'entity' && !empty($data['entity'])) {
foreach ($tokens as $name => $original) {
switch ($name) {
case 'wordproof-timestamp':
/** @var \Drupal\wordproof\Timestamp\TimestampInterface $timestamp */
$timestampRepository = \Drupal::service('wordproof.repository');
$timestamp = $timestampRepository->get($data['entity']);
if ($timestamp) {
$bubbleable_metadata->addCacheableDependency($timestamp);
$toJsonLdArray = $timestamp->toJsonLdArray();
$enable_revisions = \Drupal::config('wordproof.settings')->get('enable_revisions');
if ($enable_revisions) {
$hashInputRevisions = $timestampRepository->getHashInputRevisions($timestamp);
if (count($hashInputRevisions) > 0) {
$toJsonLdArray['revisions'] = $hashInputRevisions;
}
}
if (count($toJsonLdArray) > 0) {
$replacements[$original] = json_encode($toJsonLdArray, JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_UNESCAPED_UNICODE);
}
}
break;
}
}
}
return $replacements;
}
/**
* Add the help page for module.
*
* @see hook_help()
*/
function wordproof_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.MYMODULE':
$text = file_get_contents(__DIR__ . '/README.md');
if (!\Drupal::moduleHandler()->moduleExists('markdown')) {
return '<pre>' . Html::escape($text) . '</pre>';
}
else {
// Use the Markdown filter to render the README.
$filter_manager = \Drupal::service('plugin.manager.filter');
$settings = \Drupal::configFactory()->get('markdown.settings')->getRawData();
$config = ['settings' => $settings];
$filter = $filter_manager->createInstance('markdown', $config);
return $filter->process($text, 'en');
}
}
return NULL;
}