Simple Node Pager for the Menu System in Drupal 7

4 years 11 months ago

Ive been working with drupal 7 for about a year...  Self taught.  I like it.  

Its very coder friendly... But at the same time can be very intuitive for clients... As long as you set the site up correctly...

Anyways there has been a nagging thing that wasnt available in a module for some reason.   A simple node pager for the regular menu system in drupal 7.  After trying to come up with some elaborate view with contextual filters i decided to code one. It should give you a previous and next buttons for the depth your page is on.   It uses the current url.. So it should work for most applications.  Its also assuming you have and are using pathauto. Put it in your template.php file and call the function anywhere.

function printpager(){ 
$nextt = "";
$prevt = "";
$weight = "";
$result = db_query("SELECT weight,menu_name,depth,plid FROM menu_links where link_path = '".current_path()."'");
foreach ($result as $record) {
 $weight = $record->weight;
 $menu_name = $record->menu_name;
 $depth = $record->depth;
 $plid = $record->plid;
}

$i = 0;
$result = db_query("SELECT link_title,link_path FROM menu_links where menu_name = '".$menu_name."' and weight > '".$weight."' and depth = '".$depth."' and plid = '".$plid."' order by weight");
foreach ($result as $record) {
 if ($i == 0){
  $nextt = $record->link_title;
  $nextu = $record->link_path;
 }
 $i = $i + 1;
}

$i = 0;
$result = db_query("SELECT link_title,link_path FROM menu_links where menu_name = '".$menu_name."' and weight < '".$weight."' and depth = '".$depth."' and plid = '".$plid."' order by weight desc" );
foreach ($result as $record) {
 if ($i == 0){
  $prevt = $record->link_title;
  $prevu = $record->link_path;
 }
 $i = $i + 1;
}

 

?>
<div id="nodepager"><table><tr><td align="left" class="td1">
<?php
if ($prevt != ""){
 $result = db_query("SELECT alias FROM url_alias where source = '".$prevu."'");
 foreach ($result as $record) {
  $prevu = $record->alias;
 }
 ?>
     <h4>Return To</h4>
  <a href="<?php echo $GLOBALS['base_url'].'/'.$prevu ?>"><?php echo $prevt ?></a>
 <?
}?>

</td><td  class="td2" align="right">
<?php
if ($nextt != ""){
$result = db_query("SELECT alias FROM url_alias where source = '".$nextu."'");
foreach ($result as $record) {
 $nextu = $record->alias;
}?>
<h4>Continue To</h4>
<a href="<?php echo $GLOBALS['base_url'].'/'.$nextu ?>"><?php echo $nextt ?></a>
<?php } ?>
</td></tr></table></div>

<?php }

 Heres a bonus function to print the first child one level down

function print1leveldown(){
   
$nextt = "";
$prevt = "";
$weight = "";
$result = db_query("SELECT weight,menu_name,depth,mlid FROM menu_links where link_path = '".current_path()."'");
foreach ($result as $record) {
 $weight = $record->weight;
 $menu_name = $record->menu_name;
 $depth = intval($record->depth) + 1;
 $mlid = $record->mlid;
}

$i = 0;
$result = db_query("SELECT link_title,link_path FROM menu_links where menu_name = '".$menu_name."'  and depth = '".$depth."' and plid = '".$mlid."' order by weight");
foreach ($result as $record) {
 if ($i == 0){
  $nextt = $record->link_title;
  $nextu = $record->link_path;
 }
 $i = $i + 1;
}

?>
<div id="nodepager"><table><tr><td align="left" class="td1">
</td><td  class="td2" align="right">
<?php
if ($nextt != ""){
$result = db_query("SELECT alias FROM url_alias where source = '".$nextu."'");
foreach ($result as $record) {
 $nextu = $record->alias;
}?>
<h4>Continue To</h4>
<a href="<?php echo $GLOBALS['base_url'].'/'.$nextu ?>"><?php echo $nextt ?></a>
<?php } ?>
</td></tr></table></div>

<?php }