forked from jez500/Private-Message-Messenger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
private_message_messenger.module
executable file
·221 lines (200 loc) · 7.25 KB
/
private_message_messenger.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
<?php
/**
* @file
* The private_message_messenger.module file.
*/
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
/**
* Implements hook_theme().
*/
function private_message_messenger_theme($existing, $type, $theme, $path) {
return [
'pmm_thread' => [
'variables' => [
'fallback_picture' => NULL,
'inbox_text' => NULL,
'delete_text' => NULL,
'base_path' => '/',
'thread_actions' => TRUE,
],
],
'pmm_threads' => [
'variables' => [
'new_message_text' => NULL,
'loading_text' => NULL,
],
],
'pmm_thread_teaser' => [
'variables' => [
'fallback_picture' => NULL,
'id_suffix' => NULL,
],
],
'pmm_recent' => [
'variables' => [
'link_text' => '',
'more_text' => '',
'loading_text' => NULL,
],
],
];
}
/**
* Implements hook_preprocess_pmm_threads().
*/
function template_preprocess_pmm_threads(&$variables) {
$variables['#attached']['library'][] = 'private_message_messenger/threads';
$variables['new_message_text'] = t('New Message');
$variables['loading_text'] = t('Loading...');
}
/**
* Implements hook_preprocess_pmm_thread().
*/
function template_preprocess_pmm_thread(&$variables) {
$variables['#attached']['library'][] = 'private_message_messenger/thread';
$variables['fallback_picture'] = _private_message_messenger_fallback_picture();
$variables['inbox_text'] = t('‹ Inbox');
$variables['delete_text'] = t('Delete thread');
$variables['base_path'] = base_path();
}
/**
* Implements hook_preprocess_pmm_thread().
*/
function template_preprocess_pmm_thread_teaser(&$variables) {
$variables['fallback_picture'] = _private_message_messenger_fallback_picture();
}
/**
* Implements hook_preprocess_pmm_recent().
*/
function template_preprocess_pmm_recent(&$variables) {
$helper = \Drupal::service('private_message_messenger.messenger');
$variables['#attached']['library'][] = 'private_message_messenger/recent';
$variables['link_text'] = t('Messages');
$variables['all_text'] = t('All Messages');
$variables['loading_text'] = t('Loading...');
$variables['url'] = $helper->getMessengerPath();
}
/**
* Implements hook_form_private_message_config_form_alter().
*/
function private_message_messenger_form_private_message_config_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$helper = \Drupal::service('private_message_messenger.messenger');
$form['pmm'] = [
'#type' => 'container',
'#weight' => 20,
'title' => ['#markup' => '<h3>' . t('Messenger') . '</h3>'],
];
// Set a default text format for messages.
$form['pmm']['preferred_text_format'] = [
'#type' => 'select',
'#title' => t('Prefered text format for messages'),
'#description' => t('You should ensure that users who have access to "use private message" can also access this format.'),
'#default_value' => $helper->getConfig('preferred_text_format', 'plain_text'),
'#options' => [],
];
foreach (filter_formats() as $format) {
$form['pmm']['preferred_text_format']['#options'][$format->id()] = $format->label();
}
// Set a default image style for messages.
$styles = \Drupal::entityTypeManager()->getStorage('image_style')->loadMultiple();
$form['pmm']['image_style'] = [
'#type' => 'select',
'#title' => t('Profile picture Image style'),
'#description' => t('What image style is used in the messenger interface'),
'#default_value' => $helper->getConfig('image_style', $helper::IMAGE_STYLE_DEFAULT),
'#options' => [],
];
foreach ($styles as $style) {
$form['pmm']['image_style']['#options'][$style->id()] = $style->label();
}
// The default thread count to get.
$form['pmm']['thread_count'] = [
'#type' => 'number',
'#title' => t('Number of threads to show'),
'#description' => t('The number of threads to be shown in the block'),
'#default_value' => $helper->getConfig('thread_count', $helper::THREAD_COUNT_DEFAULT),
'#min' => 1,
];
// The default ajax refresh rate.
$form['pmm']['ajax_refresh_rate'] = [
'#type' => 'number',
'#title' => t('Ajax refresh rate'),
'#default_value' => $helper->getConfig('ajax_refresh_rate', $helper::AJAX_REFRESH_DEFAULT),
'#min' => 0,
'#description' => t(
'The number of seconds after which the inbox should refresh itself.
Setting this to a low number will result in more requests to the server,
adding overhead and bandwidth. Setting this number to zero will disable
ajax refresh, and the inbox will only updated if/when the page is refreshed.'),
];
$form['#submit'][] = 'private_message_messenger_form_private_message_config_form_submit';
}
/**
* Submit handler for private message config form.
*/
function private_message_messenger_form_private_message_config_form_submit(&$form, FormStateInterface $form_state) {
\Drupal::configFactory()->getEditable('private_message.settings')
->set('preferred_text_format', $form_state->getValue('preferred_text_format'))
->set('image_style', $form_state->getValue('image_style'))
->set('thread_count', $form_state->getValue('thread_count'))
->set('ajax_refresh_rate', $form_state->getValue('ajax_refresh_rate'))
->save();
}
/**
* Get the fallback image.
*
* @return string
* Path to the default image.
*/
function _private_message_messenger_fallback_picture() {
$img = base_path() . drupal_get_path('module', 'private_message_messenger') . '/images/default-profile.jpg';
\Drupal::moduleHandler()->alter('private_message_messenger_fallback_picture', $img);
return $img;
}
/**
* A wrapper to return messenger settings.
*
* @return array
* Array of settings for drupalSettings.
*/
function _private_message_messenger_get_settings() {
$helper = \Drupal::service('private_message_messenger.messenger');
return $helper->getSettings();
}
/**
* Implements hook_entity_extra_field_info().
*/
function private_message_messenger_entity_extra_field_info() {
$fields = [];
$user_bundles = \Drupal::entityManager()->getBundleInfo('user');
foreach (array_keys($user_bundles) as $bundle) {
$fields['user'][$bundle]['display']['private_message_messenger_link'] = [
'label' => t('Private message messenger thread link'),
'description' => t('Displays a link to send a private message to the user via Messenger'),
'visible' => FALSE,
];
}
return $fields;
}
/**
* Implements hook_user_view().
*/
function private_message_messenger_user_view(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display, $view_mode) {
if ($display->getComponent('private_message_messenger_link')) {
$current_user = \Drupal::currentUser();
if ($current_user->hasPermission('use private messaging system') && $current_user->id() != $entity->id()) {
$path = 'new-' . $entity->id();
$build['private_message_messenger_link'] = [
'#type' => 'link',
'#url' => Url::fromRoute('private_message_messenger.messenger', [], ['fragment' => $path]),
'#title' => t('Send private message'),
'#attributes' => ['class' => ['private_message_link']],
'#prefix' => '<div class="pmm_link_wrapper">',
'#suffix' => '</div>',
];
}
}
}