forked from dpc-sdp/tide_core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tide_core.module
417 lines (385 loc) · 14.6 KB
/
tide_core.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
<?php
/**
* @file
* Contains tide_core.module.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Url;
use Drupal\node\NodeInterface;
use Drupal\scheduled_transitions\Routing\ScheduledTransitionsRouteProvider;
use Drupal\scheduled_transitions\ScheduledTransitionsPermissions;
use Drupal\user\Entity\Role;
use Drupal\views\ViewExecutable;
use Drupal\workflows\Entity\Workflow;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Markup;
use Drupal\Core\Session\AccountInterface;
use Drupal\node\Entity\Node;
use Drupal\Core\Access\AccessResult;
/**
* Implements hook_views_data().
*/
function tide_core_views_data() {
$data = [];
$data['views']['text_raw'] = [
'title' => t('Raw text'),
'help' => t('Add raw custom text or markup. This is similar to the custom text field.'),
'area' => [
'id' => 'text_raw',
],
];
return $data;
}
/**
* Implements hook_views_data_alter().
*
* @see \Drupal\node\NodeViewsData::getViewsData()
* @see \Drupal\tide_core\Plugin\views\filter\StatusModerated
*/
function tide_core_views_data_alter(array &$data) {
// Add Moderation support to Node 'status_extra' filter.
// Content Overview view uses the filter 'Published status or admin user'
// without any consideration of Content Moderation. We override this filter
// to add an extra check for the 'view any unpublished content' permission
// provided by the Content Moderation module.
$data['node_field_data']['status_extra']['filter']['id'] = 'node_status_moderated';
$data['file_managed']['file_type_filter'] = [
'title' => t('Enhanced file MIME type filter'),
'filter' => [
'title' => t('Enhanced file MIME type filter'),
'help' => t('provide a custom file MIME type selection filter'),
'field' => 'filemime',
'id' => 'tide_enhanced_mime_type_filter',
],
];
}
/**
* Implements hook_views_query_substitutions().
*
* @see node_views_query_substitutions()
* @see \Drupal\node\Plugin\views\filter\Status::query()
* @see \Drupal\tide_core\Plugin\views\filter\StatusModerated::query()
*/
function tide_core_views_query_substitutions(ViewExecutable $view) {
$account = \Drupal::currentUser();
return [
'***VIEW_ANY_UNPUBLISHED_NODES***' => intval($account->hasPermission('view any unpublished content')),
];
}
/**
* Implements hook_entity_operation().
*/
function tide_core_entity_operation(EntityInterface $entity) {
// Add "Archived" operation link to entities.
$operations = [];
$workflow = Workflow::load('editorial');
if ($workflow) {
if ($workflow->getTypePlugin()
->appliesToEntityTypeAndBundle($entity->getEntityTypeId(), $entity->bundle())) {
if ($entity->access('use ' . $workflow->id() . ' transition archived')) {
$operations['archive'] = [
'title' => t('Archive'),
'weight' => 100,
'url' => Url::fromRoute('tide_core.entity.archive_confirm', ['bundle' => $entity->getEntityTypeId(), 'entity_type_id' => $entity->id()]),
];
}
}
}
if ($entity->getEntityType()->hasLinkTemplate(ScheduledTransitionsRouteProvider::LINK_TEMPLATE_ADD)) {
$routeName = ScheduledTransitionsRouteProvider::getScheduledTransitionRouteName($entity->getEntityType());
$url = Url::fromRoute($routeName, [$entity->getEntityTypeId() => $entity->id()]);
$user = \Drupal::currentUser();
if (TRUE === $url->access($user)) {
$operations['scheduled_transitions'] = [
'title' => t('Scheduled updates'),
'url' => $url,
'weight' => 50,
];
}
}
return $operations;
}
/**
* Implements hook_form_alter().
*/
function tide_core_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$info = \Drupal::service('entity_type.bundle.info');
foreach ($info->getBundleInfo('node') as $bundle => $item) {
if ($form_id == 'node_' . $bundle . '_scheduled_transitions_add_form_form') {
// In this form, we only keep 'publish' and 'archive' options regardless
// of what permissions the user has.
if (isset($form['scheduled_transitions']['new_meta']['transition']['#options'])) {
foreach ($form['scheduled_transitions']['new_meta']['transition']['#options'] as $key => $option) {
if (!in_array($key, ['publish', 'archive'])) {
unset($form['scheduled_transitions']['new_meta']['transition']['#options'][$key]);
}
}
}
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#value'] = t('Scheduled updates');
$form['actions'][$action]['#submit'][] = '_tide_core_modified_update_adding_message';
}
}
}
if ($form_id == 'node_' . $bundle . '_quick_node_clone_form') {
if (isset($form['moderation_state'])) {
$form['moderation_state']['#group'] = 'footer';
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tide_core_form_scheduled_transition_delete_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#title'] = t('Are you sure you want to delete the Scheduled updates?');
foreach (array_keys($form['actions']) as $action) {
if ($action != 'preview' && isset($form['actions'][$action]['#type']) && $form['actions'][$action]['#type'] === 'submit') {
$form['actions'][$action]['#submit'][] = '_tide_core_modified_update_delete_message';
}
}
}
/**
* Submit handler for altering updating status message.
*/
function _tide_core_modified_update_adding_message(&$form, $form_state) {
$messenger = \Drupal::messenger();
$messenger->deleteByType('status');
$onDate = $form_state->getValue(['on']);
$messenger->addMessage(t('Scheduled an update for @date', [
'@date' => \Drupal::service('date.formatter')
->format($onDate->getTimestamp()),
]));
}
/**
* Submit handler for altering deleting status message.
*/
function _tide_core_modified_update_delete_message(&$form, $form_state) {
$messenger = \Drupal::messenger();
$messenger->deleteByType('status');
$messenger->addMessage(t('The Scheduled update has been deleted.'), 'status');
}
/**
* Implements hook_menu_links_discovered_alter().
*/
function tide_core_menu_links_discovered_alter(&$links) {
if (isset($links['entity.scheduled_transition.collection']['title'])) {
$links['entity.scheduled_transition.collection']['title'] = 'Scheduled updates';
}
// TODO: delete after scheduled_updates module fully uninstalled.
if (isset($links['entity.scheduled_update.collection'])) {
unset($links['entity.scheduled_update.collection']);
}
}
/**
* Implements hook_menu_local_actions_alter().
*/
function tide_core_menu_local_actions_alter(&$local_actions) {
if (isset($local_actions['scheduled_transitions.actions:node.add_scheduled_transition']['title'])) {
$local_actions['scheduled_transitions.actions:node.add_scheduled_transition']['title'] = 'Add Scheduled update';
}
}
/**
* Implements hook_local_tasks_alter().
*/
function tide_core_local_tasks_alter(&$local_tasks) {
if (isset($local_tasks['scheduled_transitions.tasks:node.scheduled_transitions']['title'])) {
$local_tasks['scheduled_transitions.tasks:node.scheduled_transitions']['title'] = 'Scheduled updates';
}
// TODO: delete after scheduled_updates module fully uninstalled.
if (isset($local_tasks['scheduled_update.admin'])) {
unset($local_tasks['scheduled_update.admin']);
}
}
/**
* Implements hook_entity_bundle_create().
*/
function tide_core_entity_bundle_create($entity_type_id, $bundle) {
if ($entity_type_id == 'node') {
$roles = ['approver', 'site_admin'];
foreach ($roles as $role) {
$permissions = [];
$permissions[] = ScheduledTransitionsPermissions::viewScheduledTransitionsPermission($entity_type_id, $bundle);
$permissions[] = ScheduledTransitionsPermissions::addScheduledTransitionsPermission($entity_type_id, $bundle);
$permissions[] = 'view all scheduled transitions';
user_role_grant_permissions(Role::load($role)->id(), $permissions);
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function tide_core_preprocess_status_messages(&$variables) {
if (isset($variables['message_list']['error']) && !empty($variables['message_list']['error'])) {
foreach ($variables['message_list']['error'] as &$error_message) {
if ($error_message instanceof Markup) {
$message = $error_message->__toString();
// We want to ensure that the error message to be altered under
// node edit context.
preg_match('/entity:node\/(\d+)/', $message, $matches);
// Checking that the error message comes from field_paragraph_link based
// on a node context.
if (strpos($message, 'Validation error on collapsed paragraph field_paragraph_link') !== FALSE && (isset($matches[1]) && is_numeric($matches[1]))) {
$error_message = t('A link in <i>Related links</i> field on this page is broken. Please update or remove the link and retry.');
}
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function tide_core_form_content_moderation_entity_moderation_form_alter(&$form, FormStateInterface $form_state, $form_id) {
if (isset($form['revision_log'])) {
$form['#attached']['library'] = 'tide_core/content_moderation';
$form['revision_log']['#type'] = 'textarea';
$form['revision_log']['#title'] = t('Publishing instructions and comments');
$form['revision_log']['#description'] = t('All content will be published within 24 hours. State below if your content is embargoed or time critical.');
$form['new_state']['#title'] = t('Change status to');
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function tide_core_preprocess_page(&$variables) {
$variables['#attached']['library'][] = 'tide_core/ckeditor_stylesheets';
}
/**
* Implements hook_editor_js_settings_alter().
*/
function tide_core_editor_js_settings_alter(array &$settings) {
foreach (array_keys($settings['editor']['formats']) as $text_format_id) {
$settings['editor']['formats'][$text_format_id]['editorSettings']['contentsCss'][] = '/' . drupal_get_path('module', 'tide_core') . '/css/ckeditor_overrides.css';
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Move some core fields to Node form Sidebar.
*/
function tide_core_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
$form['#attached']['library'][] = 'tide_core/sticky_node_form_sidebar';
$form['#process'][] = '_tide_core_form_node_form_process';
}
/**
* Node form #process callback.
*
* @param array $element
* Form that is being processed.
* @param \Drupal\Core\Form\FormStateInterface $form_state
* The current state of the form.
* @param array $form
* The complete form structure.
*
* @return array
* The processed form.
*/
function _tide_core_form_node_form_process(array $element, FormStateInterface $form_state, array $form = []) {
/** @var \Drupal\node\NodeInterface $node */
$node = $form_state->getFormObject()->getEntity();
$element['tide_core_metadata'] = [
'#type' => 'container',
'#open' => TRUE,
'#optional' => TRUE,
'#access' => TRUE,
'#group' => 'meta',
'#attributes' => [
'class' => ['tide-core-metadata'],
],
'#weight' => 100,
];
$core_fields = [
10 => 'field_node_department',
20 => 'field_tags',
];
foreach ($core_fields as $weight => $core_field) {
if ($node->hasField($core_field)) {
if (isset($element[$core_field])) {
$element[$core_field]['#group'] = 'tide_core_metadata';
$element[$core_field]['#weight'] = $weight;
}
}
}
return $element;
}
/**
* Implements hook_ENTITY_TYPE_access().
*/
function tide_core_menu_link_content_access(EntityInterface $menu_link_item, $operation, AccountInterface $account) {
if ($menu_link_item->hasField('link') && !$menu_link_item->link->isEmpty()) {
$url = $menu_link_item->getUrlObject();
if (!$url->isExternal() && $url->isRouted() && $menu_link_item->isPublished()) {
$parameters = $url->getRouteParameters();
if (isset($parameters['node'])) {
$node = Node::load($parameters['node']);
if ($node instanceof NodeInterface) {
$menu_link_item->addCacheableDependency($node);
if ($account->hasPermission('administer menu')) {
return AccessResult::neutral()->addCacheableDependency($menu_link_item);
}
if (!$node->isPublished()) {
return AccessResult::forbidden()->addCacheableDependency($menu_link_item);
}
if ($node->hasField('moderation_state') && !$node->moderation_state->isEmpty()) {
if ($node->moderation_state->value !== 'published') {
return AccessResult::forbidden()->addCacheableDependency($menu_link_item);
}
}
}
return AccessResult::neutral()->addCacheableDependency($menu_link_item);
}
}
}
}
/**
* Implements hook_node_access_records().
*/
function tide_core_node_access_records(NodeInterface $node) {
// Only run if the module permission by terms is enabled.
if (\Drupal::moduleHandler()->moduleExists('permissions_by_term')) {
if (!$node->isPublished()) {
$grants[] = [
'realm' => 'tide_core',
'gid' => 1,
'grant_view' => 1,
'grant_update' => 0,
'grant_delete' => 0,
'nid' => $node->id(),
];
return $grants;
}
}
return [];
}
/**
* Implements hook_node_grants().
*/
function tide_core_node_grants(AccountInterface $account, $op) {
// Only run if the module permission by terms is enabled.
if (\Drupal::moduleHandler()->moduleExists('permissions_by_term')) {
if ($op === 'view') {
$view_unpublished_content_roles = array_keys(user_roles(TRUE, 'view any unpublished content'));
$account_roles = $account->getRoles();
if (!empty(array_intersect($account_roles, $view_unpublished_content_roles))) {
$grants['tide_core'][] = 1;
return $grants;
}
}
}
return [];
}
/**
* Implements hook_node_access().
*/
function tide_core_node_access(NodeInterface $node, $op, AccountInterface $account) {
// Only run if the module permission by terms is enabled.
if (\Drupal::moduleHandler()->moduleExists('permissions_by_term')) {
if (!$node->isPublished() && $op === 'view') {
$access_result = AccessResult::allowedIfHasPermission($account, 'view any unpublished content');
$access_result = $access_result->andIf(AccessResult::allowedIf($node->getOwnerId() == $account->id() || $node->getRevisionUserId() == $account->id()));
return $access_result->addCacheableDependency($node);
}
}
return AccessResult::neutral()->addCacheableDependency($node);
}