-
Notifications
You must be signed in to change notification settings - Fork 2
/
content_translation_access.module
134 lines (120 loc) · 6.38 KB
/
content_translation_access.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
<?php
/**
* @file
* Adds access hooks.
*/
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\content_translation\ContentTranslationManager;
use Drupal\Core\Form\FormStateInterface;
use Drupal\content_translation\BundleTranslationSettingsInterface;
/**
* Implements hook_entity_access() for entity edit/delete access.
*/
function content_translation_access_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\content_translation_access\AccessControlHandlerInterface $access_control_handler */
$access_control_handler = \Drupal::service('content_translation_access.access_control_handler');
return $access_control_handler->access($entity, $operation, $account);
}
/**
* Implements hook_entity_create_access() for entity create access.
*/
function content_translation_access_entity_create_access(AccountInterface $account, array $context, $entity_bundle) {
/** @var \Drupal\content_translation_access\AccessControlHandlerInterface $access_control_handler */
$access_control_handler = \Drupal::service('content_translation_access.access_control_handler');
if (isset($context['entity_type_id']) && isset($context['langcode'])) {
if ($context['langcode'] == 'x-default') {
return $access_control_handler->createAnyAccess($context['entity_type_id'], $entity_bundle, $account);
}
else {
$language = \Drupal::languageManager()->getLanguage($context['langcode']);
if ($language != NULL) {
return $access_control_handler->createAccess($context['entity_type_id'], $entity_bundle, $language, $account);
}
}
}
}
/**
* Implements hook_entity_translation_create_access() for create translation.
*/
function content_translation_access_entity_translation_create_access(EntityInterface $entity, AccountInterface $account, array $context) {
/** @var \Drupal\content_translation_access\AccessControlHandlerInterface $access_control_handler */
$access_control_handler = \Drupal::service('content_translation_access.access_control_handler');
$language = \Drupal::languageManager()->getLanguage($context['langcode']);
return $access_control_handler->createAccess($context['entity_type_id'], $entity->bundle(), $language, $account, $entity);
}
/**
* Implements hook_entity_translation_create_access() for create translation.
*/
function content_translation_access_entity_translation_access(EntityInterface $entity, $operation, AccountInterface $account) {
/** @var \Drupal\content_translation_access\AccessControlHandlerInterface $access_control_handler */
$access_control_handler = \Drupal::service('content_translation_access.access_control_handler');
return $access_control_handler->access($entity, $operation, $account);
}
/**
* Implements hook_entity_type_alter() for adding a entity validation constraint.
*/
function content_translation_access_entity_type_alter(array &$entity_types) {
// Provide defaults for translation info.
/* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
foreach ($entity_types as $entity_type) {
// @TODO: Find a better way to remove fields.
if ($entity_type->isTranslatable() && $entity_type->id() == 'node') {
$entity_type->setHandlerClass('translation', 'Drupal\content_translation_access\ContentTranslationAccessHandler');
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function content_translation_access_form_language_content_settings_form_alter(array &$form, FormStateInterface $form_state) {
/* Inject into the content language settings the translation settings if the
user has the required permission. */
if (!\Drupal::currentUser()->hasPermission('administer content translation')) {
return;
}
/** @var \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager */
$content_translation_manager = \Drupal::service('content_translation.manager');
$default = $form['entity_types']['#default_value'];
foreach ($default as $entity_type_id => $enabled) {
$default[$entity_type_id] = $enabled || $content_translation_manager->isEnabled($entity_type_id) ? $entity_type_id : FALSE;
}
$form['entity_types']['#default_value'] = $default;
$form['#attached']['library'][] = 'content_translation/drupal.content_translation.admin';
$entity_manager = Drupal::entityTypeManager();
$bundle_info_service = \Drupal::service('entity_type.bundle.info');
foreach ($form['#labels'] as $entity_type_id => $label) {
$entity_type = $entity_manager->getDefinition($entity_type_id);
$entity_type_translatable = $content_translation_manager->isSupported($entity_type_id);
foreach ($bundle_info_service->getBundleInfo($entity_type_id) as $bundle => $bundle_info) {
// Here we do not want the widget to be altered and hold also the "Enable
// translation" checkbox, which would be redundant. Hence we add this key
// to be able to skip alterations. Alter the title and display the message
// about UI integration.
$form['settings'][$entity_type_id][$bundle]['settings']['language']['#content_translation_skip_alter'] = TRUE;
if (!$entity_type_translatable) {
$form['settings'][$entity_type_id]['#title'] = t('@label (Translation is not supported).', ['@label' => $entity_type->getLabel()]);
continue;
}
// Displayed the "shared fields widgets" toggle.
if ($content_translation_manager instanceof BundleTranslationSettingsInterface) {
$settings = $content_translation_manager->getBundleTranslationSettings($entity_type_id, $bundle);
$force_hidden = ContentTranslationManager::isPendingRevisionSupportEnabled($entity_type_id, $bundle);
$form['settings'][$entity_type_id][$bundle]['settings']['content_translation']['untranslatable_fields_hide_with_permission'] = [
'#type' => 'checkbox',
'#title' => t('Hide non translatable fields on translation forms (with permission)'),
'#default_value' => $force_hidden || !empty($settings['untranslatable_fields_hide_with_permission']),
'#disabled' => $force_hidden,
'#description' => $force_hidden ? t('Hide non translatable fields in translation forms when missing permission.') : '',
'#states' => [
'visible' => [
':input[name="settings[' . $entity_type_id . '][' . $bundle . '][translatable]"]' => [
'checked' => TRUE,
],
],
],
];
}
}
}
}