Skip to content
Snippets Groups Projects
Commit 62332ad1 authored by Viktor Holovachek's avatar Viktor Holovachek Committed by Adriano Cori
Browse files

Issue #3398355 by AstonVictor: Prevnext links in block

3398355 - Add block plugin
parent 5f1b19be
No related branches found
Tags 2.0.8
No related merge requests found
<?php
namespace Drupal\prevnext\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\prevnext\PrevNextServiceInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Url;
/**
* Block with the Previous and Next links.
*
* @Block(
* id = "prevnext_block",
* admin_label = @Translation("PrevNext links"),
* category = @Translation("Other"),
* context_definitions = {
* "node" = @ContextDefinition("entity:node", required = TRUE, label = @Translation("Node"))
* }
* )
*/
class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Returns the prevnext.service service.
*
* @var \Drupal\prevnext\PrevNextServiceInterface
*/
protected $prevnext;
/**
* Returns the config.factory service.
*
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
protected $configFactory;
/**
* Constructs a PrevNextBlock block.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin ID for the plugin instance.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\prevnext\PrevNextServiceInterface $prevnext
* Interface for the main PrevNext service file.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Defines the interface for a configuration object factory.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, PrevNextServiceInterface $prevnext, ConfigFactoryInterface $config_factory) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->prevnext = $prevnext;
$this->configFactory = $config_factory;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('prevnext.service'),
$container->get('config.factory')
);
}
/**
* {@inheritdoc}
*/
public function build() {
$build = [];
/** @var \Drupal\node\NodeInterface $node */
$node = $this->getContextValue('node');
if (!empty($node->in_preview)) {
return $build;
}
$config = $this->configFactory->get('prevnext.settings');
$node_types = $config->get('prevnext_enabled_nodetypes');
if (empty($node_types[$node->getType()])) {
return $build;
}
$previous_next = $this->prevnext->getPreviousNext($node);
$cache = [
'contexts' => [
'url',
'user.permissions',
],
'tags' => [
'config:prevnext.settings',
'node_list',
],
];
$build['prevnext_previous'] = [
'#theme' => 'prevnext',
'#direction' => 'previous',
'#text' => $this->t('Previous'),
'#nid' => $previous_next['prev'],
'#url' => Url::fromUserInput('/node/' . $previous_next['prev'])->toString(),
'#void' => empty($previous_next['prev']),
'#cache' => $cache,
];
$build['prevnext_next'] = [
'#theme' => 'prevnext',
'#direction' => 'next',
'#text' => $this->t('Next'),
'#nid' => $previous_next['next'],
'#url' => Url::fromUserInput('/node/' . $previous_next['next'])->toString(),
'#void' => empty($previous_next['next']),
'#cache' => $cache,
];
$build['#cache']['tags'][] = 'prevnext-' . $node->bundle();
return $build;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment