forked from mohankumargupta/mentionsdrupal8module
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmentions.module
211 lines (180 loc) · 6.73 KB
/
mentions.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
<?php
/**
* @file mentions.module
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Symfony\Component\EventDispatcher\Event;
use Drupal\mentions\Plugin\Filter\MentionsFilter;
function mentions_help($route_name, RouteMatchInterface $route_match) {
switch($route_name) {
case 'entity.mentions_type.list':
$textformaturl = Url::fromRoute('filter.admin_overview');
$link_textformatpage = \Drupal::l('text formats', $textformaturl);
$output = '<p>Don\'t forget to enable \'Mentions filter\' for the mentions type below for the appropriate '. $link_textformatpage .'</p>';
return $output;
}
}
/**
* Provides hook implementations for mentions module.
*/
function mentions_theme() {
return array(
'mentions' => array(
'variables' => array(
'mentionsid' => NULL,
'link' => NULL,
'renderlink' => FALSE,
'rendervalue' => ''
)
)
);
}
/**
* Implements template_preprocess_mentions().
*/
function template_preprocess_mentions(&$variables) {
//$settings = \Drupal::config('mentions.mentions');
//print_r($variables);
//$variables['userid'] = 5;
//$variables['link'] = '<a href="http://google.com">';
// $variables['userintext']['userid'] = 5;
//$variables['userintext']['link'] = '<a href="http://google.com">';
/*
global $base_url;
global $base_root;
foreach (array('text', 'link') as $type) {
if (!empty($output_settings[$type])) {
$content = \Drupal::token()->replace($output_settings[$type], array('user' => $user));
if ($type == 'text') {
$text = $content;
}
else {
$link = htmlentities($content);
}
}
}
$user_page_url = Url::fromUri('base:' . $link);
$variables['userid'] = '';
$variables['link'] = \Drupal::l($output_settings['prefix'] . $text . $output_settings['suffix'], $user_page_url, array('attributes' => array('class' => 'mentions mentions-' . $variables['userid'] , 'title' => $text)))->getGeneratedLink();
* }
*/
}
/**
* Implements hook_entity_insert().
*/
function mentions_entity_insert(Drupal\Core\Entity\EntityInterface $entity) {
$entity_type = $entity->getEntityType();
$entity_type_id = $entity->getEntityTypeId();
mentions_insert_or_update($entity, FALSE);
}
/**
* Implements hook_entity_update().
*/
function mentions_entity_update(Drupal\Core\Entity\EntityInterface $entity) {
$entity_type = $entity->getEntityType();
$entity_type_id = $entity->getEntityTypeId();
mentions_insert_or_update($entity, TRUE);
}
/**
* Handling mentions crud operations.
*/
function mentions_insert_or_update(Drupal\Core\Entity\EntityInterface $entity, $update) {
//$entity_type = $entity->getEntityType();
//$entity_type_id = $entity->getEntityTypeId();
$supported_entities = array('node', 'comment', 'taxonomy_term','user');
if (!in_array($entity->getEntityTypeId(), $supported_entities)) {
return;
}
$type = $entity->getEntityType()->getProvider();
$field_definitions = $entity->getFieldDefinitions();
$fields_with_text = array();
$supported_field_types = array('text_with_summary', 'text_long', 'text');
foreach ($field_definitions as $field_definition) {
$fieldtype = $field_definition->getType();
if (isset($fieldtype) && in_array($fieldtype, $supported_field_types)) {
array_push($fields_with_text, $field_definition->getName());
}
}
foreach ($fields_with_text as $textfield) {
$boo = $entity->get($textfield);
$value = $boo->value;
$text_format = $boo->format;
$container = \Drupal::getContainer();
$filter_mentions = MentionsFilter::create($container, array(), 'filter_mentions', array('provider'=>'mentions'));
$filter_mentions->setTextFormat($text_format);
//if ($filter_mentions->shouldApplyFilter()) {
// $mentions = $filter_mentions->mentions_get_mentions($value);
// $auid = \Drupal::currentUser()->id();
// mentions_crud_update($type, $mentions, $entity->id(), $auid);
//}
//$value = $filter_mentions->process($value, 'en');
//$value = $value->getProcessedText();
if ($filter_mentions->shouldApplyFilter()) {
$mentions = $filter_mentions->filter_mentions_structure($value);
$auid = \Drupal::currentUser()->id();
foreach($mentions as $mention) {
$loo = empty($mention);
if (!$loo)
mentions_crud_update($type, $mention, $entity->id(), $auid);
}
}
}
}
/**
* Implements hook_entity_delete().
*/
function mentions_entity_delete(Drupal\Core\Entity\EntityInterface $entity) {
$mention_ids = \Drupal::entityQuery('mentions')->condition('entity_id', $entity->id())->condition('entity_type', $entity->getEntityType()->getProvider())->execute();
entity_delete_multiple('mentions', $mention_ids);
$event_dispatcher = \Drupal::service('event_dispatcher');
$event_dispatcher->dispatch('mentions.delete', new Event());
}
/**
* Insert mentions into DB.
*/
function mentions_crud_update($type, $mentions, $id, $author) {
// Build old array of mentions.
$old_users = array();
$old_mids = array();
$mention_ids = \Drupal::entityQuery('mentions')->condition('entity_type', $type)->condition('entity_id', $id)->execute();
foreach ($mention_ids as $mention) {
$entity = entity_load('mentions', $mention);
$old_user = $entity->get('uid')->getValue()[0]['value'];
$old_users[] = $old_user;
$old_mids[$old_user] = $mention;
}
// Build array of new mentions.
$new_users = array();
foreach ($mentions as $mention) {
$new_users[] = $mention['target']['entity_id'];
}
$event_dispatcher = \Drupal::service('event_dispatcher');
// Add new mentions.
foreach (array_diff($new_users, $old_users) as $uid) {
$mentionattributes = array(
'entity_type' => $type,
'entity_id' => $id,
'uid' => $uid,
'auid' => $author);
$mention = entity_create('mentions', $mentionattributes);
$mention->save();
$event_dispatcher->dispatch('mentions.insert', new Event());
}
// Update existing mentions.
foreach (array_intersect($new_users, $old_users) as $uid) {
$entity = entity_load('mentions', $old_mids[$uid]);
$entity->set('created', REQUEST_TIME);
$entity->save();
$event_dispatcher->dispatch('mentions.update', new Event());
}
// Delete old mentions.
foreach (array_diff($old_users, $new_users) as $uid) {
$entity = entity_load('mentions', $old_mids[$uid]);
$entity->delete();
$event_dispatcher->dispatch('mentions.delete', new Event());
}
}
function mentions_page_attachments_alter(&$build) {
$build['#attached']['library'][] = 'mentions/mentions';
}