-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblaetter_formatters.module
385 lines (359 loc) · 15 KB
/
blaetter_formatters.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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<?php
/**
* @file
* Contains blaetter_formatters.module.
*/
use \Drupal\Core\Entity\EntityInterface;
use \Drupal\Core\Entity\ContentEntityInterface;
use \Drupal\Core\Routing\RouteMatchInterface;
use \Drupal\field\Entity\FieldStorageConfig;
/**
* Implements hook_help()
*
* @param string $route_name The name of the route
* @param RouteMatchInterface $route_match The route match interface object
*
* @see hook_help()
* )
* @return string
*/
function blaetter_formatters_help(string $route_name, RouteMatchInterface $route_match)
{
switch ($route_name) {
// Main module help for the nodeshop module.
case 'help.page.blaetter_formatters':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t(
'This module adds formatters to Drupal fields. Currently there are the following formatters:'
) . '</p>';
$output .= '<ul>';
$output .= '<li>' . t(
'<strong>Blätter inline formatter</strong>: This formatter takes entity references and displays their ' .
'values inline with a configurable seperator. You can also specify an optional prefix and decide, if ' .
'values should be linked to its entity pages or not.'
) . '</li>';
$output .= '</ul>';
return $output;
}
return '';
}
/**
* Implements hook_theme()
*
* @see hook_theme()
*
* @return array
*/
function blaetter_formatters_theme()
{
return [
'blaetter_embed_block' => [
'template' => 'blaetter_embed_block',
'variables' => [
'embed' => [],
]
],
'blaetter_formatters_inline' => [
'template' => 'blaetter_formatters_inline',
'variables' => [
'entities' => [],
'separator' => '',
'prefix' => '',
]
],
'blaetter_formatters_issue' => [
'template' => 'blaetter_formatters_issue',
'variables' => [
'book_id' => '',
'edition' => '',
'year' => '',
'display_pages' => '',
'page_from' => '',
'page_to' => '',
'issue_prefix' => '',
'extra_styles' => ''
]
],
'blaetter_grid_block' => [
'template' => 'blaetter_grid_block',
'variables' => [
'grid_class_left' => '',
'grid_class_right' => '',
'title' => '',
'title_left' => '',
'content_left' => '',
'title_right' => '',
'content_right' => '',
]
],
'blaetter_grid_block_title' => [
'template' => 'blaetter_grid_block_title',
'variables' => [
'title' => '',
]
],
'blaetter_socialmedia_block' => [
'template' => 'blaetter_socialmedia_block',
'variables' => [
'socialmedia' => [],
]
]
];
}
/**
* Set dynamic allowed values for the provider field of the media type oembed.
*
* @param \Drupal\field\Entity\FieldStorageConfig $definition
* The field definition.
* @param \Drupal\Core\Entity\ContentEntityInterface|null $entity
* The entity being created if applicable.
* @param bool $cacheable
* Boolean indicating if the results are cacheable.
*
* @return array
* An array of possible key and value options.
*
* @see options_allowed_values()
*/
function blaaetter_formatters_allowed_media_providers(FieldStorageConfig $definition, ContentEntityInterface $entity = NULL, $cacheable) {
$options = [];
if (null !== $entity && 'generic_iframe' == $entity->bundle()) {
$options = [
'Detektor.fm' => 'Detektor.fm',
'Google Maps' => 'Google Maps',
];
} elseif (null !== $entity && 'remote_video' == $entity->bundle()) {
$options = [
'YouTube' => 'YouTube',
'Vimeo' => 'Vimeo',
];
}
return $options;
}
/**
* Implements hook_mail().
*/
function blaetter_formatters_mail($key, &$message, $params) {
$options = array(
'langcode' => $message['langcode'],
);
switch ($key) {
case 'user_update':
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('User updated profile', $options);
$message['body'][] = $params['message'];
break;
}
}
/**
* implements hook_mail_alter()
* @author Paul Reny
* @author Christoph Heger
* @author Andreas Mängel
* Custom format the contact us form, otherwise it's full of nbsp terribleness
*/
function blaetter_formatters_mail_alter(&$message) {
// only alter contact forms
if (!empty($message['id'])
&& (
$message['id'] == 'contact_page_mail' || $message['id'] == 'contact_page_copy'
)
) {
// get the contact message object
$contact_message = $message['params']['contact_message'];
// get the form id to decide which type of mail we need to send
$contact_type = $contact_message->getContactForm()->get('id');
// empty the body
$message['body'] = [];
// get the fields and rebuild the body
$fields = $contact_message->getFields();
$new_body = '';
if ('sepa_lastschriftmandat' == $contact_type) {
// create some variables to use the values easier.
$name = $contact_message->get('field_vorname')->value . ' ' . $contact_message->get('field_nachname')->value;
$ermaechtigung = $contact_message->get('field_ermaechtigung')->value ? 'ja' : 'nein';
$datenschutzhinweise = $contact_message->get('field_datenschutzhinweise')->value ? 'ja' : 'nein';
if ('contact_page_mail' == $message['id']) {
$new_body .= 'Hallo,' . PHP_EOL . PHP_EOL;
$new_body .= $name . ' hat ein SEPA-Lastschriftmandat hinterlegt:' . PHP_EOL . PHP_EOL;
} elseif ('contact_page_copy' == $message['id']) {
$new_body .= 'Hallo ' . $name . ',' . PHP_EOL . PHP_EOL;
$new_body .= 'vielen Dank für die Hinterlegung Ihres SEPA-Mandats. ';
$new_body .= 'Wir haben folgende Nachricht von Ihnen erhalten: ' . PHP_EOL . PHP_EOL;
}
$new_body .= 'Name: ' . $name . PHP_EOL;
$new_body .= 'E-Mail: ' . $contact_message->get('mail')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Anschrift:' . PHP_EOL;
$new_body .= $contact_message->get('field_strasse_und_hausnummer')->value . PHP_EOL;
$new_body .= $contact_message->get('field_postleitzahl')->value . ' ';
$new_body .= $contact_message->get('field_stadt')->value . PHP_EOL;
$new_body .= $contact_message->get('field_land')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Bankverbindung: ' . PHP_EOL;
$new_body .= $contact_message->get('field_iban')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Kundennummer: ' . $contact_message->get('field_kundennummer')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Ermächtigung des Einzugs: ' . $ermaechtigung . PHP_EOL;
$new_body .= 'Zustimmung Datenschutzbedingungen: ' . $datenschutzhinweise . PHP_EOL;
} else if ('kuendigung_abonnement' == $contact_type) {
// create some variables to use the values easier.
$name = $contact_message->get('field_vorname')->value . ' ' . $contact_message->get('field_nachname')->value;
$datenschutzhinweise = $contact_message->get('field_datenschutzhinweise')->value ? 'ja' : 'nein';
$cancel_date = \Drupal::service('date.formatter')->format(\Drupal::time()->getCurrentTime());
if ('contact_page_mail' == $message['id']) {
$new_body .= 'Hallo,' . PHP_EOL . PHP_EOL;
$new_body .= $name . ' hat eine Kündigung hinterlegt:' . PHP_EOL . PHP_EOL;
} elseif ('contact_page_copy' == $message['id']) {
$new_body .= 'Hallo ' . $name . ',' . PHP_EOL . PHP_EOL;
$new_body .= 'wir haben Ihre Kündigung mit den folgenden Daten erhalten: ' . PHP_EOL . PHP_EOL;
}
$new_body .= 'Name: ' . $name . PHP_EOL;
$new_body .= 'E-Mail: ' . $contact_message->get('mail')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Anschrift:' . PHP_EOL;
$new_body .= $contact_message->get('field_strasse_und_hausnummer')->value . PHP_EOL;
$new_body .= $contact_message->get('field_postleitzahl')->value . ' ';
$new_body .= $contact_message->get('field_stadt')->value . PHP_EOL;
$new_body .= $contact_message->get('field_land')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Kundennummer: ' . $contact_message->get('field_kundennummer')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Gewünschter Kündigungszeitraum: ' . $contact_message->get('field_kuendigungszeitraum')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Nachricht:' . PHP_EOL;
$new_body .= $contact_message->get('field_nachricht')->value . PHP_EOL . PHP_EOL;
$new_body .= 'Zustimmung Datenschutzbedingungen: ' . $datenschutzhinweise . PHP_EOL . PHP_EOL;
$new_body .= 'Zeitpunkt des Eingangs der Kündigung: ' . $cancel_date . PHP_EOL . PHP_EOL . PHP_EOL;
$new_body .= 'Bitte speichern Sie sich diese Nachricht als Nachweis über den Kündigungseingang ab.' . PHP_EOL . PHP_EOL;
$new_body .= 'Vielen Dank, Ihre »Blätter«';
//create a message for the end user containing date and time of the cancellation and give further instructions
\Drupal::Messenger()->addMessage(
t(
'We have received a cancellation of your subscription at %date. We did send you a mail containg all ' .
'entered information as well as the confirmation of the date and time of your cancellation. Please make' .
' sure to save the message.',
[
'%date' => $cancel_date,
]
)
);
} else {
// check if we have a category and set up the subject if so
if ($contact_message->hasField('field_category') && !empty($contact_message->get('field_category')->value)) {
$message['subject'] = str_replace(
'Website-Feedback',
$contact_message->get('field_category')->value,
$message['subject']
);
}
if ('contact_page_mail' == $message['id']) {
$new_body .= $contact_message->get('name')->value . ' hat folgende Nachricht über das Kontaktformular auf ' .
'https://www.blaetter.de/contact geschickt:' . PHP_EOL . PHP_EOL;
} elseif ('contact_page_copy' == $message['id']) {
$new_body .= 'Hallo ' . $contact_message->get('name')->value . ',' . PHP_EOL . PHP_EOL;
$new_body .= 'vielen Dank für Ihre Anfrage, wir haben folgende Nachricht von Ihnen erhalten: ' . PHP_EOL . PHP_EOL;
}
$new_body .= $contact_message->get('message')->value . PHP_EOL . PHP_EOL;
foreach ($fields as $field_name => $field) {
// as we use the category for the subject, there is no need to display it again here.
if ('field_category' == $field_name) {
continue;
}
if (get_class($field->getFieldDefinition()) == 'Drupal\field\Entity\FieldConfig') {
$new_body .= $field->getFieldDefinition()->label() . ':' . PHP_EOL .
$contact_message->get($field_name)->value . PHP_EOL . PHP_EOL;
}
}
// add a footer for mails sended to the user in copy
if ('contact_page_copy' == $message['id']) {
$new_body .= '--' . PHP_EOL . PHP_EOL;
$new_body .= 'Haben Sie vielen Dank für Ihre Mitteilung an den Blätter-Bestellservice.' . PHP_EOL;
$new_body .= 'Wir haben die Nachricht erhalten und bemühen uns, diese schnellstmöglich' . PHP_EOL;
$new_body .= 'zu beantworten. In dringenden Fällen können Sie uns auch telefonisch' . PHP_EOL;
$new_body .= 'erreichen: 030/30883644.' . PHP_EOL;
}
}
// reset the body of the message
$message['body'][] = trim($new_body);
}
}
/**
* Implements hook_ENTITY_TYPE_update
*
* Checks for updates on the user account and sends mail to site owner in case the user changed its address
*
* @param EntityInterface $entity
* @return void
*/
function blaetter_formatters_user_update(EntityInterface $entity)
{
// we do only want to hook into the update of a user account.
if ('user' == $entity->bundle()) {
$user_account_changed = false;
// we need to check certain fields for changes:
$fields = [
'field_salutation' => t('salutation'),
'field_first_name' => t('first name'),
'field_last_name' => t('last name'),
'field_phone' => t('phone'),
'field_street' => t('street'),
'field_street_addon' => t('street addon'),
'field_zip' => t('postal code'),
'field_city' => t('city'),
'field_country' => t('country')
];
$mail_body = t(
'Changed contact information for account @id (@url)',
[
'@id' => $entity->id(),
'@url' => $entity->toUrl('canonical', ['absolute' => true])->toString(),
]
) . PHP_EOL . PHP_EOL;
foreach ($fields as $key => $name) {
if ($entity->get($key)->value !== $entity->original->get($key)->value) {
$user_account_changed = true;
$mail_body .= t(
'@name changed from "@orig_value" to "@new_value"',
[
'@name' => $name,
'@orig_value' => $entity->original->get($key)->value,
'@new_value' => $entity->get($key)->value,
]
) . PHP_EOL;
}
}
if (true === $user_account_changed) {
$mailManager = \Drupal::service('plugin.manager.mail');
$to = \Drupal::config('nodeshop.settings')->get('payment.mail_to');
$params['message'] = $mail_body;
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$result = $mailManager->mail('blaetter_formatters', 'user_update', $to, $langcode, $params, NULL, $send);
if (true !== $result['result']) {
\Drupal::logger('blaetter_formatters')->warning(
'Could not send user update notification mail for user @id',
[
'@id' => $entity->id()
]
);
}
}
}
}
/**
* Implementation of hook_form_alter
*
* @param array $form The drupal form array as a reference
* @param \Drupal\Core\Form\FormState $form_state
* @param string $form_id The Drupal form id
* @return void
*/
function blaetter_formatters_form_alter(&$form, \Drupal\Core\Form\FormState $form_state, $form_id) {
// Search form alter
if ('search_form' == $form_id) {
$moduleHandler = \Drupal::service('module_handler');
if ($moduleHandler->moduleExists('honeypot')) {
\Drupal::service('honeypot')->addFormProtection($form, $form_state, array('honeypot', 'time_restriction'));
}
}
if ('contact_message_kuendigung_abonnement_form' == $form_id) {
$form['actions']['submit']['#value'] = t('Submit termination');
// always send copies to the user.
$form['copy']['#value'] = true;
}
}