Great API.

One problem I'm having though. Currently the first NID in the group lists the previous NID as "0" and the last NID lists the next NID as "0" and so fails when used in a theme. It would be great if it could intelligently link in a circle or "ring" and make the final NID's "next" = the first NID and the first NID's "prev" = the last NID.

CommentFileSizeAuthor
#3 cycle_or_ring-936760-3.patch2.61 KBjimmyko
Support from Acquia helps fund testing for Drupal Acquia logo

Comments

tseven’s picture

Status: Active » Needs review

Here is a workaround to achieve the circular prev/next.

Around line 420 in prev_next.module
After:

  $prev_nid = db_result(db_query("SELECT nid FROM {node} WHERE %s < '%s' AND status = 1 $cond ORDER BY %s DESC LIMIT 1",
    $search_criteria, $criteria_value, $search_criteria));

Insert:

    if (empty($next_nid)) {
        $next_nid = db_result(db_query("SELECT MIN(nid) FROM {node} WHERE status = 1 $cond LIMIT 1"));
    }
    if (empty($prev_nid)) {
        $prev_nid = db_result(db_query("SELECT MAX(next_nid) FROM {prev_next_node} LIMIT 1"));
    }

I've done some basic testing, and it seems to work fine. I have not tested it using the views functionality though.

I'm too tired at the moment to create a patch, but perhaps I will at a later date.

Anonymous’s picture

Any chance of this being in the 7.x version? would be awesome....

jimmyko’s picture

I have made this feature based on the patch I submited in #1 comment in #614592: Make indexing taxonomy aware. But the logic is similar to the one provided by tseven.

If anyone want to try this patch. Please apply the patch in #1 comment in #614592: Make indexing taxonomy aware first.

meecect’s picture

I've pasted some code below that will work. It doesn't have the bells and whistles of the patch above, ie, there are no admin screens for configuring it, but that should be a simple addition. This code goes in around line 575 or so in prev_next.module.

As soon as I get a chance, I'll roll a proper patch

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function prev_next_nid_next($nid) {
  $returnValue = db_query("SELECT next_nid FROM {prev_next_node} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
  if ($returnValue) {
    return $returnValue;
  } else {
    return _prev_next_get_first_node($nid);
    //return 0;
  }
}

/**
 * @todo Please document this function.
 * @see http://drupal.org/node/1354
 */
function prev_next_nid_prev($nid) {
  $returnValue = db_query("SELECT prev_nid FROM {prev_next_node} WHERE nid = :nid", array(':nid' => $nid))->fetchField();
  if ($returnValue) {
    return $returnValue;
  } else {
    return _prev_next_get_last_node($nid);
    //return 0;
  }
}

/*
 * Helper function to return the first node of a given type (for looping)
 */
function _prev_next_get_first_node($nid) {
  $type = db_query("SELECT type FROM {node} WHERE nid = :nid",  array(':nid' => $nid))->fetchField();
  $first = db_query("SELECT MIN(nid) FROM {node} WHERE status = 1 and type = :type", array(':type' => $type))->fetchField();
  return $first;
}

/*
 * Helper function to return the first node of a given type (for looping)
 */
function _prev_next_get_last_node($nid) {
  $type = db_query("SELECT type FROM {node} WHERE nid = :nid",  array(':nid' => $nid))->fetchField();
  $first = db_query("SELECT MAX(nid) FROM {node} WHERE status = 1 and type = :type", array(':type' => $type))->fetchField();
  return $first;
}
bhosmer’s picture

Issue summary: View changes
Status: Needs review » Closed (won't fix)

With the upcoming limited support for Drupal 6, let's move this out and take care of it in 7.

Duplicated here: https://www.drupal.org/node/1985666