-
Notifications
You must be signed in to change notification settings - Fork 3
/
oe_translation.module
402 lines (361 loc) · 12.3 KB
/
oe_translation.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
<?php
/**
* @file
* OpenEuropa Translation module file.
*/
declare(strict_types=1);
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Breadcrumb\Breadcrumb;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Link;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\Core\Url;
use Drupal\oe_translation\Entity\TranslationRequestInterface;
use Drupal\oe_translation\Entity\TranslationRequestLogInterface;
use Drupal\oe_translation\TranslationModerationHandler;
use Drupal\oe_translation\TranslationSourceFieldProcessor\AddressFieldProcessor;
use Drupal\oe_translation\TranslationSourceFieldProcessor\DefaultFieldProcessor;
use Drupal\oe_translation\TranslationSourceFieldProcessor\DescriptionListFieldProcessor;
use Drupal\oe_translation\TranslationSourceFieldProcessor\MetatagsFieldProcessor;
use Drupal\oe_translation\TranslationSourceFieldProcessor\PathFieldProcessor;
/**
* Implements hook_theme().
*/
function oe_translation_theme($existing, $type, $theme, $path) {
return [
'local_translation_form_element_group' => [
'render element' => 'element',
],
'tooltip' => [
'variables' => [
'text' => NULL,
'label' => NULL,
],
],
];
}
/**
* Implements hook_entity_type_alter().
*/
function oe_translation_entity_type_alter(array &$entity_types) {
foreach ($entity_types as $entity_type_id => $entity_type) {
// Set translator providers.
if ($entity_type_id === 'node') {
$translators = [
'local' => TRUE,
'remote' => [
'epoetry',
],
];
$entity_type->set('oe_translation_translators', $translators);
}
// Change the moderation handler if set to our own override.
if (!$entity_type->hasHandlerClass('moderation')) {
continue;
}
$entity_type->setHandlerClass('moderation', TranslationModerationHandler::class);
}
}
/**
* Implements template_preprocess_node().
*/
function oe_translation_preprocess_node(&$variables) {
// Ensure that on the preview page, we treat the node as the full page
// display.
if (\Drupal::service('current_route_match')->getRouteName() !== 'entity.oe_translation_request.preview') {
return;
}
$variables['page'] = TRUE;
}
/**
* Implements hook_entity_base_field_info_alter().
*/
function oe_translation_entity_base_field_info_alter(&$fields, EntityTypeInterface $entity_type) {
// We don't want different translations to have different moderation states
// so we make the moderation state field untranslatable.
if ($entity_type->id() === 'content_moderation_state') {
$fields['moderation_state']->setTranslatable(FALSE);
}
}
/**
* Implements hook_entity_access().
*/
function oe_translation_entity_access(EntityInterface $entity, $operation, AccountInterface $account) {
if (!$entity instanceof ContentEntityInterface) {
return AccessResult::neutral();
}
if (!in_array($operation, ['update'])) {
// @todo , see if we should allow the deletion of translations.
return AccessResult::neutral();
}
/** @var \Drupal\oe_translation\TranslatorProvidersInterface $translator_providers */
$translator_providers = \Drupal::service('oe_translation.translator_providers');
if (!$translator_providers->hasTranslators($entity->getEntityType())) {
return AccessResult::neutral();
}
// We do not want translations to be manually edited if they are supported
// by our translation system and the local translation is disabled.
if (!$entity->isDefaultTranslation() && $translator_providers->hasLocal($entity->getEntityType())) {
return AccessResult::forbidden()->addCacheableDependency($entity);
}
return AccessResult::neutral();
}
/**
* Implements hook_field_info_alter().
*/
function oe_translation_field_info_alter(&$info) {
if (isset($info['metatag'])) {
$info['metatag']['oe_translation_source_field_processor'] = MetatagsFieldProcessor::class;
}
if (isset($info['path'])) {
$info['path']['oe_translation_source_field_processor'] = PathFieldProcessor::class;
}
if (isset($info['address'])) {
$info['address']['oe_translation_source_field_processor'] = AddressFieldProcessor::class;
}
if (isset($info['description_list_field'])) {
$info['description_list_field']['oe_translation_source_field_processor'] = DescriptionListFieldProcessor::class;
}
if (isset($info['typed_link'])) {
$info['typed_link']['column_groups'] = [
'uri' => [
'label' => t('URI'),
'translatable' => TRUE,
],
'title' => [
'label' => t('Title'),
'translatable' => TRUE,
],
'options' => [
'label' => t('Options'),
'translatable' => FALSE,
],
'link_type' => [
'label' => t('Link type'),
'translatable' => TRUE,
],
];
}
// Set a default processor class for all fields that do not have one yet.
foreach ($info as &$field_type) {
if (!isset($field_type['oe_translation_source_field_processor'])) {
$field_type['oe_translation_source_field_processor'] = DefaultFieldProcessor::class;
}
}
}
/**
* Implements template_preprocess_local_translation_form_element_group().
*
* Template responsible for rendering the local translation form elements.
*/
function oe_translation_preprocess_local_translation_form_element_group(&$variables) {
$element = $variables['element'];
$table = [
'#type' => 'table',
'#attributes' => [
'id' => $element['#ajaxid'],
],
'#header' => [
[
'data' => implode(' / ', $element['#parent_label']),
'colspan' => 2,
],
],
'#rows' => [],
];
$rows = [];
$rows[] = [
[
'data' => $element['source'],
],
[
'data' => $element['translation'],
],
];
$table['#rows'] = $rows;
$variables['element'] = $table;
}
/**
* Implements hook_block_view_BASE_BLOCK_ID_alter() for the Page Header block.
*/
function oe_translation_block_view_oe_theme_helper_page_header_alter(array &$build, $block) {
$build['#pre_render'][] = function (array $build) {
// This block is shipped by OpenEuropa Theme and we use this to remove the
// language switcher from the page header on the entity preview page.
if (\Drupal::service('current_route_match')->getRouteName() !== 'entity.oe_translation_request.preview') {
return $build;
}
if (isset($build['content']['#language_switcher'])) {
unset($build['content']['#language_switcher']);
}
return $build;
};
}
/**
* Implements hook_system_breadcrumb_alter().
*/
function oe_translation_system_breadcrumb_alter(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context) {
// Prepare some basic breadcrumb links for the translation request entities.
if ($route_match->getRouteName() === 'entity.oe_translation_request.canonical') {
_oe_translation_default_breadcrumb_links($breadcrumb, $route_match, $context);
}
}
/**
* Prepares base breadcrumb links for translation pages.
*
* @param \Drupal\Core\Breadcrumb\Breadcrumb $breadcrumb
* The breadcrumb.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param array $context
* The context.
*/
function _oe_translation_default_breadcrumb_links(Breadcrumb &$breadcrumb, RouteMatchInterface $route_match, array $context): void {
$links = [];
$links[] = Link::fromTextAndUrl(t('Home'), Url::fromRoute('<front>'));
/** @var \Drupal\oe_translation\Entity\TranslationRequestInterface $translation_request */
$translation_request = $route_match->getParameter('oe_translation_request');
$entity = $translation_request->getContentEntity();
$links[] = $entity->toLink();
$links[] = Link::fromTextAndUrl(t('Translate'), $entity->toUrl('drupal:content-translation-overview'));
$cache = CacheableMetadata::createFromObject($breadcrumb);
$cache->addCacheContexts(['route']);
$breadcrumb = new Breadcrumb();
$breadcrumb->addCacheableDependency($cache);
$breadcrumb->setLinks($links);
}
/**
* Creates a table of log messages for the request.
*
* @param \Drupal\oe_translation\Entity\TranslationRequestInterface $request
* The request.
*
* @return array
* The log messages.
*/
function _oe_translation_create_request_logs_tables(TranslationRequestInterface $request): array {
$logs = $request->getLogMessages();
if (!$logs) {
return [];
}
$table = [
'#type' => 'table',
'#attributes' => [
'class' => ['translation-request-log-messages'],
],
'#header' => [
'#',
t('Type'),
t('Message'),
t('User'),
t('Date'),
],
];
$rows = [];
$i = 1;
foreach ($logs as $log) {
$class_map = [
TranslationRequestLogInterface::ERROR => 'color-error',
TranslationRequestLogInterface::WARNING => 'color-warning',
];
$class = $class_map[$log->getType()] ?? NULL;
$row = [
$i,
ucfirst($log->getType()),
$log->getMessage(),
[
'data' => [
'#theme' => 'username',
'#account' => $log->getOwner(),
'#cache' => [
'tags' => $log->getOwner()->getCacheTags(),
],
],
],
\Drupal::service('date.formatter')->format($log->getCreatedTime(), 'short'),
];
$rows[] = [
'data' => $row,
'class' => $class ? [$class] : [],
];
$i++;
}
$table['#rows'] = $rows;
return $table;
}
/**
* Implements hook_entity_operation_alter().
*/
function oe_translation_entity_operation_alter(array &$operations, EntityInterface $entity) {
if (!$entity instanceof TranslationRequestInterface) {
return;
}
if (isset($operations['edit'])) {
// Remove the edit operation because for the moment we won't edit requests.
unset($operations['edit']);
}
// Add the extra operations we are defining with our subscriber.
foreach ($entity->getOperationsLinks()['#links'] as $op => $link) {
if (!isset($operations[$op])) {
$operations[$op] = $link;
}
}
}
/**
* Implements template_preprocess_tooltip().
*/
function oe_translation_preprocess_tooltip(array &$variables) {
$variables['#attached']['library'][] = 'oe_translation/tooltip';
}
/**
* Implements hook_entity_base_field_info().
*
* We are keeping the 2 tmgmt_job and tmgmt_job_item base fields in place until
* the 2.x version of the translations system is deployed.
*/
function oe_translation_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() === 'tmgmt_job_item') {
$fields = [];
// Add a field to the Job item to store the entity revision at the moment of
// creation.
$fields['item_rid'] = BaseFieldDefinition::create('integer')
->setLabel(new TranslatableMarkup('Item revision ID'))
->setSetting('unsigned', TRUE);
// Add a field to the Job item to store the entity bundle being translated.
// This is needed to be able to inspect field related info if the entity
// itself gets deleted.
$fields['item_bundle'] = BaseFieldDefinition::create('string')
->setLabel(new TranslatableMarkup('Item bundle'));
return $fields;
}
if ($entity_type->id() === 'tmgmt_job') {
$fields = [];
$fields['poetry_request_id'] = BaseFieldDefinition::create('poetry_request_id')
->setLabel(t('The Poetry request ID'))
->setDisplayOptions('view', [
'label' => 'hidden',
'type' => 'poetry_request_id_formatter',
'weight' => 20,
]);
$fields['poetry_state'] = BaseFieldDefinition::create('list_string')
->setLabel(t('Poetry state'))
->setDescription(t('The poetry job state while the job is active.'))
->setSetting('allowed_values', [
'ongoing' => t('Ongoing'),
'translated' => t('Translated'),
]);
$fields['poetry_request_date'] = BaseFieldDefinition::create('datetime')
->setLabel(t('The Poetry request date'))
->setDescription(t('The date when the translations are expected at the moment of the request.'));
$fields['poetry_request_date_updated'] = BaseFieldDefinition::create('datetime')
->setLabel(t('The Poetry updated request date'))
->setDescription(t('The updated date when the translations are expected. Poetry can choose to update the request with a new expected delivery date.'));
return $fields;
}
}