View source
<?php
function prevnext_permission() {
return array(
'access prevnext settings' => array(
'title' => t('Access to Previous/Next Settings page'),
),
);
}
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;
}
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);
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'],
)),
);
}
}
}
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',
),
);
}
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;
}
}
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] : '',
);
}