Skip to content
Snippets Groups Projects
Commit b100e2f4 authored by Viktor Holovachek's avatar Viktor Holovachek Committed by Nikolas Costa
Browse files

Issue #3316566 by AstonVictor, cbeier: Add node title to render build

parent 37fa1cd5
Branches 2.0.x
No related tags found
No related merge requests found
...@@ -10,3 +10,12 @@ prevnext.settings: ...@@ -10,3 +10,12 @@ prevnext.settings:
prevnext_premission_check: prevnext_premission_check:
type: boolean type: boolean
label: 'Use permission check to view PrevNext links' label: 'Use permission check to view PrevNext links'
use_node_titles:
type: boolean
label: 'Show node titles as PrevNext links'
prev_link_prefix:
type: string
label: 'Previous link prefix'
next_link_suffix:
type: string
label: 'Next link suffix'
...@@ -109,10 +109,21 @@ function prevnext_node_view(array &$build, NodeInterface $node, EntityViewDispla ...@@ -109,10 +109,21 @@ function prevnext_node_view(array &$build, NodeInterface $node, EntityViewDispla
]; ];
if ($display->getComponent('prevnext_previous')) { if ($display->getComponent('prevnext_previous')) {
$previous = t('Previous');
if ($config->get('use_node_titles') &&
$previous_node = \Drupal::entityTypeManager()->getStorage('node')->load($previous_next['prev'])
) {
$previous = $previous_node->label();
if ($prefix = $config->get('prev_link_prefix')) {
$previous = "{$prefix} {$previous}";
}
}
$build['prevnext_previous'] = [ $build['prevnext_previous'] = [
'#theme' => 'prevnext', '#theme' => 'prevnext',
'#direction' => 'previous', '#direction' => 'previous',
'#text' => t('Previous'), '#text' => $previous,
'#nid' => $previous_next['prev'], '#nid' => $previous_next['prev'],
'#url' => Url::fromUserInput('/node/' . $previous_next['prev']) '#url' => Url::fromUserInput('/node/' . $previous_next['prev'])
->toString(), ->toString(),
...@@ -122,10 +133,21 @@ function prevnext_node_view(array &$build, NodeInterface $node, EntityViewDispla ...@@ -122,10 +133,21 @@ function prevnext_node_view(array &$build, NodeInterface $node, EntityViewDispla
} }
if ($display->getComponent('prevnext_next')) { if ($display->getComponent('prevnext_next')) {
$next = t('Next');
if ($config->get('use_node_titles') &&
$next_node = \Drupal::entityTypeManager()->getStorage('node')->load($previous_next['next'])
) {
$next = $next_node->label();
if ($suffix = $config->get('next_link_suffix')) {
$next = "{$next} {$suffix}";
}
}
$build['prevnext_next'] = [ $build['prevnext_next'] = [
'#theme' => 'prevnext', '#theme' => 'prevnext',
'#direction' => 'next', '#direction' => 'next',
'#text' => t('Next'), '#text' => $next,
'#nid' => $previous_next['next'], '#nid' => $previous_next['next'],
'#url' => Url::fromUserInput('/node/' . $previous_next['next']) '#url' => Url::fromUserInput('/node/' . $previous_next['next'])
->toString(), ->toString(),
......
...@@ -47,6 +47,34 @@ class PrevNextSettingsForm extends ConfigFormBase { ...@@ -47,6 +47,34 @@ class PrevNextSettingsForm extends ConfigFormBase {
'#default_value' => !empty($config->get('prevnext_premission_check')) ? $config->get('prevnext_premission_check') : FALSE, '#default_value' => !empty($config->get('prevnext_premission_check')) ? $config->get('prevnext_premission_check') : FALSE,
]; ];
$form['use_node_titles'] = [
'#title' => $this->t('Show node titles as PrevNext links'),
'#type' => 'checkbox',
'#default_value' => !empty($config->get('use_node_titles')) ? $config->get('use_node_titles') : FALSE,
];
$form['prev_link_prefix'] = [
'#title' => $this->t('Previous link prefix'),
'#type' => 'textfield',
'#default_value' => !empty($config->get('prev_link_prefix')) ? $config->get('prev_link_prefix') : '',
'#states' => [
'visible' => [
':input[name="use_node_titles"]' => ['checked' => TRUE],
],
],
];
$form['next_link_suffix'] = [
'#title' => $this->t('Next link suffix'),
'#type' => 'textfield',
'#default_value' => !empty($config->get('next_link_suffix')) ? $config->get('next_link_suffix') : '',
'#states' => [
'visible' => [
':input[name="use_node_titles"]' => ['checked' => TRUE],
],
],
];
return parent::buildForm($form, $form_state); return parent::buildForm($form, $form_state);
} }
...@@ -58,6 +86,9 @@ class PrevNextSettingsForm extends ConfigFormBase { ...@@ -58,6 +86,9 @@ class PrevNextSettingsForm extends ConfigFormBase {
$this->config('prevnext.settings') $this->config('prevnext.settings')
->set('prevnext_enabled_nodetypes', $form_state->getValue('prevnext_enabled_nodetypes')) ->set('prevnext_enabled_nodetypes', $form_state->getValue('prevnext_enabled_nodetypes'))
->set('prevnext_premission_check', (bool) $form_state->getValue('prevnext_premission_check')) ->set('prevnext_premission_check', (bool) $form_state->getValue('prevnext_premission_check'))
->set('use_node_titles', (bool) $form_state->getValue('use_node_titles'))
->set('prev_link_prefix', $form_state->getValue('prev_link_prefix'))
->set('next_link_suffix', $form_state->getValue('next_link_suffix'))
->save(); ->save();
Cache::invalidateTags(['entity_field_info']); Cache::invalidateTags(['entity_field_info']);
......
...@@ -7,6 +7,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface; ...@@ -7,6 +7,7 @@ use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\prevnext\PrevNextServiceInterface; use Drupal\prevnext\PrevNextServiceInterface;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
...@@ -39,6 +40,13 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface ...@@ -39,6 +40,13 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface
*/ */
protected $configFactory; protected $configFactory;
/**
* Returns the entity_type.manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/** /**
* Constructs a PrevNextBlock block. * Constructs a PrevNextBlock block.
* *
...@@ -52,12 +60,15 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface ...@@ -52,12 +60,15 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface
* Interface for the main PrevNext service file. * Interface for the main PrevNext service file.
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Defines the interface for a configuration object factory. * Defines the interface for a configuration object factory.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Provides an interface for entity type managers.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, PrevNextServiceInterface $prevnext, ConfigFactoryInterface $config_factory) { public function __construct(array $configuration, $plugin_id, $plugin_definition, PrevNextServiceInterface $prevnext, ConfigFactoryInterface $config_factory, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->prevnext = $prevnext; $this->prevnext = $prevnext;
$this->configFactory = $config_factory; $this->configFactory = $config_factory;
$this->entityTypeManager = $entity_type_manager;
} }
/** /**
...@@ -69,7 +80,8 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface ...@@ -69,7 +80,8 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface
$plugin_id, $plugin_id,
$plugin_definition, $plugin_definition,
$container->get('prevnext.service'), $container->get('prevnext.service'),
$container->get('config.factory') $container->get('config.factory'),
$container->get('entity_type.manager')
); );
} }
...@@ -105,20 +117,42 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface ...@@ -105,20 +117,42 @@ class PrevNextBlock extends BlockBase implements ContainerFactoryPluginInterface
], ],
]; ];
$previous = $this->t('Previous');
if ($config->get('use_node_titles') &&
$previous_node = $this->entityTypeManager->getStorage('node')->load($previous_next['prev'])
) {
$previous = $previous_node->label();
if ($prefix = $config->get('prev_link_prefix')) {
$previous = "{$prefix} {$previous}";
}
}
$build['prevnext_previous'] = [ $build['prevnext_previous'] = [
'#theme' => 'prevnext', '#theme' => 'prevnext',
'#direction' => 'previous', '#direction' => 'previous',
'#text' => $this->t('Previous'), '#text' => $previous,
'#nid' => $previous_next['prev'], '#nid' => $previous_next['prev'],
'#url' => Url::fromUserInput('/node/' . $previous_next['prev'])->toString(), '#url' => Url::fromUserInput('/node/' . $previous_next['prev'])->toString(),
'#void' => empty($previous_next['prev']), '#void' => empty($previous_next['prev']),
'#cache' => $cache, '#cache' => $cache,
]; ];
$next = $this->t('Next');
if ($config->get('use_node_titles') &&
$next_node = $this->entityTypeManager->getStorage('node')->load($previous_next['next'])
) {
$next = $next_node->label();
if ($suffix = $config->get('next_link_suffix')) {
$next = "{$next} {$suffix}";
}
}
$build['prevnext_next'] = [ $build['prevnext_next'] = [
'#theme' => 'prevnext', '#theme' => 'prevnext',
'#direction' => 'next', '#direction' => 'next',
'#text' => $this->t('Next'), '#text' => $next,
'#nid' => $previous_next['next'], '#nid' => $previous_next['next'],
'#url' => Url::fromUserInput('/node/' . $previous_next['next'])->toString(), '#url' => Url::fromUserInput('/node/' . $previous_next['next'])->toString(),
'#void' => empty($previous_next['next']), '#void' => empty($previous_next['next']),
......
...@@ -6,6 +6,7 @@ use Drupal\views\Plugin\views\field\FieldPluginBase; ...@@ -6,6 +6,7 @@ use Drupal\views\Plugin\views\field\FieldPluginBase;
use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\prevnext\PrevNextServiceInterface; use Drupal\prevnext\PrevNextServiceInterface;
use Drupal\Core\Config\ConfigFactoryInterface; use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\views\ResultRow; use Drupal\views\ResultRow;
use Drupal\Core\Url; use Drupal\Core\Url;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
...@@ -40,6 +41,13 @@ class PrevNextLinks extends FieldPluginBase { ...@@ -40,6 +41,13 @@ class PrevNextLinks extends FieldPluginBase {
*/ */
protected $currentUser; protected $currentUser;
/**
* Returns the entity_type.manager service.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/** /**
* Constructs a new PrevNextLinks instance. * Constructs a new PrevNextLinks instance.
* *
...@@ -55,13 +63,16 @@ class PrevNextLinks extends FieldPluginBase { ...@@ -55,13 +63,16 @@ class PrevNextLinks extends FieldPluginBase {
* Defines the interface for a configuration object factory. * Defines the interface for a configuration object factory.
* @param \Drupal\Core\Session\AccountInterface $current_user * @param \Drupal\Core\Session\AccountInterface $current_user
* Defines an account interface which represents the current user. * Defines an account interface which represents the current user.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* Provides an interface for entity type managers.
*/ */
public function __construct(array $configuration, $plugin_id, $plugin_definition, PrevNextServiceInterface $prevnext, ConfigFactoryInterface $config_factory, AccountInterface $current_user) { public function __construct(array $configuration, $plugin_id, $plugin_definition, PrevNextServiceInterface $prevnext, ConfigFactoryInterface $config_factory, AccountInterface $current_user, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition); parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->prevnext = $prevnext; $this->prevnext = $prevnext;
$this->configFactory = $config_factory; $this->configFactory = $config_factory;
$this->currentUser = $current_user; $this->currentUser = $current_user;
$this->entityTypeManager = $entity_type_manager;
} }
/** /**
...@@ -74,7 +85,8 @@ class PrevNextLinks extends FieldPluginBase { ...@@ -74,7 +85,8 @@ class PrevNextLinks extends FieldPluginBase {
$plugin_definition, $plugin_definition,
$container->get('prevnext.service'), $container->get('prevnext.service'),
$container->get('config.factory'), $container->get('config.factory'),
$container->get('current_user') $container->get('current_user'),
$container->get('entity_type.manager')
); );
} }
...@@ -121,20 +133,42 @@ class PrevNextLinks extends FieldPluginBase { ...@@ -121,20 +133,42 @@ class PrevNextLinks extends FieldPluginBase {
], ],
]; ];
$previous = $this->t('Previous');
if ($config->get('use_node_titles') &&
$previous_node = $this->entityTypeManager->getStorage('node')->load($previous_next['prev'])
) {
$previous = $previous_node->label();
if ($prefix = $config->get('prev_link_prefix')) {
$previous = "{$prefix} {$previous}";
}
}
$build['prevnext_previous'] = [ $build['prevnext_previous'] = [
'#theme' => 'prevnext', '#theme' => 'prevnext',
'#direction' => 'previous', '#direction' => 'previous',
'#text' => $this->t('Previous'), '#text' => $previous,
'#nid' => $previous_next['prev'], '#nid' => $previous_next['prev'],
'#url' => Url::fromUserInput('/node/' . $previous_next['prev'])->toString(), '#url' => Url::fromUserInput('/node/' . $previous_next['prev'])->toString(),
'#void' => empty($previous_next['prev']), '#void' => empty($previous_next['prev']),
'#cache' => $cache, '#cache' => $cache,
]; ];
$next = $this->t('Next');
if ($config->get('use_node_titles') &&
$next_node = $this->entityTypeManager->getStorage('node')->load($previous_next['next'])
) {
$next = $next_node->label();
if ($suffix = $config->get('next_link_suffix')) {
$next = "{$next} {$suffix}";
}
}
$build['prevnext_next'] = [ $build['prevnext_next'] = [
'#theme' => 'prevnext', '#theme' => 'prevnext',
'#direction' => 'next', '#direction' => 'next',
'#text' => $this->t('Next'), '#text' => $next,
'#nid' => $previous_next['next'], '#nid' => $previous_next['next'],
'#url' => Url::fromUserInput('/node/' . $previous_next['next'])->toString(), '#url' => Url::fromUserInput('/node/' . $previous_next['next'])->toString(),
'#void' => empty($previous_next['next']), '#void' => empty($previous_next['next']),
......
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