-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact_entity.module
83 lines (76 loc) · 2.44 KB
/
contact_entity.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
<?php
/**
* @file
* Contains contact_entity.module.
*/
use Drupal\Component\Utility\Html;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
/**
* Implements hook_help().
*/
function contact_entity_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the contact_entity module.
case 'help.page.contact_entity':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Custom entity for contact') . '</p>';
return $output;
default:
}
}
/**
* Recommend for use, but not require...
* Implements hook_mail().
*/
function contact_entity_mail($key, &$message, $params) {
$options = [
'langcode' => $message['langcode'],
];
switch ($key) {
case 'contact_message':
// Need to send a formatted text.
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed; delsp=yes';
$message['from'] = \Drupal::config('system.site')->get('mail');
$message['subject'] = t('User have subscribed, his telephone: @telephone', ['@telephone' => $params['telephone']], $options);
$message['body'][] = Html::escape($params['message'][0]['value']);
break;
}
}
/**
* Create email only for created entity.
* Implements hook_entity_insert().
*/
function contact_entity_entity_insert(EntityInterface $entity) {
if ($entity->getEntityTypeId() !== 'contact_entity') {
return;
}
$mailManager = \Drupal::service('plugin.manager.mail');
$module = 'contact_entity';
$key = 'contact_message';
$to = $entity->get('email')->getString();
$params['message'] = $entity->get('message')->getValue();
$params['telephone'] = $entity->get('telephone')->getString();
$language_code = $entity->language()->getId();
$send_now = TRUE;
$mail = $mailManager->mail($module, $key, $to, $language_code, $params, NULL, $send_now);
if ($mail['result'] == TRUE) {
drupal_set_message(t('Your message has been sent.'));
}
else {
drupal_set_message(t('There was a problem sending your message and it was not sent.'), 'error');
}
}
/**
* Implements hook_mail_alter().
*/
function contact_entity_mail_alter(&$message) {
// Id is module name plus key.
if (!empty($message['id']) && $message['id'] == 'contact_entity_contact_message') {
$reverse = $message['from'];
$message['from'] = $message['to'];
$message['to'] = $reverse;
}
// dpm($message); // For debug via devel.
}