-
Notifications
You must be signed in to change notification settings - Fork 1
/
dedup_views.module
67 lines (57 loc) · 2.02 KB
/
dedup_views.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
<?php
/**
* @file
* dedup_views functionality
*/
/**
* @implements hook_views_pre_view().
*
* https://api.drupal.org/api/views/views.api.php/function/hook_views_pre_view/7
*/
function dedup_views_views_pre_view(&$view, &$display_id, &$args){
// Uncomment if using Devel Module to find the name of the View & View Display
// dpm($view);
// View that is loaded in code to get NIDs for exclusion
$featured_view_name = 'featured_news';
$featured_view_display = 'block';
// View that is being deduped
$automated_view_name = 'automated_news';
$automated_view_display = 'block';
// Only modify the view we intend to
if ($view->name == $automated_view_name && $display_id == $automated_view_display) {
$node_ids = dedup_views_nids_for_exlusion($featured_view_name, $featured_view_display);
// Pass in NIDs of manually referenced content for exlucsion (or de-duping)
if (!empty($node_ids)) {
// $args[] are Views Arguments for the Contextual Filter
// A contectual filter must be added and configured to your view
$args[] = $node_ids;
}
}
}
function dedup_views_nids_for_exlusion($view_name,$view_display) {
$nids = array();
$results = array();
// Load the view
// https://api.drupal.org/api/views/views.module/function/views_get_view/7
$view = views_get_view($view_name);
$view->set_display($view_display);
$view->pre_execute();
$view->execute();
// Get the results
$results = $view->result;
// Make sure there is at least one result
if (count($results) > 0) {
// Build the final array of nids for exlusion
foreach ($results as $result) {
// The result nid may not always be in the same location
// dpm($result) to help troubleshoot...Devel Module Required
if (isset($result->nid)) {
$nids[] = $result->nid;
}
}
// Return $nids in a format we need (comma or plus seperated)
// This is the same format the Views contextual filter would expect if
// You manually typed in a list of NIDs
return implode('+', $nids);
}
}