-
Notifications
You must be signed in to change notification settings - Fork 1
/
globallink.module
1463 lines (1230 loc) · 45.2 KB
/
globallink.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
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
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* @file
* GlobalLink translation module.
*
* This module adds node translation using the Internationalization module (i18n). Also, includes GlobalLink core settings UI and configuration.
*/
define('TPT_ROOT', dirname(__FILE__));
define('TPT_ROLE_MANAGE_TRANSLATIONS', t('can manage GlobalLink translation tasks and settings'));
define('TPT_ROLE_DO_TRANSLATIONS', t('can manage GlobalLink translation tasks'));
define('TPT_ROLE_VIEW_TRANSLATIONS', t('can view GlobalLink translation tasks'));
define('TPT_PAGER_LIMIT', variable_get('globallink_pager_limit', 10));
define('TPT_LOGGING_CONFIG_DISABLED', '0');
define('TPT_LOGGING_CONFIG_INFO', '1');
define('TPT_LOGGING_CONFIG_DEBUG', '2');
/**
* Implements hook_permission().
*/
function globallink_permission() {
return array(
TPT_ROLE_MANAGE_TRANSLATIONS => array('title' => TPT_ROLE_MANAGE_TRANSLATIONS),
TPT_ROLE_DO_TRANSLATIONS => array('title' => TPT_ROLE_DO_TRANSLATIONS),
TPT_ROLE_VIEW_TRANSLATIONS => array('title' => TPT_ROLE_VIEW_TRANSLATIONS)
);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function globallink_form_field_ui_field_delete_form_alter(&$form, &$form_state) {
$field_name = $form['field_name']['#value'];
$field_info = field_info_field($field_name);
switch ($field_info['type']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
default:
array_unshift($form['#submit'], 'globallink_form_field_ui_field_delete_form_submit');
}
}
/**
* Adds a validation handler to check for change in multilingual options.
*/
function globallink_form_node_type_form_alter(&$form, $form_state) {
array_unshift($form['#validate'], 'globallink_form_node_type_form_validate');
}
/**
* Validation to check if any active submission exists for this content type.
* Only if there is a change in multilingual options.
*/
function globallink_form_node_type_form_validate($form, &$form_state) {
module_load_include('inc', 'globallink', 'globallink');
$language_content_type = $form_state['values']['language_content_type'];
if ($language_content_type != 2 && globallink_pending_submission_exists_for_content_type($form_state['values']['old_type'])) {
form_set_error('language_content_type', t('Active submission exists for this content type in GlobalLink.'));
}
}
/**
* Submit handler for delete link on Manage Fields Page of Content Type and Field Collection.
* This deletes a field from fields config table.
*/
function globallink_form_field_ui_field_delete_form_submit($form, &$form_state) {
$entity_type = isset($form['entity_type']['#value']) ? $form['entity_type']['#value'] : FALSE;
$bundle_name = isset($form['bundle']['#value']) ? $form['bundle']['#value'] : FALSE;
$field_name = isset($form['field_name']['#value']) ? $form['field_name']['#value'] : FALSE;
if (!$entity_type) {
return;
}
switch ($entity_type) {
case 'node':
// Request coming from Manage Fields of Content Type
$field_info = field_info_field($field_name);
switch ($field_info['type']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
case 'field_collection':
// field-collection being deleted, delete all fields recursively for this content type only
globallink_delete_fc($bundle_name, $field_name, $entity_type, $bundle_name);
default:
// Regular field, just delete from the config
db_delete('globallink_field_config')
->condition('content_type', $bundle_name, ' = ')
->condition('entity_type', $entity_type, ' = ')
->condition('bundle', $bundle_name, ' = ')
->condition('field_name', $field_name, ' = ')
->execute();
}
break;
case 'field_collection_item':
// Request coming from Manage Fields of Field Collections
// Entity type for all the fields here will be field_collection_item
$field_info = field_info_field($field_name);
switch ($field_info['type']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
case 'field_collection':
// Field-collection being deleted, delete all fields recursively for all the content types
$content_types = globallink_get_all_content_types_for_field($bundle_name, 'field_collection');
foreach ($content_types as $content_type) {
globallink_delete_fc($content_type, $field_name, $entity_type, $bundle_name);
}
break;
default:
// Regular field-collection field, just delete from the config for all content types
$content_types = globallink_get_all_content_types_for_field($bundle_name, 'field_collection');
foreach ($content_types as $content_type) {
db_delete('globallink_field_config')
->condition('content_type', $content_type, ' = ')
->condition('entity_type', $entity_type, ' = ')
->condition('bundle', $bundle_name, ' = ')
->condition('field_name', $field_name, ' = ')
->execute();
}
}
break;
}
}
/**
* Determines whether or not the translation is supported.
*
* @param string $type
* The type of translation.
*
* @return bool
* True if the translation is supported.
*/
function globallink_translation_supported($type) {
if (translation_supported_type($type) || (module_exists('globallink_entity') && entity_translation_node_supported_type($type))) {
return TRUE;
}
return FALSE;
}
/**
* Removes an existing field-collection field from field config.
*
* @param string $content_type
* The node content type.
* @param string $fc_name
* The field-collection field name.
* @param string $entity_type
* The field entity type.
* @param string $bundle
* The field bundle name.
*/
function globallink_delete_fc($content_type, $fc_name, $entity_type, $bundle) {
db_delete('globallink_field_config')
->condition('content_type', $content_type, ' = ')
->condition('entity_type', $entity_type, ' = ')
->condition('bundle', $bundle, ' = ')
->condition('field_name', $fc_name, ' = ')
->execute();
$fc_field_infos = field_info_instances('field_collection_item');
if (isset($fc_field_infos) && isset($fc_field_infos[$fc_name]) && is_array($fc_field_infos[$fc_name])) {
$fc_items = array_keys($fc_field_infos[$fc_name]);
foreach ($fc_items as $fc_item) {
// All field collection items have entity type as field_collection_item
globallink_delete_fc_items($content_type, $fc_name, $fc_item, 'field_collection_item');
}
}
}
/**
* Removes an existing child field of field-collection from field config.
*
* @param string $content_type
* The node content type.
* @param string $parent_field_name
* The field-collection parent name.
* @param string $field_name
* The field name.
* @param string $entity_type
* The field entity type.
*/
function globallink_delete_fc_items($content_type, $parent_field_name, $field_name, $entity_type) {
$fc_field_info = field_info_field($field_name);
switch ($fc_field_info['type']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
case 'field_collection':
// First delete the field-collection field
db_delete('globallink_field_config')
->condition('content_type', $content_type, ' = ')
->condition('entity_type', $entity_type, ' = ')
->condition('bundle', $parent_field_name, ' = ')
->condition('field_name', $field_name, ' = ')
->execute();
$fc_field_infos = field_info_instances('field_collection_item');
if (isset($fc_field_infos) && isset($fc_field_infos[$field_name]) && is_array($fc_field_infos[$field_name])) {
$fc_items = array_keys($fc_field_infos[$field_name]);
foreach ($fc_items as $fc_item) {
// Delete all child fields recursively
globallink_delete_fc_items($content_type, $field_name, $fc_item, $entity_type);
}
}
break;
default:
// Regular field-collection field, just delete from config
db_delete('globallink_field_config')
->condition('content_type', $content_type, ' = ')
->condition('entity_type', $entity_type, ' = ')
->condition('bundle', $parent_field_name, ' = ')
->condition('field_name', $field_name, ' = ')
->execute();
}
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function globallink_form_field_ui_field_overview_form_alter(&$form, &$form_state) {
$form['#submit'][] = 'globallink_form_field_ui_field_overview_form_submit';
}
/**
* Submit handler for Manage Fields Page of Content Type and Field Collection.
* Adds a new field being added to the fields config table for translation.
*/
function globallink_form_field_ui_field_overview_form_submit(&$form, &$form_state) {
$entity_type = isset($form['#entity_type']) ? $form['#entity_type'] : FALSE;
$bundle = isset($form['#bundle']) ? $form['#bundle'] : FALSE;
$new_field = isset($form['fields']['_add_new_field']) ? $form['fields']['_add_new_field'] : FALSE;
$existing_field = isset($form['fields']['_add_existing_field']) ? $form['fields']['_add_existing_field'] : FALSE;
if (!$entity_type) {
return;
}
switch ($entity_type) {
case 'node':
if (!globallink_translation_supported($bundle)) {
break;
}
// Request coming from Manage Fields of Content Type
if ($new_field && isset($new_field['type']['#value'])) {
switch ($new_field['type']['#value']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
case 'field_collection':
// New field-collection added, just add the empty field-collection to config
$new_field_name = $new_field['field_name']['#value'];
if ($new_field_name != '') {
db_insert('globallink_field_config')
->fields(array(
'content_type' => $bundle,
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => 'field_' . $new_field_name,
'field_type' => $new_field['type']['#value'],
'field_label' => $new_field['label']['#value'],
'translatable' => 1,
))->execute();
}
break;
default:
// Regular New field, just add to config
$new_field_name = $new_field['field_name']['#value'];
if ($new_field_name != '') {
db_insert('globallink_field_config')
->fields(array(
'content_type' => $bundle,
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => 'field_' . $new_field_name,
'field_type' => $new_field['type']['#value'],
'field_label' => $new_field['label']['#value'],
'translatable' => 1,
))->execute();
}
}
}
if (!$existing_field || !isset($existing_field['field_name']['#value'])) {
break;
}
// Existing field is being added to this content type
$existing_field_name = $existing_field['field_name']['#value'];
if ($existing_field_name == '') {
break;
}
$existing_field_info = field_info_field($existing_field_name);
switch ($existing_field_info['type']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
case 'field_collection':
// Existing FC being added to this content type
// Add FC and all the FC fields recursively to the config
globallink_insert_fc($entity_type, $existing_field_name, $bundle, $bundle);
break;
default:
// Regular existing field, just add to config
$existing_field_instance = field_info_instance($entity_type, $existing_field_name, $bundle);
db_insert('globallink_field_config')
->fields(array(
'content_type' => $bundle,
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => $existing_field_name,
'field_type' => $existing_field_info['type'],
'field_label' => $existing_field_instance['label'],
'translatable' => 1,
))->execute();
break;
}
break;
case 'field_collection_item':
// Request coming from Manage Fields of Field Collections
// Entity type for all the fields here will be field_collection_item
$fc_name = $bundle;
if ($new_field && isset($new_field['type']['#value'])) {
// A new field is being added to this field-collection
switch ($new_field['type']['#value']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
default:
// Regular new field is being added to this field-collection, just add to config
$new_field_name = $new_field['field_name']['#value'];
if ($new_field_name == '') {
break;
}
// First get all the content types for this field-collection from config,
// then add this field for all the content types in the config
$content_types = globallink_get_all_content_types_for_field($fc_name, 'field_collection');
foreach ($content_types as $content_type) {
db_insert('globallink_field_config')
->fields(array(
'content_type' => $content_type,
'entity_type' => $entity_type,
'bundle' => $fc_name,
'field_name' => 'field_' . $new_field_name,
'field_type' => $new_field['type']['#value'],
'field_label' => $new_field['label']['#value'],
'translatable' => 1
))->execute();
}
}
}
if (!$existing_field || !isset($existing_field['field_name']['#value'])) {
break;
}
// Existing field is being added to this field-collection
$existing_field_name = $existing_field['field_name']['#value'];
if ($existing_field_name == '') {
break;
}
$existing_field_info = field_info_field($existing_field_name);
switch ($existing_field_info['type']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
case 'field_collection':
// If existing field-collection is added to the field-collection, then add fields recursively
$content_types = globallink_get_all_content_types_for_field($fc_name, 'field_collection');
// First get all the content types for this field-collection from config
// then add this field for all the content types in the config
foreach ($content_types as $content_type) {
globallink_insert_fc($entity_type, $existing_field_name, $content_type, $fc_name);
}
break;
default:
// First get all the content types for this field-collection from config
// then add this field for all the content types in the config
$content_types = globallink_get_all_content_types_for_field($fc_name, 'field_collection');
foreach ($content_types as $content_type) {
$existing_field_instance = field_info_instance($entity_type, $existing_field_name, $bundle);
db_insert('globallink_field_config')
->fields(array(
'content_type' => $content_type,
'entity_type' => $entity_type,
'bundle' => $fc_name,
'field_name' => $existing_field_name,
'field_type' => $existing_field_info['type'],
'field_label' => $existing_field_instance['label'],
'translatable' => 1,
))->execute();
}
}
break;
}
}
/**
* Adds field-collection field to field config.
*
* @param string $entity_type
* The field entity type.
* @param string $fc_name
* The field-collection name.
* @param string $content_type
* The node content type.
* @param string $bundle
* The field bundle name.
*/
function globallink_insert_fc($entity_type, $fc_name, $content_type, $bundle) {
$fc_field_info = field_info_field($fc_name);
$fc_field_instance = field_info_instance($entity_type, $fc_name, $bundle);
// Add FC field to the config
db_insert('globallink_field_config')
->fields(array(
'content_type' => $content_type,
'entity_type' => $entity_type,
'bundle' => $bundle,
'field_name' => $fc_name,
'field_type' => $fc_field_info['type'],
'field_label' => $fc_field_instance['label'],
'translatable' => 0
))->execute();
$fc_field_infos = field_info_instances('field_collection_item');
if (isset($fc_field_infos) && isset($fc_field_infos[$fc_name]) && is_array($fc_field_infos[$fc_name])) {
$fc_items = array_keys($fc_field_infos[$fc_name]);
foreach ($fc_items as $fc_item) {
// Add all the FC field items recursively
globallink_insert_fc_items($content_type, $fc_name, $fc_item);
}
}
}
/**
* Adds child field of field-collection to field config.
*
* @param string $content_type
* The node content type.
* @param string $parent_field_name
* The parent field-collection name.
* @param string $field_name
* The field name.
*/
function globallink_insert_fc_items($content_type, $parent_field_name, $field_name) {
// Everything added here will have entity type as field_collection_item
$fc_field_info = field_info_field($field_name);
$fc_field_instance = field_info_instance('field_collection_item', $field_name, $parent_field_name);
switch ($fc_field_info['type']) {
case 'image':
case 'file':
case 'taxonomy_term_reference':
break;
case 'field_collection':
// Field-collection in a field-collection, first add this field-collection to the config
db_insert('globallink_field_config')
->fields(array(
'content_type' => $content_type,
'entity_type' => 'field_collection_item',
'bundle' => $parent_field_name,
'field_name' => $field_name,
'field_type' => $fc_field_info['type'],
'field_label' => $fc_field_instance['label'],
'translatable' => 0,
))->execute();
$fc_field_infos = field_info_instances('field_collection_item');
if (isset($fc_field_infos) && isset($fc_field_infos[$field_name]) && is_array($fc_field_infos[$field_name])) {
$fc_items = array_keys($fc_field_infos[$field_name]);
foreach ($fc_items as $fc_item) {
// Now add the child fields for this field-collection recursively
globallink_insert_fc_items($content_type, $field_name, $fc_item);
}
}
break;
default:
$translatable = 1;
db_insert('globallink_field_config')
->fields(array(
'content_type' => $content_type,
'entity_type' => 'field_collection_item',
'bundle' => $parent_field_name,
'field_name' => $field_name,
'field_type' => $fc_field_info['type'],
'field_label' => $fc_field_instance['label'],
'translatable' => $translatable,
))->execute();
}
}
/**
* Gets all content types for a specified field.
*
* @param string $field_name
* The name of the field to search.
* @param string $type
* The field's type.
*
* @return array
* An array containing all content types for the specified field.
*/
function globallink_get_all_content_types_for_field($field_name, $type) {
$array = array();
$result = db_select('globallink_field_config', 'tf')
->fields('tf', array('content_type'))
->distinct()
->condition('field_name', $field_name, '=')
->condition('field_type', $type, '=')
->execute();
foreach ($result as $row) {
$array[] = $row->content_type;
}
return $array;
}
/**
* Implements hook_node_type_insert().
*/
function globallink_node_type_insert($info) {
if (!globallink_translation_supported($info->type)) {
return;
}
db_insert('globallink_field_config')
->fields(array(
'content_type' => $info->type,
'entity_type' => 'node',
'bundle' => $info->type,
'field_name' => 'title',
'field_type' => 'text',
'field_label' => 'Title',
'translatable' => 1,
))->execute();
db_insert('globallink_field_config')
->fields(array(
'content_type' => $info->type,
'entity_type' => 'node',
'bundle' => $info->type,
'field_name' => 'body',
'field_type' => 'text_with_summary',
'field_label' => 'Body',
'translatable' => 1,
))->execute();
$metatag_module_exists = module_exists("metatag");
if ($metatag_module_exists) {
db_insert('globallink_field_config')
->fields(array(
'content_type' => $info->type,
'entity_type' => 'node',
'bundle' => $info->type,
'field_name' => 'metatags',
'field_type' => 'text',
'field_label' => 'Meta tags',
'translatable' => 1,
))->execute();
}
}
/**
* Implements hook_node_type_update().
*/
function globallink_node_type_update($info) {
if (!$info->modified) {
return;
}
if (count(globallink_get_translatable_fields($info->old_type)) == 0) {
if (globallink_translation_supported($info->type)) {
globallink_insert_all_fields($info->type);
}
return;
}
if (!globallink_translation_supported($info->type)) {
globallink_delete_field_configs($info->type);
return;
}
db_update('globallink_field_config')
->fields(array('content_type' => $info->orig_type))
->condition('content_type', $info->old_type, '=')
->execute();
db_update('globallink_field_config')
->fields(array('bundle' => $info->orig_type))
->condition('bundle', $info->old_type, '=')
->execute();
db_update('globallink_core')
->fields(array('type' => $info->orig_type))
->condition('type', $info->old_type, '=')
->execute();
}
/**
* Adds all fields of content type to field config.
*
* @param string $content_type
* The node content type.
*/
function globallink_insert_all_fields($content_type) {
$p_arr = globallink_get_pending_fields($content_type);
$f_keys = array_keys($p_arr);
foreach ($f_keys as $f_key) {
if ($f_key == '[all]') {
continue;
}
if ($f_key != 'title' && $f_key != 'metatags') {
$field = field_info_field($f_key);
switch ($field['type']) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
case 'field_collection':
break;
default:
db_insert('globallink_field_config')
->fields(array(
'content_type' => $content_type,
'entity_type' => 'node',
'bundle' => $content_type,
'field_name' => $f_key,
'field_type' => $field['type'],
'field_label' => $p_arr[$f_key],
'translatable' => 1,
))->execute();
}
}
else {
db_insert('globallink_field_config')
->fields(array(
'content_type' => $content_type,
'entity_type' => 'node',
'bundle' => $content_type,
'field_name' => $f_key,
'field_type' => 'text',
'field_label' => $p_arr[$f_key],
'translatable' => 1,
))->execute();
}
}
if (module_exists('field_collection')) {
globallink_insert_fc_fields($content_type);
}
}
/**
* Removes all fields for content type from field config.
*
* @param string $type
* The node content type.
*/
function globallink_delete_field_configs($type) {
db_delete('globallink_field_config')
->condition('content_type', $type, ' = ')
->execute();
}
/**
* Implements hook_node_type_delete().
*/
function globallink_node_type_delete($info) {
globallink_delete_field_configs($info->type);
}
/**
* Implements hook_form_FORM_ID_alter().
*/
function globallink_form_field_ui_field_edit_form_alter(&$form, $form_state) {
module_load_include('inc', 'globallink', 'globallink');
$field_type = $form['#field']['type'];
switch ($field_type) {
case 'list_boolean':
case 'image':
case 'file':
case 'taxonomy_term_reference':
case 'field_collection':
break;
default:
$field_name = $form['#field']['field_name'];
$entity_type = $form['#instance']['entity_type'];
$bundle_name = $form['#instance']['bundle'];
if ($entity_type == 'field_collection_item') {
break;
}
if (!globallink_translation_supported($bundle_name)) {
break;
}
$translatable = globallink_is_field_configured_for_translation($entity_type, $bundle_name, $field_name, $bundle_name);
$label = t('GlobalLink Translation');
$title = t('This field can be translated using Translations.com translation services.');
$form['instance']['globallink_translate_field'] = array(
'#prefix' => '<label>' . $label . '</label>',
'#type' => 'checkbox',
'#title' => $title,
'#default_value' => $translatable ? 1 : 0,
);
}
$form['#submit'][] = 'globallink_form_field_ui_field_edit_form_submit';
}
/**
* Update field config for modified bundle field.
*/
function globallink_form_field_ui_field_edit_form_submit($form, &$form_state) {
$field_name = $form['#instance']['field_name'];
$entity_type = $form['#instance']['entity_type'];
$bundle_name = $form['#instance']['bundle'];
if (!globallink_check_field_configured($bundle_name, $entity_type, $bundle_name, $field_name)) {
return;
}
if ($entity_type != 'node') {
return;
}
if (!globallink_translation_supported($bundle_name)) {
return;
}
if (!isset($form['instance']['globallink_translate_field'])) {
return;
}
$translatable = $form['instance']['globallink_translate_field']['#value'];
db_update('globallink_field_config')
->fields(array(
'field_label' => $form['instance']['label']['#value'],
'translatable' => $translatable,
))
->condition('content_type', $bundle_name, '=')
->condition('entity_type', $entity_type, '=')
->condition('bundle', $bundle_name, '=')
->condition('field_name', $field_name, '=')
->execute();
}
/**
* Implements hook_node_presave().
*/
function globallink_node_presave($node) {
if (isset($node->tpt_skip) && $node->tpt_skip == TRUE) {
return;
}
if (!translation_supported_type($node->type)) {
return;
}
if (!isset($node->original)) {
return;
}
$handler = entity_translation_get_handler("node", $node);
$config_fields = globallink_get_translatable_fields($node->type, $node->type);
$orig = $node->original;
$lang = $handler->getFormLanguage();
foreach ($config_fields as $field) {
if ($field->translatable != 1) {
continue;
}
$field_name = $field->field_name;
switch ($field_name) {
case 'title':
if ($orig->title != $node->title) {
globallink_update_change_detection($node, $lang);
break 2;
}
break 1;
case 'metatags':
continue 2;
}
$field_info = field_info_field($field_name);
if ($field_info['type'] == 'list_boolean' && $field_info['type'] == 'image' && $field_info['type'] == 'file' && $field_info['type'] == 'taxonomy_term_reference' && $field_info['type'] == 'field_collection') {
continue;
}
if (!isset($node->$field_name) || !isset($orig->$field_name)) {
globallink_update_change_detection($node, $lang);
break;
}
$o_arr = isset($orig->$field_name) ? $orig->$field_name : array();
$n_arr = isset($node->$field_name) ? $node->$field_name : array();
if (empty($o_arr) || empty($n_arr) || count($o_arr) != count($n_arr)) {
globallink_update_change_detection($node, $lang);
continue;
}
if (!is_array($o_arr) || !is_array($n_arr)) {
if ($o_arr != $n_arr) {
globallink_update_change_detection($node, $lang);
break;
}
continue;
}
if (empty($o_arr[$lang]) && empty($n_arr[$lang]) && isset($o_arr[LANGUAGE_NONE]) && isset($n_arr[LANGUAGE_NONE])) {
$lang = LANGUAGE_NONE;
}
if (isset($o_arr[$lang]) && isset($n_arr[$lang]) && count($o_arr[$lang]) != count($n_arr[$lang])) {
globallink_update_change_detection($node, $lang);
break;
}
if ((isset($o_arr[$lang]) && empty($n_arr[$lang])) || (empty($o_arr[$lang]) && isset($n_arr[$lang]))) {
globallink_update_change_detection($node, $lang);
break;
}
if (count($o_arr[$lang]) != count($n_arr[$lang])) {
globallink_update_change_detection($node, $lang);
break;
}
foreach ($o_arr[$lang] as $delta => $n_field) {
if (isset($n_arr[$lang][$delta]) && isset($n_arr[$lang][$delta]['value'])) {
if ($n_field['value'] != $n_arr[$lang][$delta]['value']) {
globallink_update_change_detection($node, $lang);
break 2;
}
}
else {
globallink_update_change_detection($node, $lang);
break 2;
}
}
}
}
/**
* Implements hook_menu_local_tasks_alter().
*/
function globallink_menu_local_tasks_alter(&$data, $router_item, $root_path) {
$node_check = variable_get('globallink_implementation_type', 0);
if ($root_path != 'node/%/translate') {
return;
}
if (!preg_match("!^node/(\d+)(/.+|)$!", $router_item['href'], $matches)) {
return;
}
if ($node = node_load((int) $matches[1])) {
$nid = $node->tnid;
if ($node->tnid == 0) {
$nid = $node->nid;
}
if (translation_supported_type($node->type)) {
$send_link = 'admin/globallink-translations/dashboard';
$active_link = 'admin/globallink-translations/activeSubmissions';
}
elseif (entity_translation_node_supported_type($node->type)) {
$send_link = 'admin/globallink-translations/dashboard/entity';
$active_link = 'admin/globallink-translations/activeSubmissions/entity';
}
else {
return;
}
if ($node_check == 1) {
if (!globallink_is_node_translatable(node_load($nid))) {
return;
}
}
$pending = TRUE;
$source = language_default()->language;
$t_nodes = translation_node_get_translations($nid);
if (count($t_nodes) > 0) {
if (isset($t_nodes[$source])) {
unset($t_nodes[$source]);
$langs = language_list();
unset($langs[$source]);
if (count($langs) > 0) {
$tgt_arr = globallink_get_tpt_sent_rows($nid, $source);
$tpt_count = count($tgt_arr);
if ($tpt_count > 0 && $tpt_count == count($langs)) {
$pending = FALSE;
}
}
}
}
$data['actions']['output']['globallink'] = array(
'#theme' => 'menu_local_action',
'#link' => array(),