Hi,

I need render teasers of nodes side by side on one row like this:

<div class="row">
  <div class="teaser left">node teaser</div>
  <div class="teaser right">node teaser</div>
  <div class="cleaner"></div>
</div>

I have implemented hook_view() with node counter on my module , and on my theme i have odd and even counter for left/right class... This is working ok but i have problem if i have only odd number of nodes on page - i need append last (empty) right teaser after last odd teaser and close opened div. How to i can detect last node?

Thanks for advice.

Comments

havran’s picture

Ok, i have found solution hard way (trial and error). I have dig two functions from taxonomy.module and one function from archive.module, rename, slightly modify and override taxonomy/term menu (and archive menu) in mymodule_menu() hook...

from taxonomy module:
taxonomy_render_nodes() and taxonomy_term_page()

on taxonomy/term menu i change callback to mymodule_taxonomy_term_page() function (modified taxonomy_term_page) (same for archive menu callback) and insert odd/even program logic into mymodule_taxonomy_render_nodes() function

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

  if ($may_cache) {
    ...
  }
  else {
    // ovveride archive menu
    $items[] = array(
      'path' => 'archive',
      'title' => t('archives'),
      'access' => user_access('access content'),
      'callback' => 'mymodule_archive_page',
      'type' => MENU_SUGGESTED_ITEM);
    // ovveride taxonomy/term menu
    $items[] = array(
      'path' => 'taxonomy/term',
      'title' => t('taxonomy term'),
      'callback' => 'mymodule_taxonomy_term_page',
      'access' => user_access('access content'),
      'type' => MENU_CALLBACK);
  }

  return $items;
}

// my own variant taxonomy_term_page()
function mymodule_taxonomy_term_page($str_tids = '', $depth = 0, $op = 'page') {
  ...
          // here we need change taxonomy_render_nodes() function for our mymodule_taxonomy_render_nodes() function
          $output = mymodule_taxonomy_render_nodes(taxonomy_select_nodes($tids, $operator, $depth, TRUE, 'n.title'));
  ...
}

// my own variant taxonomy_render_nodes()
function mymodule_taxonomy_render_nodes($result) {
  if (db_num_rows($result) > 0) {
    $counter = 0;
    while ($n = db_fetch_object($result)) {
      // odd/even teaser
      $flag = ($counter % 2) ? FALSE : TRUE;
      // load node object
      $node = node_load($n->nid);
      // save actual flag (for use in theme)
      $node->flag = $flag;
      // left teaser - begin with div row
      if ($node->type == 'mymodule' && $flag) {
        $output .= "\n<div class=\"row\">\n";
        $output .= node_view($node, 1);
        $counter++;
      }
      // right teaser - end with div row
      else if ($node->type == 'mymodule' && !$flag) {
        $output .= node_view($node, 1);
        $output .= "\n<div class=\"cleaner\"></div></div>\n";
        $counter++;
      }
      // for other modules
      else {
        // if not show right teaser before we must end div row
        $output .= !$flag && $counter != 0 ? '<div class="teaser right"></div><div class="cleaner"></div></div>' : '';
        // counter must be set to zero (for possibility begin new row)
        $counter = 0;
        $output .= node_view($node, 1);
      }
    }
    // and last test if not show right teaser before we must end div row
    $output .= $flag && $counter != 0 ? '<div class="teaser right"></div><div class="cleaner"></div></div>' : '';
    // now we can render pager
    $output .= theme('pager', NULL, variable_get('default_nodes_main', 10), 0);
  }
  else {
    $output .= t('There are currently no posts in this category.');
  }
  return $output;
}

// my own variant archive_page()
function mymodule_archive_page($year = 0, $month = 0, $day = 0) {
  ...
    if (db_num_rows($result) > 0) {
      $counter = 0;
      while ($nid = db_fetch_object($result)) {
        // odd/even teaser
        $flag = ($counter % 2) ? FALSE : TRUE;
        // load node object
        $node = node_load($nid->nid);
         ... etc.
         ... same changes as mymodule_taxonomy_render_nodes() function
      }
      $output .= $flag && $counter != 0 ? '<div class="teaser right"></div><div class="cleaner"></div></div>' : '';
    }
    else {
      $output .= theme('box', t('No posts found.'), '');
    }
  ...
}

Template look like this:

<?php if ($page): ?>
  <div class="page">normal node page</div>
<?php elseif ($teaser): ?>
  <div class="teaser<?php if ($flag): print ' left'; else: print ' right'; endif; ?>">node teaser</div>
<?php else: ?>
  <div class="page">normal node page with comments (???)</div>
<?php endif; ?>

Maybe this example help someone. Maybe is there better way for do this but i have found this way :).

Thanks for DRUPAL!

--
My first drupal site - http://www.enigma.sk/kope-vas-muza/ (now developing)

--
My site - Svoji.SK
My first Drupal site - http://www.enigma.sk/

summit’s picture

Hi, Is there a module for this?
greetings, Martijn