forked from ec-europa/nexteuropa-newsroom-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexteuropa_newsroom.module
1265 lines (1135 loc) · 42.1 KB
/
nexteuropa_newsroom.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
* Code for the Nexteuropa Newsroom feature.
*/
include_once 'nexteuropa_newsroom.features.inc';
/**
* @file
* Code for the Nexteuropa Newsroom feature.
*/
module_load_include('inc', 'pathauto', 'pathauto');
module_load_include('inc', 'nexteuropa_newsroom', 'nexteuropa_newsroom.features');
module_load_include('inc', 'nexteuropa_newsroom', 'nexteuropa_newsroom.formatters');
module_load_include('inc', 'nexteuropa_newsroom', 'nexteuropa_newsroom.forms');
module_load_include('inc', 'nexteuropa_newsroom', 'nexteuropa_newsroom.blocks');
module_load_include('inc', 'nexteuropa_newsroom', 'nexteuropa_newsroom.theme');
module_load_include('inc', 'nexteuropa_newsroom', 'nexteuropa_newsroom.menu');
define('NEXTEUROPA_NEWSROOM_ALL', 'all');
define('NEXTEUROPA_NEWSROOM_PROPOSAL_SCRIPT', 'propose-item.cfm');
define('NEXTEUROPA_NEWSROOM_CLICKTHROUGH_FORCE', 'force');
define('NEXTEUROPA_NEWSROOM_CACHE_TABLE', 'cache_newsroom');
define('NEXTEUROPA_NEWSROOM_BASE_URL', 'https://ec.europa.eu/newsroom/');
define('NEXTEUROPA_NEWSROOM_CONTENT_TYPE', 'newsroom_item');
define('NEXTEUROPA_NEWSROOM_ISSUE_CONTENT_TYPE', 'newsroom_newsletter_issue');
define('NEXTEUROPA_NEWSROOM_NEWSLETTER_CONTENT_TYPE', 'newsroom_newsletter_issue');
define('NEXTEUROPA_NEWSROOM_TYPE_VOCABULARY', 'newsroom_item_type');
define('NEXTEUROPA_NEWSROOM_TOPIC_VOCABULARY', 'newsroom_topic');
define('NEXTEUROPA_NEWSROOM_SERVICE_VOCABULARY', 'newsroom_service');
define('NEXTEUROPA_NEWSROOM_TOPIC_IMPORTER', 'newsroom_topics_multilingual');
define('NEXTEUROPA_NEWSROOM_SERVICE_IMPORTER', 'newsroom_services_multilingual');
define('NEXTEUROPA_NEWSROOM_TYPE_IMPORTER', 'newsroom_types_multilingual');
define('NEXTEUROPA_NEWSROOM_ITEM_IMPORTER', 'newsroom_items_multilingual');
define('NEXTEUROPA_NEWSROOM_NEWSLETTER_IMPORTER', 'newsroom_newsletter_multilingual');
define('NEXTEUROPA_NEWSROOM_ITEM_SCRIPT', 'feed/full');
define('NEXTEUROPA_NEWSROOM_NEWSLETTER_SCRIPT', 'feed/newsletter-issues');
define('NEXTEUROPA_NEWSROOM_UNPUBLISH', 'feed/unpublished');
define('NEXTEUROPA_NEWSROOM_DELETE', 'feed/deleted');
define('NEXTEUROPA_NEWSROOM_TOPIC_SCRIPT', 'feed/services');
define('NEXTEUROPA_NEWSROOM_TYPE_SCRIPT', 'feed/item-types');
define('NEXTEUROPA_NEWSROOM_ITEM_SEGMENT', '&item_id=');
define('NEXTEUROPA_NEWSROOM_TOPIC_SEGMENT', '?topic_id=');
define('NEXTEUROPA_NEWSROOM_SERVICE_SEGMENT', '?service_id=');
define('NEXTEUROPA_NEWSROOM_TYPE_SEGMENT', '?item_type_id=');
define('NEXTEUROPA_NEWSROOM_PROPOSAL_ACCESS', 'send newsroom item proposal');
define('NEXTEUROPA_NEWSROOM_IMPORT_ACCESS', 'import newsroom feeds');
define('NEXTEUROPA_NEWSROOM_EDIT_ACCESS', 'edit remote newsroom item');
define('NEXTEUROPA_NEWSROOM_ADMIN_ACCESS', 'administer newsroom settings');
define('NEXTEUROPA_NEWSROOM_ITEM', 'item');
define('NEXTEUROPA_NEWSROOM_ISSUE', 'issue');
define('NEXTEUROPA_NEWSROOM_TOPIC', 'topic');
define('NEXTEUROPA_NEWSROOM_SERVICE', 'service');
define('NEXTEUROPA_NEWSROOM_TYPE', 'type');
/**
* Implements hook_page_build().
*/
function nexteuropa_newsroom_page_build(&$page) {
if (variable_get('nexteuropa_newsroom_use_css') && !strpos($_GET['q'], 'node/add') && empty(preg_match('#node/[0-9]+/edit#', $_GET['q'], $match))) {
_nexteuropa_newsroom_attach_css();
}
}
/**
* Attaches the main newsroom css.
*/
function _nexteuropa_newsroom_attach_css() {
drupal_add_css(drupal_get_path('module', 'nexteuropa_newsroom') . '/styles/nexteuropa_newsroom.css');
}
/**
* Deletes newsroom entity.
*
* @param string $type
* Type: item, topic, service, type.
* @param int $newsroom_id
* Original newsroom id.
*
* @return null
* Returns nothing.
*/
function _nexteuropa_newsroom_delete_item($type, $newsroom_id) {
switch ($type) {
case NEXTEUROPA_NEWSROOM_TYPE:
_nexteuropa_newsroom_taxonomy_term_delete('field_newsroom_type_id', $newsroom_id, NEXTEUROPA_NEWSROOM_TYPE_VOCABULARY);
break;
case NEXTEUROPA_NEWSROOM_TOPIC:
_nexteuropa_newsroom_taxonomy_term_delete('field_newsroom_topic_id', $newsroom_id, NEXTEUROPA_NEWSROOM_TOPIC_VOCABULARY);
break;
case NEXTEUROPA_NEWSROOM_SERVICE:
_nexteuropa_newsroom_taxonomy_term_delete('field_newsroom_service_id', $newsroom_id, NEXTEUROPA_NEWSROOM_SERVICE_VOCABULARY);
break;
case NEXTEUROPA_NEWSROOM_ITEM:
_nexteuropa_newsroom_item_delete($newsroom_id);
break;
default:
NexteuropaNewsroomHelper::set404response();
}
return NULL;
}
/**
* Imports newsroom entity.
*
* @param string $type
* Type: item, topic, service, type.
* @param int $newsroom_id
* Original newsroom id.
*
* @return string
* Link to the item.
*/
function _nexteuropa_newsroom_import_item($type, $newsroom_id) {
$path = NULL;
switch ($type) {
case NEXTEUROPA_NEWSROOM_TYPE:
$path = NexteuropaNewsroomImporterHelper::getTaxonomyTermImportPath($type, $newsroom_id, NEXTEUROPA_NEWSROOM_TYPE_IMPORTER);
break;
case NEXTEUROPA_NEWSROOM_TOPIC:
$path = NexteuropaNewsroomImporterHelper::getTaxonomyTermImportPath($type, $newsroom_id, NEXTEUROPA_NEWSROOM_TOPIC_IMPORTER);
break;
case NEXTEUROPA_NEWSROOM_SERVICE:
$path = NexteuropaNewsroomImporterHelper::getTaxonomyTermImportPath($type, $newsroom_id, NEXTEUROPA_NEWSROOM_SERVICE_IMPORTER);
break;
case NEXTEUROPA_NEWSROOM_ITEM:
$path = NexteuropaNewsroomImporterHelper::getNewsroomItemImportPath($newsroom_id);
break;
default:
NexteuropaNewsroomHelper::set404response();
}
drupal_set_message(t('The item has been imported.'));
return l(t('Open the item'), $path);
}
/**
* Implements hook_help().
*/
function nexteuropa_newsroom_help($path, $arg) {
$help = '';
switch ($path) {
// Help text for the newsroom feature.
case 'admin/help#nexteuropa_newsroom':
$help = '<p>' . t('The nexteuropa newsroom feature is meant to integrate the newsroom corporate service into a Drupal nexteuropa/Next Europa client. By enabling it you will have the chance to fetch contents from the newsroom service basing on the configuraiton of your "universe" into your instance of the newsroom. To configure the newsroom you will need to get an "Universe id" from the service provider, you will have this code once your universe has been fully configured as a service and it will be ready to serve you contents. In the newsroom <a href="@newsroom" title="newsroom configuraiton">admin settings page</a> you can then define many of the newsroom behaviours and you can start palying associating you existing content types with the newly imported news coming from the newsroom.', ['@newsroom' => url('admin/config/content/newsroom')]) . '</p>';
break;
}
return $help;
}
/**
* Implements hook_permission().
*/
function nexteuropa_newsroom_permission() {
return [
NEXTEUROPA_NEWSROOM_ADMIN_ACCESS => [
'title' => t('Administer Newsroom settings'),
],
NEXTEUROPA_NEWSROOM_IMPORT_ACCESS => [
'title' => t('Import Newsroom feeds'),
],
NEXTEUROPA_NEWSROOM_EDIT_ACCESS => [
'title' => t('Edit newsroom item in Newsroom'),
],
NEXTEUROPA_NEWSROOM_PROPOSAL_ACCESS => [
'title' => t('Send newsroom item proposal'),
],
];
}
/**
* Implements hook_image_default_styles().
*/
function nexteuropa_newsroom_image_default_styles() {
// Exported image style: newsroom_style.
$styles['newsroom_style'] = [
'name' => 'newsroom_style',
'label' => 'newsroom_style',
'effects' => [
1 => [
'label' => 'Scale',
'help' => 'Scaling will maintain the aspect-ratio of the original image. If only a single dimension is specified, the other dimension will be calculated.',
'effect callback' => 'image_scale_effect',
'dimensions callback' => 'image_scale_dimensions',
'form callback' => 'image_scale_form',
'summary theme' => 'image_scale_summary',
'module' => 'image',
'name' => 'image_scale',
'data' => [
'width' => 250,
'height' => '',
'upscale' => 1,
],
'weight' => 1,
],
],
];
return $styles;
}
/**
* Implements hook_post_features_enable_feature().
*/
function nexteuropa_newsroom_post_features_enable_feature($component) {
switch ($component) {
case 'taxonomy':
// Set translation mode for the vocabularies.
$importer_id = [
'topics' => NEXTEUROPA_NEWSROOM_TOPIC_VOCABULARY,
'services' => NEXTEUROPA_NEWSROOM_SERVICE_VOCABULARY,
'types' => NEXTEUROPA_NEWSROOM_TYPE_VOCABULARY,
];
foreach ($importer_id as $importer) {
if (!field_info_instance('taxonomy_term', 'name_field', $importer)) {
title_field_replacement_toggle('taxonomy_term', $importer, 'name');
}
}
break;
}
}
/**
* Get instance view mode settings.
*
* @param array|undefined $field_instances
* Array or undefined.
* @param array $modes
* Array.
* @param string $bundle
* Array.
*
* @return field_instances
* Array.
*/
function _nexteuropa_newsroom_get_real_modes(array $field_instances, array $modes = ['teaser', 'full'], $bundle = NEXTEUROPA_NEWSROOM_CONTENT_TYPE) {
// Get the current instances.
$fields = field_info_instances('node', $bundle);
foreach ($field_instances as $field_name => $instance) {
// We work only on the content type, for now.
if (strpos($field_name, 'node-newsroom_item') !== FALSE) {
// Field names are different in the two arrays.
$fields_name = str_replace('node-newsroom_item-', '', $field_name);
// Teaser and full node by default.
foreach ($modes as $view_mode) {
// During install we have to check for reliable data.
if (!empty($fields[$fields_name]['display'][$view_mode]['label'])) {
// Override the field_instances in features with the current settings.
$field_instances[$field_name]['display'][$view_mode] = $fields[$fields_name]['display'][$view_mode];
}
}
}
}
return $field_instances;
}
/**
* Get field groups.
*
* @param array $modes
* Array.
* @param string $bundle
* Array.
*
* @return array
* Array.
*/
function _nexteuropa_newsroom_get_real_groups(array $modes = ['teaser', 'full'], $bundle = NEXTEUROPA_NEWSROOM_CONTENT_TYPE) {
$groups = [];
foreach ($modes as $mode) {
$groups[$mode] = field_group_info_groups('node', $bundle, $mode, TRUE);
}
return $groups;
}
/**
* Enables translations for specific fields.
*/
function _nexteuropa_newsroom_enable_translations() {
// Enable entity translation support for terms.
$enabled = variable_get('entity_translation_entity_types', []);
if (!in_array('taxonomy_term', $enabled)) {
$enabled['taxonomy_term'] = 'taxonomy_term';
}
if (!in_array('file', $enabled)) {
$enabled['file'] = 'file';
}
variable_set('entity_translation_entity_types', $enabled);
// Enable translations for various fields.
$field_names = [
'body',
'field_file_image_alt_text',
'field_file_image_title_text',
'field_caption',
];
foreach ($field_names as $field_name) {
if ($field = field_read_field($field_name)) {
// Make the field translatable.
$field['translatable'] = 1;
field_update_field($field);
}
}
}
/**
* Implements hook_url_inbound_alter().
*/
function nexteuropa_newsroom_url_inbound_alter(&$path, $original_path, $path_language) {
// Check if no url alias has not been found for incoming path.
if ($path && $path == $original_path) {
// Resolve alias if the content is not translated to current language. Get
// alias of existing translation.
foreach (array_keys(language_list()) as $language) {
$source = drupal_lookup_path('source', $path, $language);
if ($source && strpos($source, 'node/') === 0) {
// Check if we are at the document page.
$path = $source;
break;
}
}
}
}
/**
* Implements hook_node_presave().
*/
function nexteuropa_newsroom_node_presave($node) {
if ($node->type == NEXTEUROPA_NEWSROOM_CONTENT_TYPE && variable_get('nexteuropa_newsroom_patterns', FALSE)) {
// Get all the types.
$type_tid = NexteuropaNewsroomHelper::getFieldValue($node, 'field_newsroom_item_type', 'tid');
if (!empty($type_tid) && $type = NexteuropaNewsroomVocabularyHelper::getNewsroomType($type_tid)) {
$type_name = pathauto_cleanstring($type->name);
if ($conf = variable_get('nexteuropa_newsroom_' . $type_name . '_root', FALSE)) {
$node->path['pathauto'] = FALSE;
$node->path['alias'] = $conf . '/' . pathauto_cleanstring($node->title);
pathauto_alias_uniquify($node->path['alias'], !empty($node->nid) ? 'node/' . $node->nid : NULL, NexteuropaNewsroomHelper::getDefaultLanguage());
}
}
}
}
/**
* Implements hook_date_format_types().
*/
function nexteuropa_newsroom_date_format_types() {
return [
'newsroom_date_only' => t('NR : Date only'),
'newsroom_long_type_without_time' => t('NR : Long date without time'),
];
}
/**
* Implements hook_date_formats().
*/
function nexteuropa_newsroom_date_formats() {
return [
[
'type' => 'newsroom_date_only',
'format' => 'd/m/Y',
'locales' => [],
],
[
'type' => 'newsroom_long_type_without_time',
'format' => 'j F Y',
'locales' => [],
],
];
}
/**
* Implements hook_feeds_after_import().
*/
function nexteuropa_newsroom_feeds_after_import(FeedsSource $source) {
// Restore the default URL in order to run importers manually or via cron.
NexteuropaNewsroomImporterHelper::setDefaultImporterUrl($source->importer->fetcher->id);
}
/**
* Implements hook_views_data_alter().
*/
function nexteuropa_newsroom_views_data_alter(&$data) {
// Term name field.
$data['taxonomy_term_data']['name'] = [
'title' => t('Name'),
'help' => t('The taxonomy term name.'),
'field' => [
'handler' => 'nexteuropa_newsroom_views_handler_field_taxonomy',
'click sortable' => TRUE,
],
];
}
/**
* Implements hook_node_view().
*/
function nexteuropa_newsroom_node_view($node, $view_mode, $langcode) {
if ($node->type == NEXTEUROPA_NEWSROOM_CONTENT_TYPE &&
in_array($view_mode, ['full', 'newsroom_events', 'newsroom_default'])) {
$type = NexteuropaNewsroomVocabularyHelper::getNewsroomType(NexteuropaNewsroomHelper::getFieldValue($node, 'field_newsroom_item_type', 'tid'));
$main_link_url = NexteuropaNewsroomHelper::getFieldTranslatedValue($node, 'field_newsroom_item_main_link', 'url');
// Custom main link title per type.
if ($type && $main_link_url) {
// Case there is a custom read more text set, change it.
if ($read_more_text = NexteuropaNewsroomHelper::getFieldTranslatedValue($type, 'field_newsroom_read_more_text', 'value', 'taxonomy_term')) {
$node->content['field_newsroom_item_main_link'][0]['#element']['title'] = $read_more_text;
}
}
// Custom responsible person label per type.
$speaker = NexteuropaNewsroomHelper::getFieldTranslatedValue($node, 'field_newsroom_item_speaker');
if ($type && $speaker) {
// Case there is a custom read more text set, change it.
if ($responsible_person = NexteuropaNewsroomHelper::getFieldTranslatedValue($type, 'field_newsroom_resp_person_label', 'value', 'taxonomy_term')) {
$node->content['field_newsroom_item_speaker']['#title'] = $responsible_person;
}
}
}
}
/**
* Controls access to page based on IP (newsroom server) and role.
*
* @return bool
* Result of check whether the remote client can call import or other ops.
*/
function _nexteuropa_newsroom_item_import_access() {
$allowed_ips = array_map('trim', explode(',', variable_get('nexteuropa_newsroom_allowed_ips')));
return user_access(NEXTEUROPA_NEWSROOM_IMPORT_ACCESS) ? TRUE : in_array(ip_address(), $allowed_ips);
}
/**
* Deletes taxonomy term by original newsroom ID.
*
* @param string $field_name
* Field name to store original newsroom ID.
* @param int $original_newsroom_id
* Original newsroom id.
* @param string $bundle
* Bundle.
*/
function _nexteuropa_newsroom_taxonomy_term_delete($field_name, $original_newsroom_id, $bundle) {
if ($term = NexteuropaNewsroomHelper::getTaxonomyTermFromField($field_name, $original_newsroom_id, $bundle)) {
taxonomy_term_delete($term->tid);
drupal_set_message(t('Item has been deleted.'));
}
else {
NexteuropaNewsroomHelper::set404response();
}
}
/**
* Deletes newsroom item by original newsroom ID.
*
* @param int $newsroom_id
* Original newsroom id.
*/
function _nexteuropa_newsroom_item_delete($newsroom_id) {
if ($newsroom_id && $newsroom_item = NexteuropaNewsroomHelper::getNodeByFieldValue('field_newsroom_item_id', $newsroom_id)) {
node_delete_multiple([$newsroom_item->nid]);
$message = t('Newsroom Item nid: %1 deleted', ['%1' => implode(', ', $newsroom_item->nid)]);
drupal_set_message($message);
}
else {
NexteuropaNewsroomHelper::set404response();
}
}
/**
* Redirects to the Newsroom Item. With the Original Newsroom ID.
*
* @param int $newsroom_id
* Original newsroom ID.
*/
function _nexteuropa_newsroom_item_redirect($newsroom_id = 0) {
_nexteuropa_newsroom_redirect(NEXTEUROPA_NEWSROOM_ITEM, $newsroom_id);
}
/**
* Redirects to the Newsroom Item with the Original Newsroom ID.
*
* @param string $redirect_type
* Redirect type: item, type, topic.
* @param int $newsroom_entity_id
* ID of entity from newsroom side.
*/
function _nexteuropa_newsroom_redirect($redirect_type = NEXTEUROPA_NEWSROOM_ITEM, $newsroom_entity_id = 0) {
if (!$newsroom_entity_id) {
NexteuropaNewsroomHelper::set404response();
}
$path = FALSE;
switch ($redirect_type) {
// Newsroom item based on original newsroom ID.
case NEXTEUROPA_NEWSROOM_ITEM:
if ($item = NexteuropaNewsroomHelper::getNodeByFieldValue('field_newsroom_item_id', $newsroom_entity_id)) {
$path = 'node/' . $item->nid;
}
break;
// Newsroom newsletter issue.
case NEXTEUROPA_NEWSROOM_ISSUE:
if ($item = NexteuropaNewsroomHelper::getNodeByFieldValue('field_newsroom_newsletter_id', $newsroom_entity_id)) {
$path = 'node/' . $item->nid;
}
break;
// Newsroom Item Type based on original import name.
case NEXTEUROPA_NEWSROOM_TYPE:
$path = _newsroom_nexteuropa_get_term_redirect_path('field_newsroom_type_id', $newsroom_entity_id, NEXTEUROPA_NEWSROOM_TYPE_VOCABULARY);
break;
// Newsroom Topic based on original newsroom topic ID.
case NEXTEUROPA_NEWSROOM_TOPIC:
$path = _newsroom_nexteuropa_get_term_redirect_path('field_newsroom_topic_id', $newsroom_entity_id, NEXTEUROPA_NEWSROOM_TOPIC_VOCABULARY);
break;
// Newsroom service.
case NEXTEUROPA_NEWSROOM_SERVICE:
$path = _newsroom_nexteuropa_get_term_redirect_path('field_newsroom_service_id', $newsroom_entity_id, NEXTEUROPA_NEWSROOM_SERVICE_VOCABULARY);
break;
}
if ($path) {
drupal_goto($path, [], 301);
}
else {
NexteuropaNewsroomHelper::set404response();
}
}
/**
* Get redirect URL for taxonomy term.
*
* @param string $field_name
* Field name with original newsroom id.
* @param int $newsroom_entity_id
* Original newsroom id.
* @param string $bundle
* Bundle.
*
* @return string
* Taxonomy term url.
*/
function _newsroom_nexteuropa_get_term_redirect_path($field_name, $newsroom_entity_id, $bundle) {
$path = FALSE;
if ($taxonomy_term = NexteuropaNewsroomHelper::getTaxonomyTermFromField($field_name, $newsroom_entity_id, $bundle)) {
$path = 'taxonomy/term/' . $taxonomy_term->tid;
}
return $path;
}
/**
* Implements hook_views_pre_view().
*/
function nexteuropa_newsroom_views_pre_view(&$view, &$display_id, array &$args) {
if ($view->name == 'newsletter_subscription') {
// The proper context filter cannot be found for taxonomy term pages
// so we set it manually.
$current_path = menu_get_item();
// Try to get proper taxonomy context filter.
if ($current_path['path'] === 'taxonomy/term/%') {
$taxonomy_term = NULL;
$current_page_path = current_path();
// Never sure what menu_callback will get you with.
// A view overridding a taxo page.
$url_tokens = explode('/', $current_page_path);
if (!empty($url_tokens[2]) && is_numeric($url_tokens[2])) {
$taxonomy_term = taxonomy_term_load($url_tokens[2]);
}
if ($associated_topic_id = NexteuropaNewsroomHelper::getFieldValue($taxonomy_term, 'field_newsroom_associated_topic', 'tid')) {
$has_newsroom_topic_argument = FALSE;
// Check the current display.
if (!empty($view->display[$view->current_display]->handler->options['arguments'])) {
foreach ($view->display[$view->current_display]->handler->options['arguments'] as $argument) {
// Check if this field taxonomy ref to newsroom_topic vocabulary.
if ($argument['default_argument_type'] == 'taxonomy_tid' && !empty($argument['default_argument_options']['vocabularies']['newsroom_topic'])) {
$has_newsroom_topic_argument = TRUE;
break;
}
}
}
// Check the default display, if we don't have ovverrides.
if (!empty($view->display['default']->handler->options['arguments'])) {
foreach ($view->display['default']->handler->options['arguments'] as $argument) {
// Check if this field taxonomy ref to newsroom_topic vocabulary.
if ($argument['default_argument_type'] == 'taxonomy_tid' &&
!empty($argument['default_argument_options']['vocabularies']['newsroom_topic'])) {
$has_newsroom_topic_argument = TRUE;
break;
}
}
}
// Set argument from the taxonomy associated newsroom topic.
if ($has_newsroom_topic_argument) {
$args = [$associated_topic_id];
}
}
}
}
}
/**
* Implements hook_entity_presave().
*/
function nexteuropa_newsroom_entity_presave($entity, $type) {
// Set custom updated date from newsroom.
if (!empty($entity->_feed_changed) && isset($entity->changed)) {
$entity->changed = $entity->_feed_changed;
}
}
/**
* Implements hook_feeds_after_save().
*/
function nexteuropa_newsroom_feeds_after_save(FeedsSource $source, $entity, $item, $entity_id) {
if ($source->importer()->fetcher->id == 'newsroom_unpublished_items' || $source->importer()->fetcher->id == 'newsroom_deleted_items') {
NexteuropaNewsroomHelper::deleteEntityTranslations($entity, $entity_id);
entity_delete('node', $entity_id);
}
elseif ($source->importer()->fetcher->id == 'newsroom_items_multilingual') {
// TODO : find a better way to remove translation.
$languages = language_list();
// Try to remove disabled translations, check the title for translation.
foreach ($languages as $language) {
// Check only active and skip the default language.
if ($language->enabled && $language->language != NexteuropaNewsroomHelper::getDefaultLanguage()) {
$config = $source->importer()->processor->getConfig();
if (!empty($config['mappings'])) {
$xpath_expression = NULL;
foreach ($config['mappings'] as $mapping) {
if ($mapping['target'] == 'title_field:et:' . $language->language) {
$xpath_expression = $mapping['source'];
break;
}
}
// If title is empty it means there is no translation.
if (!empty($xpath_expression) || empty($item[$xpath_expression])) {
NexteuropaNewsroomHelper::deleteEntityTranslations($entity, $entity_id, $language);
}
}
}
}
$image_fields = [
'field_newsroom_illustrative_img',
'field_newsroom_private_img',
];
foreach ($image_fields as $image_field) {
_nexteuropa_newsroom_set_image_meta_info($image_field, $entity, $source, $item, $languages, NexteuropaNewsroomHelper::getDefaultLanguage());
_nexteuropa_newsroom_clean_files($entity, $image_field);
}
}
}
/**
* Clean up files.
*
* @param object $node
* Current node.
* @param string $field
* Field name.
*/
function _nexteuropa_newsroom_clean_files($node, $field) {
$count = 0;
if (!empty($node->{$field}[LANGUAGE_NONE][0]['fid'])) {
$file = file_load($node->{$field}[LANGUAGE_NONE][0]['fid']);
if (!empty($file)) {
$query = db_select('file_managed', 'f');
$query->leftJoin('file_usage', 'fu', 'f.fid = fu.fid');
$query->leftJoin('node', 'n', 'fu.id = n.nid');
$query->fields('f', ['fid', 'status', 'filesize', 'filemime', 'uri']);
$query->fields('fu', ['id']);
$query->condition('f.filesize', $file->filesize);
$query->condition('f.filemime', $file->filemime);
$query->condition('f.filename', $file->filename);
$query->condition('f.uri', '%newsroom%', 'LIKE');
$query->condition('f.fid', $file->fid, '<>');
$db_or = db_or();
$db_or->condition('n.type', NEXTEUROPA_NEWSROOM_CONTENT_TYPE);
$db_or->isNull('n.type');
$query->condition($db_or);
$result = $query->execute()->fetchAll();
foreach ($result as $fid) {
$file_to_delete = file_load($fid->fid);
if (!empty($file_to_delete)) {
file_delete($file_to_delete, TRUE);
$count++;
}
}
}
}
return $count;
}
/**
* Save image meta info.
*
* @param string $image_field
* Image field name.
* @param object $entity
* Entity object.
* @param object $source
* Feed source object.
* @param array $item
* Feed result array.
* @param array $languages
* Array of the available languages.
* @param string $default_language
* Default language code.
*/
function _nexteuropa_newsroom_set_image_meta_info($image_field, $entity, $source, array $item, array $languages, $default_language) {
if ($fid = NexteuropaNewsroomHelper::getFieldValue($entity, $image_field, 'fid')) {
if ($file = file_load($fid)) {
$fields = [
'field_file_image_title_text' => 'title',
'field_newsroom_copyrights' => 'copyright',
'field_caption' => 'caption',
'field_file_image_alt_text' => 'alt',
];
$config = $source->importer()->processor->getConfig();
if (!empty($config['mappings'])) {
// Save default language at first.
foreach ($config['mappings'] as $mapping) {
foreach ($fields as $field => $field_name) {
if ($mapping['target'] == $image_field . ':' . $field_name . ':et:' . $default_language && !empty($mapping['source']) && !empty($item[$mapping['source']])) {
$file->$field = [$default_language => [0 => ['value' => $item[$mapping['source']]]]];
}
}
}
$handler = entity_translation_get_handler('file', $file);
// Handle the rest of available translations.
foreach ($config['mappings'] as $mapping) {
foreach ($fields as $field => $field_name) {
foreach ($languages as $language) {
if ($language->language == $default_language) {
continue;
}
if ($mapping['target'] == $image_field . ':' . $field_name . ':et:' . $language->language && !empty($mapping['source']) && !empty($item[$mapping['source']])) {
$translation = [
'translate' => 0,
'status' => 1,
'language' => $language->language,
'source' => $default_language,
];
$values = [
"$field" => [
"$language->language" => [
'0' => [
'value' => $item[$mapping['source']],
],
],
],
];
$handler->setTranslation($translation, $values);
}
}
}
}
}
file_save($file);
if (module_exists('entitycache')) {
cache_clear_all($fid, 'cache_entity_file');
}
}
}
}
/**
* Implements hook_feeds_set_target().
*/
function nexteuropa_newsroom_feeds_set_target(FeedsSource $source, $entity, $target, $feed_element) {
$entity->_feed_changed = feeds_to_unixtime($feed_element, REQUEST_TIME);
}
/**
* Implements hook_feeds_processor_targets_alter().
*/
function nexteuropa_newsroom_feeds_processor_targets_alter(&$targets, $entity_type, $bundle_name) {
// Set custom target callback to set updated date later.
$targets['_feed_changed'] = [
'name' => t('Changed date'),
'description' => t('The UNIX time when a !entity_type has been changed.', ['!entity_type' => $entity_type]),
'callback' => 'newsroom_feeds_set_target',
];
// Add custom targets for file fields: copyrights, caption, title, alt.
$field_names = [
'field_newsroom_illustrative_img:title:et:' => 'field_file_image_title_text',
'field_newsroom_illustrative_img:copyright:et:' => 'field_newsroom_copyrights',
'field_newsroom_illustrative_img:caption:et:' => 'field_caption',
'field_newsroom_illustrative_img:alt:et:' => 'field_file_image_alt_text',
'field_newsroom_private_img:title:et:' => 'field_file_image_title_text',
'field_newsroom_private_img:copyright:et:' => 'field_newsroom_copyrights',
'field_newsroom_private_img:caption:et:' => 'field_caption',
'field_newsroom_private_img:alt:et:' => 'field_file_image_alt_text',
];
$fields = [];
$languages = language_list();
foreach ($languages as $language) {
// Check only active and skip the default language.
if ($language->enabled) {
foreach (array_keys($field_names) as $field_name) {
$fields[] = $field_name . $language->language;
}
}
}
foreach (field_info_instances('file', 'image') as $name => $instance) {
if (in_array($name, $field_names)) {
foreach ($fields as $field) {
$targets[$field] = [
'name' => t('Image:@name', ['@name' => $field]),
'callback' => NULL,
'description' => t('The @label field of the image.', ['@label' => $field]),
'real_target' => $field,
];
}
}
}
if ($entity_type == 'node') {
if ($bundle_name == NEXTEUROPA_NEWSROOM_CONTENT_TYPE && !empty($targets['field_newsroom_item_id'])) {
$targets['field_newsroom_item_id']['unique_callbacks'] = ['_nexteuropa_newsroom_use_as_guid'];
$targets['field_newsroom_item_id']['optional_unique'] = TRUE;
}
elseif ($bundle_name == NEXTEUROPA_NEWSROOM_ISSUE_CONTENT_TYPE && !empty($targets['field_newsroom_newsletter_id'])) {
$targets['field_newsroom_newsletter_id']['unique_callbacks'] = ['_nexteuropa_newsroom_use_as_guid'];
$targets['field_newsroom_newsletter_id']['optional_unique'] = TRUE;
}
}
}
/**
* Implements hook_field_default_field_instances_alter().
*/
function nexteuropa_newsroom_field_default_field_instances_alter(&$fields) {
// Add the teaser and full view mode to the display key, if they exists.
$fields = _nexteuropa_newsroom_get_real_modes($fields);
}
/**
* Callback for the unique_callbacks specified in hook_feed_processor_targets().
*
* @param FeedsSource $source
* The Feed source.
* @param string $entity_type
* Entity type for the entity to be processed.
* @param string $bundle
* Bundle name for the entity to be processed.
* @param string $target
* A string identifying the unique target on the entity.
* @param array $values
* The unique values to be checked.
*
* @return int|null
* The existing entity id, or NULL if no existing entity is found.
*
* @see hook_feeds_processor_targets()
* @see FeedsProcessor::existingEntityId()
*/
function _nexteuropa_newsroom_use_as_guid(FeedsSource $source, $entity_type, $bundle, $target, array $values) {
// Example for if the target is a field.
$query = new EntityFieldQuery();
$result = $query
->entityCondition('entity_type', $entity_type)
->entityCondition('bundle', $bundle)
->fieldCondition($target, 'value', $values)
->addTag('DANGEROUS_ACCESS_CHECK_OPT_OUT')
->execute();
if (!empty($result[$entity_type])) {
return key($result[$entity_type]);
}
return NULL;
}
/**
* Implements hook_views_handlers().
*/
function nexteuropa_newsroom_views_handlers() {
return [
'handlers' => [
'newsroom_views_handler_field_taxonomy' => [
'parent' => 'views_handler_field_taxonomy',
'path' => drupal_get_path('module', 'nexteuropa_newsroom') . '/views',
],
],
];
}
/**
* Implements hook_feeds_after_parse().
*/
function nexteuropa_newsroom_feeds_after_parse(FeedsSource $source, FeedsParserResult $result) {
if ($source->id == NEXTEUROPA_NEWSROOM_ITEM_IMPORTER) {
// We set end date to start date if it is empty or doesn't exist.
$config = $source->importer()->processor->getConfig();
$start_date_exp = '';
$end_date_exp = '';
// Check Xpath expressions for Start date and End date.
if (!empty($config['mappings'])) {
foreach ($config['mappings'] as $mapping) {
if ($mapping['target'] == 'field_newsroom_item_date:start') {
$start_date_exp = $mapping['source'];
}
if ($mapping['target'] == 'field_newsroom_item_date:end') {
$end_date_exp = $mapping['source'];
}
if (!empty($start_date_exp) && !empty($end_date_exp)) {
break;
}
}
}
// If end date is empty we change it to start date.
foreach ($result->items as &$item) {
if (empty($item[$end_date_exp]) && !empty($item[$start_date_exp])) {
$item[$end_date_exp] = $item[$start_date_exp];
}
}
}
}
/**
* Implements hook_flush_caches().
*/
function nexteuropa_newsroom_flush_caches() {
return [NEXTEUROPA_NEWSROOM_CACHE_TABLE];
}
/**
* Implements hook_context_page_reaction().
*/
function nexteuropa_newsroom_context_page_reaction() {
if ($plugin = context_get_plugin('reaction', 'newsroom_agenda_block')) {
$plugin->execute();
}
}
/**
* Implements hook_context_plugins().
*/
function nexteuropa_newsroom_context_plugins() {
$plugins = [];
// Add a plugin for tabs
// it allows us set agenda block with certain newsroom types.
$plugins['newsroom_agenda_block_reaction'] = [
'handler' => [
'path' => drupal_get_path('module', 'nexteuropa_newsroom') . '/plugins',
'file' => 'nexteuropa_newsroom_agenda_reaction.inc',
'class' => 'NexteuropaNewsroomAgendaReaction',