开发者

Programmatic Views in Drupal 7

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.


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. If 15 is the term ID for Sweaters, when this view is called with the argument 15 it will only render 3 random linked sweater product images.

I now went back to uc_catalog_terms view and created a views-view-fields--uc-catalog-terms.tpl.php (Row Style Output) template file.

THE DEFAULT VIEW VERSION OF THIS FILE (BEFORE MODIFICATION) IS:

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php print $field->content; ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>

THE MODIFIED VERSION BECOMES:

<?php foreach ($fields as $id => $field): ?>
  <?php if (!empty($field->separator)): ?>
    <?php print $field->separator; ?>
  <?php endif; ?>

  <?php print $field->wrapper_prefix; ?>
    <?php print $field->label_html; ?>
    <?php
        $title = str_replace('/','-',strtolower($field->raw));
        print '<img src="'.drupal_get_path('theme','my_theme').'/images/catalog/'.$title.'-header.png" />';
        print '<hr style="width: 100%; background: #000; height: 2px; margin-top: 3px;"/>';
        // get the taxonomy term ID 
        $tid = $row->tid;
        // render the 3 random items
        if ($random_products = views_get_view('random_catalog_items' )) {
            print $random_products->execute_display('default', array($tid));
        }

    ?>
  <?php print $field->wrapper_suffix; ?>
<?php endforeach; ?>

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:

  • If you are certain you will re-use the code several times in future projects a view-addon is worth the investment.
  • If you will use the views-interface for more then one-time-setup (the general use) e.g. define or change views in an editors/webmasters workflow this makes sense.

The basics of this is really simple and most probably a lot less coding and development then writing the views extensions.

/** Implementation of hook_menu().
 */
function gallery_menu() {
  $items = array();

  $items['gallery'] = array(
    'title'            => 'Gallery',
    'page callback'    => '_gallery_list',
    'access arguments' => array('access content'),
  );

  $items['gallery/%gallery'] = array(
    'title'            => 'For dynamic titles, see title_callback documentation',
    'page callback'    => '_gallery_view',
    'access arguments' => array('access content'),
  );

  return $items;
}

/** Load a gallery from database. Name follows %parameter_load hook.
 */
function gallery_load($id) {
  return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}

/** Render a list of galleries.
 */
function _gallery_list() {
  $html = "";
  $galleries = pager_query("SELECT * FROM {galleries}", 10);

  foreach($galleries as $gallery) {
    $html .= check_plain($gallery->title); //You would actually build vars here and push them to theme layer instead.
  }
  $html .= theme("pager");
  return $html;
}

/** Load a gallery from database. Name follows %parameter_load hook.
 */
function gallery_load($id) {
  return db_query("SELECT * FROM {galleries} WHERE id = %d", $id);
}

/** Render a list of galleries.
 */
function _gallery_view($gallery) {
  $html = "";
  $images = pager_query("SELECT * FROM {images} WHERE gallery_id = %d", 10, $gallery->id);

  foreach($images as $image) {
    $html .= check_plain($image->title); //You would actually build vars here and push them to theme layer instead.
  }
  $html .= theme("pager");
  return $html;
}

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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜