You are here

prev_next_views.module in Previous/Next API 7

Same filename and directory in other branches
  1. 6 prev_next_views.module
  2. 7.2 prev_next_views.module

File

prev_next_views.module
View source
<?php

/**
 * Implements hook_prev_next_nid().
 */
function prev_next_views_prev_next_nid($nid, $op = 'next') {
  if (isset($_SESSION['prev_next_views'])) {

    // There's a prev_next views list stored for this session
    $index = array_search($nid, $_SESSION['prev_next_views']);
    if ($index !== FALSE) {

      // This node is part of the prev_next list, so return the appropriate node
      $ret = $index;
      if ($op == 'prev') {
        $ret--;
      }
      elseif ($op == 'next') {
        $ret++;
      }
      if ($ret >= 0 && $ret < count($_SESSION['prev_next_views'])) {
        return $_SESSION['prev_next_views'][$ret];
      }
      else {

        // First or last element of the view
        return 0;
      }
    }
  }
  return FALSE;
}

/**
 * Implements hook_permission().
 */
function prev_next_views_permission() {
  return array(
    'administer prev_next_views' => array(
      'title' => t('Administer Prev/Next for Views'),
      'description' => t('Perform administration tasks for Prev/Next for Views module.'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function prev_next_views_menu() {
  $items = array();
  $items['admin/config/system/prev_next/views'] = array(
    'title' => 'Views',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'prev_next_views_settings',
    ),
    'access arguments' => array(
      'administer prev_next_views',
    ),
    'type' => MENU_LOCAL_TASK,
    'weight' => 10,
    'file' => 'prev_next_views.admin.inc',
  );
  return $items;
}

/**
 * Implements hook_node_load().
 */
function prev_next_views_node_load($nodes, $types) {
  if (isset($_SESSION['prev_next_views']) && is_array($_SESSION['prev_next_views'])) {
    foreach ($nodes as $node) {
      if (array_search($node->nid, $_SESSION['prev_next_views']) === FALSE) {

        // The node being loaded is no longer part of the views results, so forget about it
        unset($_SESSION['prev_next_views']);
        break;
      }
    }
  }
}

/**
 * Implements hook_views_api().
 */
function prev_next_views_views_api() {
  return array(
    'api' => 2,
  );
}

/**
 * Implements hook_views_pre_execute().
 */
function prev_next_views_views_pre_execute(&$view) {
  if (in_array($view->name, variable_get('prev_next_views_enabled', array()))) {
    $result = $view->build_info['query']
      ->execute();
    $nids = array();
    foreach ($result as $item) {
      $nids[] = $item->nid;
    }
    $_SESSION['prev_next_views'] = $nids;
  }
}