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


Seeking out fields within a node and returning a list of NIDs is the exact thing that EntityFieldQueries was born to do. (See also: my own question on the topic).

An example from my code, which runs upon webform submission, and in which I seek out nodes of type "Quiz Anzwer Key" to find the one(s) with field "Quiz Reference" filled in with the NID of the currently-validating webform:

    $arg = arg();
    $nid = $arg[1]; //this finds us the NID of the current page
    $query = new EntityFieldQuery;  // this EntityFieldQuery searches through Entities for Nodes that point to the current Webform.
    $result = $query
        ->entityCondition('entity_type', 'node') // find nodes
        ->propertyCondition('status', 1) // that are published
        ->propertyCondition('type', 'quiz_answer_key')  // of type Quiz Answer Key
        ->fieldCondition('field_answer_quiz', 'nid', $nid, '=') // that point to the current node in their Quiz reference field
        ->execute(); // do the query
    $results        = $query->ordered_results; //grab the results

Then, if you want to actually load values from fields in the nodes, you'd use node_load to grab more info out of the nodes you want, like so:

    $ans_key_id     = $results[0]->entity_id;
    $ans_node       = node_load($ans_key_id);       //once the correct node has been identified, load the contents of that node and suss out the answers 

Now $ans_node is an array (or object, I forget) that contains all the info in all the fields in the node. Voilá!

Just off the top of my head

With views 'get_nids', and 'get_related', get_related taking a nid argument, that is set to accept multiple values. This option is hidden under "more", at the bottom of the configuration window.

Option 1:

$view = views_get_view('get_nids');
// Repeating query, ensure the View is set to cache the results.
$view->preview('display_machine_name', array($arg1, $arg2));

$nids = '';
foreach($view->result as $node) {
  $nids += $node->nid . ',';
$nids = rtrim($nids, ',');
$view = views_get_view('get_related');
$view->execute_display('display_machine_name', array($nids));
print $view->render();

Option 2:

Add a "context" display type to get_nids, and pass that context on as the argument to a content pane display of get_related. Unfortunately, I would have to guess that Views contexts lacks support for compressing multiple results into a single context, so that may require a feature request in Ctools.

Option 3:

Beth has an excellent suggestion in a comment, which is using EntityFieldQuery, for when you want a pure code solution, that would also work with MongoDB. Views though has the advantage that most of the work can be done in a UI, which is often convenient.

Ok a coded solution (there area few) but here is one:

  1. Create a view that gets all the nid's output these nid's as a comma de-liminated list, you can do this by using fields as your output and setting your style settings to Style Unfomatted, row style fields -> make the nid field inline and use a comma as a separator.
  2. In your module create a function that calls this view and gets the output
  3. Create your second view (or display) use this function as an argument in your second view that actually does the display output, so under arguments the argument should be node:nid, the Action to take should be set to "provide default" and you then use the PHP code setting to call your function: return mymodule_get_nids(); ensure he checkbox "allow multiple terms per argument" is ticked

Step 2 code

function mymodule_get_nids() {
    $view = views_get_view('getmynids')) // View name
        $view->set_display('mynids'); // Display name
        if ($view->result) {
         return $view->preview(); // Can't remember if this needs to be echo or return try both.

Should this be a separate answer?? or an edit??

Tags: Views

Similar questions

Is there a way to filter blocks by vocabulary instead of NID or CT?
Each time I filter block visibility (that is: When the block will appear in it's region), I can do so only via Node ID or Content-Type... You might have a ready-2-go way to do so with a vocabulary term ? Here is a pseudocode of my meaning: Sorry if it's a bit wired, I am just starting to learn PHP and my meaning is "if at least one term of a given ...
Drupal 7 - How do I parse JSON data from a website and import them as Nodes and have them update hourly?
I have drupal 7 with the Feeds and FeedsJSONParser module installed, but I can't seem to figure out how to configure this to parse the feeds. I want to parse the following feeds into their own nodes: I have the following settings for the JSONparser, but doesnt seem to do anything. Would someone be ki...
What urls does a page manager intercept?
The custom page is set up to intercept /sections/%section. Will it intercept an URl such as '/section/section-1/articlename' or only '/section/section-1' Regarding answer from Berdir: How do I get a custom page to ignore C and respond based upon /A/B regardless of what C value is presented? In my case, C is the name of a specific article of type /A...
How to intercept the loading of a field?
I have a field called field_price. How do I get that field on my page to display Free when the value is $0.00, and the actual value every other time? I tried to intercept the loading of the field by doing things like: I tried so many different permutations of the above as modules, template, changing function names etc... But none of them ever fire....
drupal 7: hook intercept url file requests located within the "Public File System Path"
For Drupal 7 when going to: admin/config/media/file-system you can set up both a Public and Private File System Path. Is it possible for files that are within the "Public File System Path" that when they are directly accessed via url that a hook could intercept that file request and decide based on the users role whether to allow or send them to a ...
Intercept 404s for pages in certain path
I have a site that regularly adds & removes event nodes based on a third party system. When these nodes are removed, any other articles that link to them get 404 pages. Is there a way I could intercept the 404 results for pages with a certain path and redirect them to the event landing page instead? I have tried adding the following to template...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.