-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanonymous_subscriptions.module
More file actions
351 lines (309 loc) · 12.9 KB
/
anonymous_subscriptions.module
File metadata and controls
351 lines (309 loc) · 12.9 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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* @file
* The Anonymous Subscriptions module.
*
* Module allows a non-registered user to your site
* the ability to register for email notifications to certain pre-defined
* content types. These notifications are optional on the pre-defined
* content types with a flag displayed as to whether to send e-mails or not.
*/
use Drupal\Core\Url;
use Drupal\anonymous_subscriptions\Form\SettingsForm;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
/**
* Implements hook_help().
*/
function anonymous_subscriptions_help($route_name) {
switch ($route_name) {
case 'help.page.anonymous_subscriptions':
$output = '<p>';
$output .= '<p>' . t('Anonymous Subscriptions allows anonymous visitors to your site to subscribe to updates to nodes. The <a href="@anon">anonymous subscriptions administration page</a> allows you to configure options including whether emails need to be verified and which content types the option should appear to send notifications for.', [
'@anon' => Url::fromRoute('anonymous_subscriptions.settings_form'),
]);
$output .= '</p>';
return $output;
}
}
/**
* Implements hook_theme().
*/
function anonymous_subscriptions_theme() {
return [
'anonymous_subscriptions_message' => [
'variables' => [
'title' => FALSE,
'message' => FALSE,
'link' => FALSE,
'subscription' => FALSE,
],
'template' => 'anonymous-subscriptions-message',
],
'anonymous_subscriptions_notification_email' => [
'variables' => [
'body' => FALSE,
'subscription_reason_text' => FALSE,
'unsubscribe_url' => FALSE,
'unsubscribe_all_url' => FALSE
],
'template' => 'anonymous-subscriptions-notification-email',
],
];
}
/**
* Implements hook_mail().
*/
function anonymous_subscriptions_mail($key, &$message, $params) {
switch ($key) {
case 'anonymous_subscriptions_key':
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
if (!isset($params['from'])) {
$params['from'] = \Drupal::config('system.site')->get('mail');
}
if (!isset($params['sender'])) {
$params['sender'] = \Drupal::config('system.site')->get('name');
}
$params['sender'] = '=?UTF-8?B?' . base64_encode($params['sender']) . '?=';
$message['headers'] = array_merge($message['headers'], [
'Content-Type' => 'text/html; charset=UTF-8;',
'Content-Transfer-Encoding' => '8Bit',
'MIME-Version' => '1.0',
'From' => $params['from'],
'Sender' => $params['from'],
]);
if (isset($params['Cc'])) {
$message['headers']['Cc'] = $params['Cc'];
}
if (isset($params['Bcc'])) {
$message['headers']['Bcc'] = $params['Bcc'];
}
break;
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for node_form().
*
* This is used to display the checkbox as to whether or not send the
* notification for this particular node.
*/
function anonymous_subscriptions_form_node_form_alter(&$form, $form_state, $form_id) {
$form_object = $form_state->getFormObject();
/** @var \Drupal\node\NodeInterface $node */
$node = $form_object->getEntity();
$config = \Drupal::config(SettingsForm::$configName);
$valid_types = $config->get('anonymous_subscriptions_node_types') ?: [];
if (!empty($valid_types[$node->getType()])) {
/** @var \Drupal\Core\Session\AccountInterface $current_user */
$current_user = \Drupal::service('current_user');
$form['options']['send_emails'] = [
'#type' => 'checkbox',
'#title' => t('Send e-mails to subscribers'),
'#default_value' => $config->get('anonymous_subscriptions_send_default'),
'#group' => 'options',
'#access' => $current_user->hasPermission('alter anonymous_subscriptions') || $current_user->hasPermission('administer anonymous_subscriptions'),
'#weight' => 10,
];
$form['options']['send_test_email'] = [
'#type' => 'checkbox',
'#title' => t('Send test e-mails'),
'#group' => 'options',
'#access' => $current_user->hasPermission('alter anonymous_subscriptions') || $current_user->hasPermission('administer anonymous_subscriptions'),
'#weight' => 11,
];
$form['options']['send_test_email_emails'] = [
'#type' => 'textfield',
'#title' => t('Recipients of the test email'),
'#group' => 'options',
'#access' => $current_user->hasPermission('alter anonymous_subscriptions') || $current_user->hasPermission('administer anonymous_subscriptions'),
'#weight' => 12,
'#states' => [
'visible' => [
':input[name="send_test_email"]' => ['checked' => TRUE],
],
],
];
array_unshift($form['actions']['submit']['#submit'], 'anonymous_subscriptions_form_node_form_alter_submit');
}
}
/**
* Custom form submit callback.
*/
function anonymous_subscriptions_form_node_form_alter_submit(&$form, &$form_state) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
$tempStore = \Drupal::service('tempstore.shared')->get('anonymous_subscriptions');
$id = $node->id() ?: 'new';
if ($form_state->getValue('send_test_email')) {
$test_emails = $form_state->getValue('send_test_email_emails');
$emails = explode(',', $test_emails);
if (!empty($emails)) {
$tempStore->set('send_test_mail_emails:' . $node->getType() . ':' . $id, $emails);
}
}
// Saving send email flag to temporary shared storage.
$tempStore->set('send_mail:' . $node->getType() . ':' . $id, $form_state->getValue('send_emails'));
}
/**
* Implements hook_node_insert().
*/
function anonymous_subscriptions_node_insert(\Drupal\node\NodeInterface $node) {
$config = \Drupal::config(SettingsForm::$configName);
$valid_types = $config->get('anonymous_subscriptions_node_types') ?: [];
if (!empty($valid_types[$node->getType()])) {
/** @var \Drupal\Core\TempStore\SharedTempStore $tempStore */
$tempStore = \Drupal::service('tempstore.shared')->get('anonymous_subscriptions');
// Getting the flag for the test email.
$testEmails = $tempStore->get('send_test_mail_emails:' . $node->getType() . ':new');
if ($testEmails) {
\Drupal::service('anonymous_subscriptions.default')->sendTestEmail($node, $testEmails);
// Deleting the flag.
$tempStore->delete('send_test_mail_emails:' . $node->getType() . ':new');
}
// Getting the flag for the new node notification.
$sendEmail = $tempStore->get('send_mail:' . $node->getType() . ':new');
// Flag is present - we are dealing with node added via form.
if (isset($sendEmail)) {
// Checking the flag is TRUE.
if ($sendEmail) {
$tempStore->set('send_mail:' . $node->getType() . ':' . $node->id(), 'send_mail:' . $node->getType() . ':new');
}
// Deleting the flag.
$tempStore->delete('send_mail:' . $node->getType() . ':new');
}
// Flag is missing - we are dealing with automatic creation, force email to be sent.
else {
$tempStore->set('send_mail:' . $node->getType() . ':' . $node->id(), 'send_mail:' . $node->getType() . ':new');
}
\Drupal::service('anonymous_subscriptions.default')->addPendingEmails($node);
}
}
/**
* Implements hook_node_update().
*/
function anonymous_subscriptions_node_update($node) {
/** @var \Drupal\Core\TempStore\SharedTempStore $tempStore */
$tempStore = \Drupal::service('tempstore.shared')->get('anonymous_subscriptions');
// Getting the flag for the test email.
$testEmails = $tempStore->get('send_test_mail_emails:' . $node->getType() . ':' . $node->id());
if ($testEmails) {
\Drupal::service('anonymous_subscriptions.default')->sendTestEmail($node, $testEmails);
// Deleting the flag.
$tempStore->delete('send_test_mail_emails:' . $node->getType() . ':' . $node->id());
}
\Drupal::service('anonymous_subscriptions.default')->addPendingEmails($node);
}
/**
* Implements hook_module_implements_alter().
*/
function anonymous_subscriptions_module_implements_alter(&$implementations, $hook) {
if ($hook == 'node_insert') {
// Move anonymous_subscriptions_node_insert() to the end of the list.
$group = $implementations['anonymous_subscriptions'];
unset($implementations['anonymous_subscriptions']);
$implementations['anonymous_subscriptions'] = $group;
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function anonymous_subscriptions_form_field_config_edit_form_alter(array &$form, FormStateInterface $form_state) {
/** @var \Drupal\field\FieldConfigInterface $field */
$field = $form_state->getFormObject()->getEntity();
if ($field->getTargetEntityTypeId() != 'node'
|| $field->getType() != 'entity_reference'
|| $field->getSetting('handler') != 'default:taxonomy_term') {
return;
}
$config = \Drupal::config(SettingsForm::$configName);
$valid_types = $config->get('anonymous_subscriptions_node_types') ?: [];
$form['anonymous_subscriptions'] = [
'#type' => 'checkbox',
'#title' => t('Enable anonymous subscription for this reference'),
'#default_value' => $field->getThirdPartySetting('anonymous_subscriptions', 'enabled', FALSE),
'#disabled' => empty($valid_types[$field->getTargetBundle()]),
'#description' => t('Be sure you have enabled content type for using anonymous subscriptions on @settings_link', [
'@settings_link' => Link::fromTextAndUrl(t('settings page'), Url::fromRoute('anonymous_subscriptions.settings_form', ['destination' => Drupal::service('path.current')->getPath()]))->toString(),
]),
'#weight' => -1,
];
$form['actions']['submit']['#submit'][] = 'anonymous_subscriptions_field_config_edit_form_submit';
}
/**
* Form submission handler for "field_config_edit_form" alter.
*
* @param array $form
* The form array.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The form state.
*/
function anonymous_subscriptions_field_config_edit_form_submit(array $form, FormStateInterface $form_state) {
$field = $form_state->getFormObject()->getEntity();
$form_fields = &$form_state->getValues();
// If the anonymous_subscription option is checked, update settings.
if ($form_fields['anonymous_subscriptions']) {
$field->setThirdPartySetting('anonymous_subscriptions', 'enabled', TRUE);
$field->save();
}
else {
$field->unsetThirdPartySetting('anonymous_subscriptions', 'enabled');
$field->save();
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter() for taxonomy_term_form().
*
* This is used to display the "Disable anonymous subscription" checkbox.
*/
function anonymous_subscriptions_form_taxonomy_term_form_alter(&$form, $form_state, $form_id) {
$form_object = $form_state->getFormObject();
/** @var Drupal\taxonomy\TermInterface $term */
$term = $form_object->getEntity();
/** @var Drupal\anonymous_subscriptions\DefaultService $service */
$service = \Drupal::service('anonymous_subscriptions.default');
if (!$service->isVocabularyAllowed($term->bundle()) || empty($term->id())) {
return;
}
$config = \Drupal::config(SettingsForm::$configName);
$anonymous_subscription_disabled_terms = $config->get('anonymous_subscription_disabled_terms') ?: [];
$form['anonymous_subscription'] = [
'#type' => 'details',
'#title' => t('Anonymous subscription'),
'#open' => in_array($term->id(), $anonymous_subscription_disabled_terms),
];
$form['anonymous_subscription']['anonymous_subscription_disabled_terms'] = [
'#type' => 'checkbox',
'#title' => t('Disable subscription for this taxonomy term'),
'#default_value' => in_array($term->id(), $anonymous_subscription_disabled_terms),
'#weight' => 10,
'#groups' => 'anonymous_subscription',
];
array_unshift($form['actions']['submit']['#submit'], 'anonymous_subscriptions_form_taxonomy_term_form_alter_submit');
}
/**
* Custom form submit callback for taxonomy_term_form.
*/
function anonymous_subscriptions_form_taxonomy_term_form_alter_submit(&$form, &$form_state) {
$form_fields = &$form_state->getValues();
/** @var Drupal\taxonomy\TermInterface $term */
$term = $form_state->getFormObject()->getEntity();
$config = \Drupal::configFactory()->getEditable(SettingsForm::$configName);
$anonymous_subscription_disabled_terms = $config->get('anonymous_subscription_disabled_terms') ?: [];
// Update disabled terms array.
if ($form_fields['anonymous_subscription_disabled_terms']) {
if (!in_array($term->id(), $anonymous_subscription_disabled_terms)) {
$anonymous_subscription_disabled_terms[] = $term->id();
$config->set('anonymous_subscription_disabled_terms', $anonymous_subscription_disabled_terms);
$config->save();
}
}
else {
if (in_array($term->id(), $anonymous_subscription_disabled_terms)) {
$key = array_search($term->id(), $anonymous_subscription_disabled_terms);
unset($anonymous_subscription_disabled_terms[$key]);
$config->set('anonymous_subscription_disabled_terms', $anonymous_subscription_disabled_terms);
$config->save();
}
}
}