I'm trying to create two views.
View-1 is a list of nodes.
View-2 is an image gallery associated with each node.
I basically want to pass the node title from View-1 to a programmatic View-2, so that each row in View-1 will load View-2(with a result set filtered by the title of View-1!).
I'm confused about the approach. Should this happen in a custom module, preprocess functions, or some combination thereof?
I run into this a lot - wanting to pass an argument from a primary view to a secondary view that displays with each result.
I realize that the question is a bit general, but I'm curious how folks with more experience would approach this problem.
This sounds like a good opportunity to use an ajax callback. You could have your primary view on a part of the page just like normal and a secondary view in a custom block or something. when focus lands on the primary item (in the form of a button click or hover or something) you can use an ajax callback to replace the content of your custom block with the secondary view using your argument.
are you using drupal 6 or 7 for this? my understanding is that they do this is different ways.
I've done this before on D6 where basically I just create a couple template tpl.php files for my View-1.
Inside my View-1 template for Display Output (views-view--default.tpl.php in D7 now) I would simply programmatically find the value passed or returned by View-1 for this row.
In your case on each row you would check to see which node is returned by View-1 and then I'd add code in my View-1 template to programmatically load View-2 based on the current View-1 row (ie. node in your case.)
Make sense? 5 months late on the response but I was looking for a refresher and seeing if there's a better way to do this now in D7.
UPDATE:
Just did this on my new D7 install. As an example I'll explain how it relates to my Ubercart implementation.
Ubercart, when installed, has it's main "home" shop page located at
mysite.com/catalog
This page, when loaded, calls a View created by Ubercart called
uc_catalog_terms
. It is a taxonomy based view and all it does is grab all your Catalog taxonomy categories and render them.E.g
As a clothing store, when you navigate to
mysite.com/catalog
, all you'll see at this page is a grid structure like:Sweaters Shirts Jeans
My requirement was that I needed to show the shop catalog categories/terms on this page, but ALSO show 3 random products (images) from that category/term below it each catalog category.
E.g
Sweaters
Random Sweater #1 - Random Sweater #2 - Random Sweater #3
Jeans
Random Jean #1 - Random Jean #2 - Random Jean #3
How is this accomplished?
I created my own brand new custom view (no page or lock, just default) which grabs 3 random product images based on a taxonomy term ID argument and renders 3 linked product images. I'll call this custom view
random_catalog_items
. If15
is the term ID for Sweaters, when this view is called with the argument15
it will only render 3 random linked sweater product images.I now went back to
uc_catalog_terms
view and created aviews-view-fields--uc-catalog-terms.tpl.php
(Row Style Output) template file.THE DEFAULT VIEW VERSION OF THIS FILE (BEFORE MODIFICATION) IS:
THE MODIFIED VERSION BECOMES:
So as you can see inside the first View, for every row that is rendered I get the current taxonomy term ID being shown through the available row result object -
$row->tid
and then I simply call my created view for each row, passing along this Term ID as the argument for it. I leave a lot of the default code in there but inside my view configurations the LABELS and such are set to HIDDEN so they don't even render anyway.In your case it should be very easily adaptable to just pass a Node NID instead of a Taxonomy Term ID.
VOILA IT ALL WORKS! View within a View! Hope this helps :)
It helps to have the Devel module loaded since then inside these View templates you can debug and see what variables are available to you via something like
print krumo($row)
.Personally I would avoid views here alltogether.
A simple module using a
hook_menu
to define the menu-items and two simple menu-callback-functions dealing with the required parameters.The alternative would be to make all the custom parameters and custom query-filtering and tables known to views.
My (pseronal) rule of thumb is:
The basics of this is really simple and most probably a lot less coding and development then writing the views extensions.
Obviously, as stated in the comments, you would additionally create a few theme functions to handle the rendering, to 1) avoid hardcoded spagetty-HTML all over your module and b) allow the frontenders to stay in their theme when creating the HTML.