-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathding_language.module
More file actions
534 lines (450 loc) · 16.8 KB
/
ding_language.module
File metadata and controls
534 lines (450 loc) · 16.8 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
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
<?php
/**
* @file
* Multilanguage functionality for easySuite.
*/
include_once 'ding_language.features.inc';
/**
* Implements hook_menu().
*/
function ding_language_menu() {
$items = [];
$items['admin/config/ding/language'] = [
'title' => 'Ding Language',
'description' => 'Ding Language settings form.',
'page callback' => 'drupal_get_form',
'page arguments' => ['ding_language_settings_form'],
'file' => 'ding_language.admin.inc',
'access arguments' => ['access administration pages'],
'type' => MENU_NORMAL_ITEM,
];
return $items;
}
/**
* Implements hook_views_query_alter().
*/
function ding_language_views_query_alter(&$view, &$query) {
$enabled_views = views_get_enabled_views();
$content_modules = [
'ding_eresource',
'ding_event',
'ding_news',
'ding_library',
'ding_groups',
];
$processed_views = [];
foreach ($enabled_views as $module) {
if (in_array($module->name, $content_modules)) {
$display_names = array_keys($module->display);
$processed_views[] = $display_names;
}
}
$inlined = call_user_func_array('array_merge', $processed_views);
foreach ($inlined as $item) {
if ($view->current_display == $item) {
$query->where[1]['conditions'][] = [
'field' => 'node.language',
'operator' => '=',
'value' => [
'***CURRENT_LANGUAGE***' => '***CURRENT_LANGUAGE***',
],
];
}
}
// Show events on library events page, related to library and it's language.
if ($view->name == 'ding_event' && $view->current_display == 'ding_event_list_library') {
// Get library info.
$library_nid = reset($view->args);
// Get source id of library translation node.
$tnid = db_select('node', 'n')
->fields('n', ['tnid'])
->condition('type', 'ding_library')
->condition('nid', $library_nid)
->execute()
->fetchField();
// Loading node ids which acts as group id.
$gids = db_select('node', 'n')
->fields('n', ['nid'])
->condition('tnid', $tnid)
->execute()
->fetchAllKeyed(0, 0);
// Alter condition, so query fetches all nodes from group and it's translated
// instances.
$query->where[0]['conditions'][1]['value'] = $gids;
$query->where[0]['conditions'][1]['field'] = 'og_membership_node.gid';
$query->where[0]['conditions'][1]['operator'] = 'in';
}
// Events filter by library on events page.
if ($view->name == 'ding_event' && $view->current_display == 'ding_event_list') {
if (isset($view->exposed_input['og_group_ref_target_id_entityreference_filter'])) {
foreach ($view->exposed_input['og_group_ref_target_id_entityreference_filter'] as $checked_library) {
$tnid = $tnid = db_select('node', 'n')
->fields('n', ['tnid'])
->condition('type', 'ding_library')
->condition('nid', $checked_library)
->execute()
->fetchField();
$gids = db_select('node', 'n')
->fields('n', ['nid'])
->condition('tnid', $tnid)
->execute()
->fetchAllKeyed(0, 0);
$query_conditions = $query->where;
foreach ($query_conditions as $key => $conditions_group) {
foreach ($conditions_group['conditions'] as $pos => $condition) {
if ($condition['field'] == 'og_membership.gid') {
$query->where[$key]['conditions'][$pos]['value'][] = $gids;
}
}
}
}
}
}
}
/**
* Implements hook_search_api_views_query_alter().
*/
function ding_language_search_api_views_query_alter(view &$view, SearchApiViewsQuery &$query) {
global $language;
$idx_id = db_select('search_api_index', 'sai')
->fields('sai', ['id'])
->condition('machine_name', 'default_multiple_index', '=')
->execute()
->fetchField();
$index = search_api_index_load($idx_id);
$fields = array(
'options' => $index->options,
);
if (empty($fields['options']['fields']['node:language'])) {
$fields['options']['fields']['node:language'] = [
'type' => 'string',
];
search_api_index_edit($index->id, $fields);
try {
search_api_index_items($index);
}
catch (Exception $e) {
watchdog('error', $e->getMessage());
}
}
$query->where[1]['conditions'][0] = [
'0' => 'node:language',
'1' => $language->language,
'2' => '=',
];
}
/**
* Implements hook_query_TAG_alter().
*/
function ding_language_query_language_alter(QueryAlterableInterface $query) {
global $language;
$query->condition('n.language', $language->language, '=');
}
/**
* Implements hook_form_alter().
*/
function ding_language_form_alter(&$form, &$form_state, $form_id) {
// Filtering "Library" field of ding_event and ding_news node form.
if ($form_id == 'ding_event_node_form' || $form_id == 'ding_news_node_form') {
global $language;
$rels = $form['og_group_ref'][LANGUAGE_NONE]['#options'];
if (isset($rels['_none'])) {
unset($rels['_none']);
}
$nids = [];
foreach ($rels as $key => $rel) {
$nids[] = $key;
}
$nodes = node_load_multiple($nids);
$current_lang_rels = [];
foreach ($nodes as $node) {
if ($node->language == $language->language) {
$current_lang_rels[$node->nid] = $node->title;
}
}
$current_lang_rels = ['_none' => t('- None -')] + $current_lang_rels;
$form['og_group_ref'][LANGUAGE_NONE]['#options'] = $current_lang_rels;
}
}
/**
* Implements hook_post_features_revert().
*/
function ding_language_post_features_revert($component) {
// Enable detection of language from URL.
$negotiation = array(
'locale-url' => array(
'callbacks' => array(
'language' => 'locale_language_from_url',
'switcher' => 'locale_language_switcher_url',
'url_rewrite' => 'locale_language_url_rewrite_url',
),
'file' => 'includes/locale.inc',
),
'language-default' => array(
'callbacks' => array(
'language' => 'language_from_default',
),
),
);
variable_del('language_negotiation_language');
variable_set('language_negotiation_language', $negotiation);
// Set custom path to language icons.
variable_set('languageicons_path', 'https://storage.easyting.dk/icons/*.png');
// Enable multilanguage support for content types.
$content_types = node_type_get_types();
foreach ($content_types as $content_type) {
variable_set('language_content_type_' . $content_type->type, 2);
}
if (!module_exists('i18n_taxonomy')) {
module_enable(array('i18n_taxonomy'));
}
// Enable localization for taxonomy vocabularies.
$vocabularies = taxonomy_get_vocabularies();
foreach ($vocabularies as $vocabulary) {
db_update('taxonomy_vocabulary')
->fields(array(
'i18n_mode' => 1,
))
->execute();
i18n_string_object_update('taxonomy_vocabulary', $vocabulary);
}
variable_set('ding_language_random', rand());
}
/**
* Implements hook_preprocess_HOOK().
*/
function ding_language_preprocess_menu_block_wrapper(&$variables) {
if ($variables['config']['menu_name'] == 'menu-secondary-menu') {
if ($variables['config']['disabled'] == FALSE) {
// Fetching "Secondary menu" pane metadata.
$query = db_select('panels_pane', 'pp')
->fields('pp', ['pid', 'did'])
->condition('panel', 'navigation', '=')
->condition('subtype', 'menu_block-2', '=')
->execute()
->fetchAll();
$pane_metadata = reset($query);
// Loading "Navigation" display.
$display = panels_load_display($pane_metadata->did);
if (!empty($display)) {
$pane = $display->content[$pane_metadata->pid];
// Disabling "Secondary menu" block.
$pane->shown = 0;
// Load "Language switcher" pane.
$new_pane = panels_new_pane('block', 'locale-language', TRUE);
$new_pane->configuration = [
'override_title' => 1,
'override_title_text' => '',
'override_title_heading' => 'h2',
];
$display->add_pane($new_pane, 'navigation');
// Save whole display.
panels_save_display($display);
}
}
}
}
/**
* Implements hook_panels_pre_render().
*/
function ding_language_panels_pre_render($panels_display, $renderer) {
$context = $panels_display->context;
if (!empty($context['argument_term_1']) && $context['argument_term_1']->plugin == 'entity:taxonomy_term') {
if (!empty($context['argument_term_1']->data)) {
$term_tid = $context['argument_term_1']->data->tid;
$term = i18n_taxonomy_localize_terms(taxonomy_term_load($term_tid));
$panels_display->context['argument_term_1']->data = $term;
$panels_display->title = $term->name;
$trail = menu_get_active_trail();
$trail[1]['title'] = $term->name;
menu_set_active_trail($trail);
}
}
}
/**
* Implements hook_preprocess().
*/
function ding_language_preprocess(&$variables, $hook) {
switch ($hook) {
case strpos($hook, 'ding_nodelist.'):
$tid = $variables['item']->category['#items'][0]['tid'];
$localized_term = i18n_taxonomy_localize_terms(taxonomy_term_load($tid));
$variables['item']->category[0]['#markup'] = $localized_term->name;
break;
case 'views_view_fields':
if (!empty($variables['fields']['field_ding_eresource_category'])) {
$tid = reset($variables['view']->args);
$localized_term = i18n_taxonomy_localize_terms(taxonomy_term_load($tid));
$category_field = $variables['fields']['field_ding_eresource_category'];
$build_string = [
'#type' => 'markup',
'#markup' => "<{$category_field->element_type}>$localized_term->name</{$category_field->element_type}>"
];
$category_field->content = drupal_render($build_string);
$variables['fields']['field_ding_eresource_category'] = $category_field;
}
break;
case 'node':
$fields = ['field_ding_eresource_category', 'field_ding_eresource_access', 'field_ding_event_target', 'field_ding_news_category'];
foreach ($fields as $field) {
if (!empty($variables['content'][$field])) {
$tid = $variables[$field][0]['tid'];
$localized_term = i18n_taxonomy_localize_terms(taxonomy_term_load($tid));
$variables['content'][$field]['#items'][0]['taxonomy_term'] = $localized_term;
$variables['elements'][$field][0]['#markup'] = $localized_term->name;
if ($field == 'field_ding_news_category') {
$output = $localized_term->name;
}
else {
$output = l($localized_term->name, 'taxonomy/term/' . $tid);
}
$variables['content'][$field][0] = $output;
}
}
break;
case 'entity':
if ($variables['entity_type'] == 'profile2') {
$tid = $variables['content']['field_ding_staff_department']['#items'][0]['tid'];
$translated = i18n_taxonomy_localize_terms(taxonomy_term_load($tid));
$variables['content']['field_ding_staff_department'][0]['#title'] = $translated->name;
}
break;
}
}
/**
* Implements hook_field_attach_view_alter().
*/
function ding_language_field_attach_view_alter(&$output, $context) {
foreach (element_children($output) as $field_name) {
$element = &$output[$field_name];
if ($element['#field_type'] == 'taxonomy_term_reference' && $element['#formatter'] == 'taxonomy_term_reference_link') {
foreach ($element['#items'] as $delta => $item) {
$tid = $item['tid'];
$translated_term = i18n_taxonomy_localize_terms(taxonomy_term_load($tid));
$element[$delta]['#title'] = $translated_term->name;
}
}
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ding_language_form_customerror_admin_settings_alter(&$form, &$form_state, $form_id) {
$languages = language_list();
$group_404 = $form['customerror_404_group'];
$title = $group_404['customerror_404_title'];
$body = $group_404['customerror_404'];
$default_err_response = variable_get('customerror_404', []);
$group_404['default'] = [
'#type' => 'fieldset',
'#title' => t('Default'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$group_404['default']['customerror_404_title'] = $title;
$group_404['default']['customerror_404'] = $body;
foreach ($languages as $code => $lang) {
$group_404[$lang->language] = [
'#type' => 'fieldset',
'#title' => $lang->name,
'#description' => t('Customerror values for @language language. If empty, values from "Default" group are used.', ['@language' => $lang->name]),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
];
$group_404[$lang->language]['customerror_404_title_' . $code] = $title;
$group_404[$lang->language]['customerror_404_title_' . $code]['#title'] = $lang->name . ': ' . $title['#title'];
$group_404[$lang->language]['customerror_404_title_' . $code]['#default_value'] = !empty(variable_get('customerror_404_title_' . $code)) ? variable_get('customerror_404_title_' . $code) : variable_get('customerror_404_title');
$err_response = variable_get('customerror_404_' . $lang->language, []);
$group_404[$lang->language]['customerror_404_' . $code] = $body;
$group_404[$lang->language]['customerror_404_' . $code]['#title'] = $lang->name . ': ' . $body['#title'];
$group_404[$lang->language]['customerror_404_' . $code]['#default_value'] = !empty($err_response['value']) ? $err_response['value'] : $default_err_response['value'];
}
unset($group_404['customerror_404_title']);
unset($group_404['customerror_404']);
$form['customerror_404_group'] = $group_404;
}
/**
* Implements hook_page_build().
*/
function ding_language_page_build(&$page) {
global $language;
$current_path = current_path();
if ($current_path != 'customerror/404') {
return;
}
$path = drupal_get_normal_path(variable_get('site_404', ''));
if ($path == $current_path) {
$default_customerror_title = variable_get('customerror_404_title', '');
$page_title = variable_get('customerror_404_title_' . $language->language, $default_customerror_title);
drupal_set_title($page_title);
$default_customerror_body = variable_get('customerror_404', []);
$page_content = variable_get('customerror_404_' . $language->language, $default_customerror_body);
$return = $page_content['value'];
drupal_set_page_content($return);
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function ding_language_form_i18n_string_locale_translate_edit_form_alter(&$form, &$form_state, $form_id) {
$groups = variable_get('ding_language_text_groups', []);
$groups = array_filter($groups);
if (!empty($groups) && in_array($form['textgroup']['#value'], $groups)) {
$lid = $form["lid"]["#value"];
$source = db_query('SELECT source, context, textgroup, location FROM {locales_source} WHERE lid = :lid', [':lid' => $lid])->fetchObject();
// Include default form controls with empty values for all languages.
// This ensures that the languages are always in the same order in forms.
$languages = language_list();
$form['translations'] = ['#tree' => TRUE];
// Approximate the number of rows to use in the default textarea.
$rows = min(ceil(str_word_count($source->source) / 12), 10);
foreach ($languages as $langcode => $language) {
$form['translations'][$langcode] = [
'#type' => 'textarea',
'#title' => t($language->name),
'#rows' => $rows,
'#default_value' => '',
];
}
// Fetch translations and fill in default values in the form.
$result = db_query("SELECT DISTINCT translation, language FROM {locales_target} WHERE lid = :lid", [':lid' => $lid]);
foreach ($result as $translation) {
$form['translations'][$translation->language]['#default_value'] = $translation->translation;
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function ding_language_preprocess_eu_cookie_compliance_popup_info(&$variables) {
global $language;
if (empty($variables['agree_button'])) {
$enabled_languages = language_list();
// Filter enabled languages to find the non-core one.
$non_default_langs = array_diff(array_keys($enabled_languages), [
'da',
'en'
]);
// Use default translatable `en` value for agree button when current language
// don't match the core ones.
if (in_array($language->language, $non_default_langs)) {
$variables['agree_button'] = t('OK, I agree');
}
}
}
/**
* Implements hook_preprocess_HOOK().
*/
function ding_language_preprocess_panels_pane(&$variables) {
if ($variables['pane']->subtype === 'block-1') {
global $language;
$module = 'block';
$delta = 1;
$block = block_load($module, $delta);
$lang = i18n_language_object($language);
$langcode = $lang ? $lang->language : NULL;
$object = i18n_object('block', $block);
$strings = $object->get_strings(array('empty' => TRUE));
$variables['title'] = $strings['blocks:block:1:title']->translations[$langcode] ?? $block->title;
}
}