Skip to main content
5 of 6
re-wrote additional info
Ashlar
  • 5.1k
  • 6
  • 35
  • 56

Is there a way to intercept a list of NID's created by a view and use them as a filter for another view?

I am at the point in Drupal experience where I am attempting to solve site design challenges at the custom code level using Drupal and Views APIs. As usual, my apparently simple attempt to solve a problem has demonstrated to me how much I don't know yet!

I have a node-reference field that points to a 'related facts' content used by many different content types and subcategories for each of these types. While I can create a relationship and display the 'related facts' for a view, there are problems. For one, I want the related facts put in a separate block and to display a single list of related facts for the entire list of articles provided by the view. Views does not handle any duplicates for multiple nids with the same 'related facts' reference well. In addition, I would have to generate separate 'related facts' views for each content type and subcategory which will be quite a large number.

If I could create a separate related facts view that can accept a list of nids as an argument, the related facts view could be simplified. So my question is how to go about this. While it may be possible from within views, I am seeking a custom module php solution, so I need to achieve the following:

  1. Extract the results of any of the 'content type' views as a list of arguments. In addition to displaying the view results, I need to load the list of NIDs that are being displayed.

  2. Call the related facts view and pass the list of nids to it to use as a filter or contextual filter in the 'related facts' view.

Additional Information:

Using this strategy I began by defining a view and attempting to extract the results from it. This attempt led to a problem finding the results in the view object (see this question). Once I get the results I will need to generate another view that has those results as filters. I have seen views implemented from code as include files where all the parameters are coded, but have not found examples that define and generate a view and manipulate the results using a php function. Any examples or links would be appreciated.

Thus Far:

Two answers have addressed the process for applying the results of a view and placing it in another view as a contextual filter. But I still face the problem of having dozens of content types with at least a dozen view content pane variations in each. Instead of directly linking each view to the related facts, I will need to have the any of my view1 content views pass its results to a single, separate 'related fact' view.

CONTENT TYPE    SUB-CATEGORY       RELATED FACT (nid)
    Type 1 --------- General Info       101, 105 
                     Specifications     103, 105
                     Inspections        102 
                     Quality  
                     etc...   
   Type 2 ----------General Info       101, 106
                    Specifications     102, 103 
                    Cost factors       107
                    etc... 

View1 finds a specific content type and contains various sub-category articles sorted by the value in a subcategory field. In addition it contains a node reference field for related facts. View one filters for a content type and a subcategory and has exposed fields for display and a hidded field with thte related fact reference nodes.

View2 filters for 'related fact' articles and contains a contextual filter set for nid (although the setting applies to the URL)

Using the code in Letharion's answer, the foreach loop in line 6 is compiling a list of the article ID's, not the related fact id's (the articles hold a reference to related facts, but the related facts do not have a node reference to articles). I need to extract the related fact id's from the facts field in each node. I have included the facts field in the View1 definition, but cannot find the result since the command dpm($node); has been crashing and I don't know where the values are stored in the array.

Since there will be more than one View1 cloned for different content types I thought it may make sense to use hook_views_post_execute and have coded it as follows:

function mymodule_views_post_execute (&$sourceview) {

  $nids = '';
  foreach($sourceview->result as $node) {
    $nids += $node->nid . ',';  //I need field name for $node->facts
    
  }
  
  $nids = rtrim($nids, ',');
  $view = views_get_view('get_related');
  
  //dpm($nids);  // Fatal error: Cannot unset string offsets...

  $view->execute_display('panel_pane_1', array($nids));
  }

Finally, I am not sure if I have to direct the display of the view to the proper location or is this done simply by placing the related facts content pane in the panel location through the Panels UI and let panels theming handle the rest.

Ashlar
  • 5.1k
  • 6
  • 35
  • 56