-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent_lock.module
More file actions
executable file
·272 lines (237 loc) · 7.88 KB
/
content_lock.module
File metadata and controls
executable file
·272 lines (237 loc) · 7.88 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
<?php
/**
* @file
* Content lock - Main functions of the module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\user\Entity\User;
use Drupal\Core\Url;
use Drupal\Core\Routing\LocalRedirectResponse;
/**
* Implements hook_help().
*/
function content_lock_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
case 'help.page.content_lock':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Prevents multiple users from trying to edit a single node simultaneously to prevent edit conflicts.') . '</p>';
return $output;
}
}
/**
* Implements hook_hook_info().
*/
function content_lock_hook_info() {
$hooks = array(
'content_lock_path_protected',
'content_lock_skip_locking',
'content_lock_form_id_blacklist_alter',
'content_lock_node_type_blacklist_alter',
'content_lock_locked',
'content_lock_release',
'content_lock_entity_lockable',
);
return array_fill_keys($hooks, array('group' => 'content_lock'));
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function content_lock_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$base_form_id = isset($form_state->getBuildInfo()['base_form_id']) ? $form_state->getBuildInfo()['base_form_id'] : NULL;
$base_form_id_supported = [
'node_form',
'block_content_form',
'taxonomy_term_form',
];
if (!in_array($base_form_id, $base_form_id_supported)) {
return;
}
/** @var \Drupal\core\Entity\ContentEntityInterface $entity */
$entity = $form_state->getFormObject()->getEntity();
$entity_type = $entity->getEntityTypeId();
$user = Drupal::currentUser();
// Check if we must lock this entity.
/** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
$lock_service = \Drupal::service('content_lock');
if (!$lock_service->isLockable($entity)) {
return;
}
// We act only on edit form, not for a creation of a new entity.
if (!is_null($entity->id())) {
$form['actions']['submit']['#submit'][] = 'content_lock_form_submit';
$form['actions']['publish']['#submit'][] = 'content_lock_form_submit';
// This hook function is called twice, first when the form loads
// and second when the form is submitted.
// Only perform set and check for lock on initial form load.
$userInput = $form_state->getUserInput();
if (!empty($userInput)) {
return;
}
// We lock the content if it is currently edited by another user.
if (!$lock_service->locking($entity->id(), $user->id(), $entity_type)) {
$form['#disabled'] = TRUE;
// Do not allow deletion, publishing, or unpublishing if locked.
if (isset($form['actions']['delete'])) {
unset($form['actions']['delete']);
}
if (isset($form['actions']['publish'])) {
unset($form['actions']['publish']);
}
if (isset($form['actions']['unpublish'])) {
unset($form['actions']['unpublish']);
}
}
else {
// ContentLock::locking() returns TRUE if the content is locked by the
// current user. Add an unlock button only for this user.
$form['actions']['unlock'] = $lock_service->unlockButton($entity_type, $entity->id(), \Drupal::request()->query->get('destination'));
}
}
}
/**
* Submit handler for content_lock.
*/
function content_lock_form_submit($form, FormStateInterface $form_state) {
// Signals editing is finished; remove the lock.
$user = \Drupal::currentUser();
/** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
$lock_service = \Drupal::service('content_lock');
/** @var \Drupal\core\Entity\ContentEntityInterface $entity */
$entity = $form_state->getFormObject()->getEntity();
// If the user submitting owns the lock, release it.
$lock_service->release($entity->id(), $user->id(), $entity->getEntityTypeId());
// We need to redirect to the taxonomy term page after saving it. If not, we
// stay on the taxonomy term edit form and we relock the term.
if ($entity->getEntityTypeId() === 'taxonomy_term') {
$form_state->setRedirect(
'entity.taxonomy_term.canonical',
array('taxonomy_term' => $entity->id())
);
}
}
/**
* Implements hook_entity_predelete().
*
* Check if the entity attempting to be deleted is locked and prevent deletion.
*/
function content_lock_entity_predelete(EntityInterface $entity) {
$entity_id = $entity->id();
$entity_type = $entity->getEntityTypeId();
// We support BlockContent, Term and Node entity type only.
$entity_type_supported = [
'node',
'block_content',
'taxonomy_term',
];
if (!in_array($entity_type, $entity_type_supported)) {
return;
}
/** @var \Drupal\content_lock\ContentLock\ContentLock $lock_service */
$lock_service = \Drupal::service('content_lock');
$data = $lock_service->fetchLock($entity_id, $entity_type);
if ($data !== FALSE) {
$current_user = \Drupal::currentUser();
// If the entity is locked, and current user is not the lock's owner,
// set a message and stop deletion.
if ($current_user->id() !== $data->uid) {
$lock_user = User::load($data->uid);
$message = t('@entity cannot be deleted because it was locked by @user since @time.', array(
'@entity' => $entity->label(),
'@user' => $lock_user->getDisplayName(),
'@time' => \Drupal::service('date.formatter')->formatInterval(REQUEST_TIME - $data->timestamp),
));
drupal_set_message($message, 'warning');
$url = Url::fromRoute('entity.' . $entity_type . '.canonical', array($entity_type => $entity_id))->toString();
$redirect = new LocalRedirectResponse($url);
$redirect->send();
exit(0);
}
}
}
/**
* Implements hook_views_data().
*/
function content_lock_views_data() {
// Define the return array.
$data = [];
$data['content_lock']['table']['group'] = t('Content lock');
$data['content_lock']['table']['provider'] = 'content_lock';
$data['content_lock']['table']['join'] = array(
'node_field_data' => array(
'left_field' => 'nid',
'field' => 'entity_id',
'extra' => array(
0 => array(
'field' => 'entity_type',
'value' => 'node',
),
),
),
'users_field_data' => [
'left_field' => 'uid',
'field' => 'uid',
],
);
$data['content_lock']['nid'] = array(
'title' => t('Node locked'),
'help' => t('The node being locked.'),
'relationship' => array(
'base' => 'node_field_data',
'base field' => 'nid',
'id' => 'standard',
'label' => t('Node locked'),
),
);
$data['content_lock']['uid'] = array(
'title' => t('Lock owner'),
'help' => t('The user locking the node.'),
'relationship' => array(
'base' => 'users_field_data',
'base field' => 'uid',
'id' => 'standard',
'label' => t('Lock owner'),
),
);
$data['content_lock']['timestamp'] = array(
'title' => t('Lock Date/Time'),
'help' => t('Timestamp of the lock'),
'field' => array(
'id' => 'date',
'click sortable' => TRUE,
),
'sort' => array(
'id' => 'date',
),
'filter' => array(
'id' => 'date',
),
);
$data['content_lock']['is_locked'] = array(
'title' => t('Is Locked'),
'help' => t('Whether the node is currently locked'),
'field' => array(
'id' => 'content_lock_field',
'additional fields' => ['timestamp' => ['table' => 'content_lock', 'field' => 'timestamp']],
),
'sort' => array(
'id' => 'content_lock_sort',
),
'filter' => array(
'id' => 'content_lock_filter',
'click sortable' => TRUE,
),
);
// Break link.
$data['content_lock']['break'] = array(
'title' => t('Break link'),
'help' => t('Link to break the content lock.'),
'field' => array(
'id' => 'content_lock_break_link',
'real field' => 'entity_id',
),
);
return $data;
}