-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisqus.module
291 lines (272 loc) · 10.7 KB
/
disqus.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
/**
* @file
* The Disqus Drupal module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\field\FieldStorageConfigInterface;
/**
* API No action on entity delete.
*/
define('DISQUS_API_NO_ACTION', 0);
/**
* API Close on entity delete.
*/
define('DISQUS_API_CLOSE', 1);
/**
* API Remove on entity delete.
*/
define('DISQUS_API_REMOVE', 2);
/**
* Implements hook_help().
*/
function disqus_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.disqus':
$output = '<p>' . t('Uses the <a href=":disqus">Disqus</a> comment system to enhance comments.', array(
':disqus' => URL::fromUri('http://disqus.com/')->toString(),
)) . '</p>';
$output .= '<h3>' . t('Installation') . '</h3>';
$output .= '<ol><li>' . t('Register your site information at <a href=":disqus">Disqus</a>.', array(':disqus' => URL::fromUri('http://disqus.com/')->toString())) . '</li>';
$output .= '<li>' . t('In the <a href=":configuration">Disqus configuration</a>, set the domain to what you registered with Disqus.',
array(':configuration' => Url::fromRoute('disqus.settings')->toString())) . '</li>';
$output .= '<li>' . t('Disqus comments can be enabled for any <a href=":entity-help">entity sub-type</a> (for example, a <a href=":content-type">content type</a>). On the Manage fields page for each entity sub-type, you can enable disqus by adding a Disqus comments field.', array(
':entity-help' => Url::fromRoute('help.page', array('name' => 'entity'))->toString(),
':content-type' => Url::fromRoute('entity.node_type.collection')->toString(),
)) . '</li>';
$output .= '<li>' . t('Alternatively disqus comments can be used on <a href=":blocks">Blocks</a>. You will first need to configure the disqus comment field for any entity sub-type.',
array(':blocks' => Url::fromRoute('block.admin_display')->toString())) . '</li>';
$output .= '<li>' . t('Visit the <a href=":permissions">permissions</a>,'
. ' and set which users you would like to have the ability to view ' .
'Disqus threads (recommended for role).',
array(
':permissions' => Url::fromRoute('user.admin_permissions',
array(),
array(
'fragment' => 'module-disqus',
))->toString(),
)) . '</li></ol>';
return $output;
case 'disqus.settings':
return '<p>' . t('The following provides the general configuration options for the <a href=":disqus">Disqus</a> comment web service.',
array(':disqus' => URL::fromUri('http://disqus.com')->toString())) . '</p>';
}
}
/**
* Implements hook_node_links_alter().
*/
function disqus_node_links_alter(array &$node_links, NodeInterface $node, array &$context) {
$fields = \Drupal::service('disqus.manager')->getFields('node');
foreach ($fields as $field_name => $detail) {
// Skip fields that the node does not have.
if (!$node->hasField($field_name)) {
continue;
}
$links = array();
if ($node->get($field_name)->status) {
if (\Drupal::currentUser()->hasPermission('view disqus comments')) {
if ($context['view_mode'] === 'teaser') {
// Display the Disqus link.
$links['disqus_comments_num'] = array(
'title' => t('Comments'),
'url' => $node->urlInfo(),
'fragment' => 'disqus_thread',
'attributes' => array(
// Identify the node for Disqus with the unique identifier:
// http://docs.disqus.com/developers/universal/#comment-count
'data-disqus-identifier' => 'node/' . $node->id(),
),
);
}
$node_links['disqus'] = array(
'#theme' => 'links',
'#links' => $links,
'#attributes' => array(
'class' => array('links', 'inline'),
),
);
// Attach disqus library to load the Disqus comment count JavaScript.
$node_links['#attached']['library'][] = 'disqus/disqus';
$node_links['disqus']['#attached']['drupalSettings']['disqusComments'] = \Drupal::config('disqus.settings')
->get('disqus_domain');
}
}
}
}
/**
* Implements hook_entity_delete().
*/
function disqus_entity_delete(EntityInterface $entity) {
// Only act on content entities.
if (!($entity instanceof ContentEntityInterface)) {
return;
}
$field = \Drupal::service('disqus.manager')
->getFields($entity->getEntityTypeId());
if (!$entity->hasField(key($field))) {
return;
}
$config = \Drupal::config('disqus.settings');
// Close/remove the thread on disqus if required.
$action = $config->get('advanced.api.disqus_api_delete');
if ($action != DISQUS_API_NO_ACTION) {
$disqus = disqus_api();
if ($disqus) {
try {
// Load the thread data from disqus. Passing thread is required to allow
// the thread:ident call to work correctly. There is a pull request to
// fix this issue.
$thread = $disqus->threads->details(array(
'forum' => $config->get('disqus_domain'),
'thread:ident' => "{$entity->getEntityTypeId()}/{$entity->id()}",
'thread' => '1',
));
}
catch (Exception $exception) {
drupal_set_message(t('There was an error loading the thread details from Disqus.'), 'error');
\Drupal::logger('disqus')
->error('Error loading thread details for entity : @identifier. Check your API keys.', array('@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}"));
}
if (isset($thread->id)) {
if ($action == DISQUS_API_CLOSE) {
try {
$disqus->threads->close(array(
'access_token' => $config->get('advanced.disqus_useraccesstoken'),
'thread' => $thread->id,
'forum' => $config->get('disqus_domain'),
));
}
catch (Exception $exception) {
drupal_set_message(t('There was an error closing the thread on Disqus.'), 'error');
\Drupal::logger('disqus')
->error('Error closing thread for entity : @identifier. Check your user access token.', array('@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}"));
}
}
if ($action == DISQUS_API_REMOVE) {
try {
$disqus->threads->remove(array(
'access_token' => $config->get('advanced.disqus_useraccesstoken'),
'thread' => $thread->id,
'forum' => $config->get('disqus_domain'),
));
}
catch (Exception $exception) {
drupal_set_message(t('There was an error removing the thread on Disqus.'), 'error');
\Drupal::logger('disqus')
->error('Error closing thread for entity : @identifier. Check your user access token.', array('@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}"));
}
}
}
}
}
}
/**
* Implements hook_entity_update().
*/
function disqus_entity_update(EntityInterface $entity) {
// Only act on content entities.
if (!($entity instanceof ContentEntityInterface)) {
return;
}
$field = \Drupal::service('disqus.manager')
->getFields($entity->getEntityTypeId());
if (!$entity->hasField(key($field))) {
return;
}
$config = \Drupal::config('disqus.settings');
// Update the thread information on disqus if required.
if ($config->get('advanced.api.disqus_api_update') && ($entity->label() != $entity->original->label() || $entity->url() != $entity->original->url())) {
$disqus = disqus_api();
if ($disqus) {
try {
// Load the thread data from disqus. Passing thread is required to allow
// the thread:ident call to work correctly. There is a pull request to
// fix this issue.
$thread = $disqus->threads->details(array(
'forum' => $config->get('disqus_domain'),
'thread:ident' => "{$entity->getEntityTypeId()}/{$entity->id()}",
'thread' => '1',
));
}
catch (Exception $exception) {
drupal_set_message(t('There was an error loading the thread details from Disqus.'), 'error');
\Drupal::logger('disqus')
->error('Error loading thread details for entity : @identifier. Check your API keys.', array('@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}"));
}
if (isset($thread->id)) {
try {
$disqus->threads->update(array(
'access_token' => $config->get('advanced.disqus_useraccesstoken'),
'thread' => $thread->id,
'forum' => $config->get('disqus_domain'),
'title' => $entity->label(),
'url' => $entity->url('canonical', array('absolute' => TRUE)),
));
}
catch (Exception $exception) {
drupal_set_message(t('There was an error updating the thread details on Disqus.'), 'error');
\Drupal::logger('disqus')
->error('Error updating thread details for entity : @identifier. Check your user access token.', array('@identifier' => "{$entity->getEntityTypeId()}/{$entity->id()}"));
}
}
}
}
}
/**
* Implements hook_field_views_data().
*/
function disqus_field_views_data(FieldStorageConfigInterface $field_storage) {
$data = views_field_default_views_data($field_storage);
foreach ($data as $table_name => $table_data) {
$data[$table_name]['entity_id']['field'] = array(
'title' => t('Disqus Comment Count'),
'group' => t('Content'),
'help' => t('The number of Disqus comments made on the post. Note that this will not work in the preview.'),
'id' => 'disqus_comment_count',
);
}
return $data;
}
/**
* Implements hook_theme().
*/
function disqus_theme() {
$domain = \Drupal::config('disqus.settings')->get('disqus_domain');
return [
'disqus_noscript' => [
'variables' => [
'message' => t('View the discussion thread.'),
'url' => "http://$domain.disqus.com/",
],
],
];
}
/**
* Creates an instance of the Disqus PHP API.
*
* @return object
* The instance of the Disqus API.
*/
function disqus_api() {
try {
$disqus = new DisqusAPI(\Drupal::config('disqus.settings')
->get('advanced.disqus_secretkey'));
}
catch (Exception $exception) {
drupal_set_message(t('There was an error loading the Disqus PHP API. Please check your API keys and try again.'), 'error');
\Drupal::logger('disqus')
->error('Error loading the Disqus PHP API. Check your API keys.', array());
return FALSE;
}
return $disqus;
}
/**
* Implements hook_views_api().
*/
function disqus_views_api() {
return array('api' => 3);
}