prevnext.module in Prevnext 8
Same filename and directory in other branches
Contains prevnext.module.
File
prevnext.moduleView source
<?php
/**
* @file
* Contains prevnext.module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Url;
use Drupal\Core\Cache\Cache;
/**
* Implements hook_help().
*/
function prevnext_help($route_name, RouteMatchInterface $route_match) {
switch ($route_name) {
// Main module help for the prevnext module.
case 'help.page.prevnext':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('Add a "Previous/Next" links to the node display.') . '</p>';
return $output;
default:
}
}
/**
* Implements hook_theme().
*/
function prevnext_theme($existing, $type, $theme, $path) {
$themes = [];
$themes['prevnext'] = [
'variables' => [
'direction' => '',
'text' => '',
'nid' => '',
'url' => '',
'void' => TRUE,
],
];
return $themes;
}
/**
* Implements hook_entity_extra_field_info().
*/
function prevnext_entity_extra_field_info() {
$extra = [];
$config = \Drupal::config('prevnext.settings');
$enabled_nodetypes = $config
->get('prevnext_enabled_nodetypes');
if (!empty($enabled_nodetypes)) {
foreach ($enabled_nodetypes as $bundle_key => $bundle_name) {
if (!empty($bundle_name)) {
$extra['node'][$bundle_key]['display']['prevnext_previous'] = [
'label' => t('Previous'),
'description' => t('Previous node indicator'),
'weight' => 50,
];
$extra['node'][$bundle_key]['display']['prevnext_next'] = [
'label' => t('Next'),
'description' => t('Next node indicator'),
'weight' => 50,
];
}
}
}
return $extra;
}
/**
* Implements hook_ENTITY_TYPE_view().
*/
function prevnext_node_view(array &$build, \Drupal\node\Entity\Node $node, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
/** @var \Drupal\prevnext\PrevnextService $prevnext */
$prevnext = \Drupal::service('prevnext.service');
$previous_next = $prevnext
->getPreviousNext($node);
if ($display
->getComponent('prevnext_previous')) {
$build['prevnext_previous'] = [
'#theme' => 'prevnext',
'#direction' => 'previous',
'#text' => t('Previous'),
'#nid' => $previous_next['prev'],
'#url' => Url::fromUserInput('/node/' . $previous_next['prev'])
->toString(),
'#void' => empty($previous_next['prev']),
];
}
if ($display
->getComponent('prevnext_next')) {
$build['prevnext_next'] = [
'#theme' => 'prevnext',
'#direction' => 'next',
'#text' => t('Next'),
'#nid' => $previous_next['next'],
'#url' => Url::fromUserInput('/node/' . $previous_next['next'])
->toString(),
'#void' => empty($previous_next['next']),
];
}
// Once these links will be cached inside the node rendered output, we will
// add a custom cache tag to allow invalidation of all these cached info
// later (for example when a new node of this type is created).
$build['#cache']['tags'][] = 'prevnext-' . $node
->bundle();
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function prevnext_node_presave(Drupal\Core\Entity\EntityInterface $entity) {
$config = \Drupal::config('prevnext.settings');
$enabled_nodetypes = $config
->get('prevnext_enabled_nodetypes');
if (in_array($entity
->bundle(), $enabled_nodetypes)) {
// We are saving a node of a type with prevnext enabled, so invalidate
// all cached rendered output of other nodes of this type with our tag.
Cache::invalidateTags([
'prevnext-' . $entity
->bundle(),
]);
}
}
Functions
Name | Description |
---|---|
prevnext_entity_extra_field_info | Implements hook_entity_extra_field_info(). |
prevnext_help | Implements hook_help(). |
prevnext_node_presave | Implements hook_ENTITY_TYPE_presave(). |
prevnext_node_view | Implements hook_ENTITY_TYPE_view(). |
prevnext_theme | Implements hook_theme(). |