You are here

prevnext.module in Prevnext 7

Same filename and directory in other branches
  1. 8 prevnext.module
  2. 2.x prevnext.module
  3. 2.0.x prevnext.module

File

prevnext.module
View source
<?php

/**
 * @file
 *
 * prevnext.module
 */

/**
 * Implements hook_permission().
 */
function prevnext_permission() {
  return array(
    'access prevnext settings' => array(
      'title' => t('Access to Previous/Next Settings page'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function prevnext_menu() {
  $items = array();
  $items['admin/config/user-interface/prevnext'] = array(
    'title' => 'Previous/Next',
    'description' => 'Enable bundles that will use previous/next',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'prevnext_settings',
    ),
    'access arguments' => array(
      'access prevnext settings',
    ),
    'file' => 'prevnext.admin.inc',
    'file path' => drupal_get_path('module', 'prevnext') . '/include',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Implements hook_node_view().
 */
function prevnext_node_view($node, $view_mode, $langcode) {
  $enabled_nodetypes = array_filter(variable_get('prevnext_enabled_nodetypes', array()));
  if (in_array($node->type, $enabled_nodetypes)) {
    $extrafields = field_extra_fields_get_display('node', $node->type, $view_mode);
    $language = NULL;
    if (module_exists('i18n_node')) {
      $mode = variable_get('language_content_type_' . $node->type, 0);
      $language = $mode != 0 ? $langcode : NULL;
    }
    $prevnext = _prevnext_get_prevnext($node, $language);

    // @todo: Change theme() with a renderable array.
    if (isset($extrafields['prevnext_previous']) && !empty($extrafields['prevnext_previous']['visible']) && !empty($prevnext['prev'])) {
      $node->content['prevnext_previous'] = array(
        '#markup' => theme('prevnext_previous', array(
          'previous' => 'node/' . $prevnext['prev'],
        )),
      );
    }
    if (isset($extrafields['prevnext_next']) && !empty($extrafields['prevnext_next']['visible']) && !empty($prevnext['next'])) {
      $node->content['prevnext_next'] = array(
        '#markup' => theme('prevnext_next', array(
          'next' => 'node/' . $prevnext['next'],
        )),
      );
    }
  }
}

/**
 * Implements hook_theme().
 */
function prevnext_theme($existing, $type, $theme, $path) {
  return array(
    'prevnext_previous' => array(
      'variables' => array(
        'previous' => NULL,
      ),
      'template' => 'prevnext-previous',
      'path' => $path . '/templates',
    ),
    'prevnext_next' => array(
      'variables' => array(
        'next' => NULL,
      ),
      'template' => 'prevnext-next',
      'path' => $path . '/templates',
    ),
  );
}

/**
 * Implements hook_field_extra_fields().
 */
function prevnext_field_extra_fields() {
  $enabled_nodetypes = array_filter(variable_get('prevnext_enabled_nodetypes', array()));
  if (!empty($enabled_nodetypes)) {
    $extra = array();
    foreach ($enabled_nodetypes as $key => $value) {
      $extra['node'][$key]['display']['prevnext_previous'] = array(
        'label' => t('Previous'),
        'description' => t('Previous node indicator'),
        'weight' => 50,
      );
      $extra['node'][$key]['display']['prevnext_next'] = array(
        'label' => t('Next'),
        'description' => t('Next node indicator'),
        'weight' => 50,
      );
    }
    return $extra;
  }
}

/**
 * Helper: Retrieve previous/next nodes
 */
function _prevnext_get_prevnext($node, $language = NULL) {
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', $node->type)
    ->propertyCondition('status', 1);
  if (!is_null($language)) {
    $query
      ->propertyCondition('language', $language);
  }
  $result = $query
    ->execute();
  $nids = array();
  foreach ($result['node'] as $result_nids) {
    $nids[] = $result_nids->nid;
  }
  $key = array_search($node->nid, $nids);
  return array(
    'prev' => !empty($nids[$key - 1]) ? $nids[$key - 1] : '',
    'next' => !empty($nids[$key + 1]) ? $nids[$key + 1] : '',
  );
}

Functions

Namesort descending Description
prevnext_field_extra_fields Implements hook_field_extra_fields().
prevnext_menu Implements hook_menu().
prevnext_node_view Implements hook_node_view().
prevnext_permission Implements hook_permission().
prevnext_theme Implements hook_theme().
_prevnext_get_prevnext Helper: Retrieve previous/next nodes