-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraft_translation_helper.module
89 lines (81 loc) · 3.54 KB
/
draft_translation_helper.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
<?php
/**
* @file
* Contains draft_translation_helper.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;
/**
* Implements hook_help().
*/
function draft_translation_helper_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the draft_translation_helper module.
case 'help.page.draft_translation_helper':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Prevents users from translating content with an active draft') . '</p>';
return $output;
default:
}
}
function draft_translation_helper_default_translation_is_draft($front = false) {
$return = &drupal_static(__FUNCTION__);
if(is_null($return)){
$return = false;
$node = \Drupal::request()->attributes->get('node');
if(!empty($node) && !$node->isDefaultTranslation()){
// Determining if the original node has a draft in progress is determined differently in display vs. edit form.
$draft_in_progress = $front ? !$node->getUntranslated()->isLatestRevision() : !$node->getUntranslated()->isDefaultRevision();
$account = Drupal::currentUser();
$can_edit = $node->access('update', $account);
if ( $can_edit && $draft_in_progress ){
$language = $node->getUntranslated()->language();
$return = $language->getName();
}
}
}
return $return;
}
function draft_translation_helper_preprocess_block(&$variables) {
if ($variables['plugin_id'] == 'local_tasks_block') {
/*
* If the node is a translation, and the original node has a draft in
* progress, make the node edit buttons inactive and let the user know
* they will be back once the original node's draft is published or removed.
*/
if ($language = draft_translation_helper_default_translation_is_draft(TRUE)) {
$node = \Drupal::request()->attributes->get('node');
$message = t('There is a draft in progress for the @language version of this page.
Editing options are disabled until the @language draft is published or removed.
The @language draft-in-progress can be found <a href="/node/@id/latest">here</a>',
['@language' => $language, '@id' => $node->id()]);
$variables['attributes']['class'][] = 'local-actions-unavailable';
$variables['#attached']['library'][] = 'draft_translation_helper/draft_translation_helper_fe';
$variables['content']['#primary']['other'] = [
'#markup' => sprintf('<div data-alert="" class="alert-box radius alert">%s</div>', $message),
'#allowed_tags' => ['div', 'a'],
'#weight' => -50,
];
}
}
}
/**
* Implements hook_form_BASE_FORM_ID_alter().
*/
function draft_translation_helper_form_node_form_alter(&$form, FormStateInterface $form_state, $form_id) {
/*
* If the node is a translation, and the original node has a draft in progress,
* hide the form and inform the user they should not make edits until the
* original node's draft is published or removed
*/
if ($language = draft_translation_helper_default_translation_is_draft()) {
$node = \Drupal::request()->attributes->get('node');
$message = t('There is a draft in progress for the @language version of this page.
Editing options are disabled until the @language draft is published or removed.
The @language draft-in-progress can be found <a href="/node/@id/latest">here</a>',
['@language' => $language, '@id' => $node->id()]);
drupal_set_message($message, 'error');
$form['#attributes']['class'][] = 'hidden';
}
}