Skip to content
Snippets Groups Projects
Commit 28cb72f3 authored by Daniele Piaggesi's avatar Daniele Piaggesi Committed by Daniele Piaggesi
Browse files

Issue #2734749 by bmeme: Port to D8 - converted...

Issue #2734749 by bmeme: Port to D8 - converted _prevnext_get_prev_and_next_nids function in service
parent ff92005b
No related branches found
No related tags found
No related merge requests found
......@@ -78,33 +78,38 @@ function prevnext_entity_extra_field_info() {
*/
function prevnext_node_view(array &$build, \Drupal\Core\Entity\EntityInterface $entity, \Drupal\Core\Entity\Display\EntityViewDisplayInterface $display, $view_mode) {
/** @var \Drupal\prevnext\PrevnextService $prevnext */
$prevnext = \Drupal::service('prevnext.service');
$prevnext->setEntity($entity);
//@todo set variable and check code.
$previous_next = $prevnext->getPreviousNext();
if ($display->getComponent('prevnext_previous')) {
$nids = _prevnext_get_prev_and_next_nids($entity);
$build['prevnext_previous'] = [
'#theme' => 'prevnext',
'#direction' => 'previous',
'#text' => t('Previous'),
'#nid' => $nids['prev'],
'#url' => Url::fromUserInput('/node/' . $nids['prev'], ['absolute' => TRUE])->toString(),
'#void' => empty($nids['prev']),
'#nid' => $previous_next['prev'],
'#url' => Url::fromUserInput('/node/' . $previous_next['prev'], ['absolute' => TRUE])->toString(),
'#void' => empty($previous_next['prev']),
];
}
if ($display->getComponent('prevnext_next')) {
$build['prevnext_next'] = [
'#theme' => 'prevnext',
'#direction' => 'next',
'#text' => t('Next'),
'#nid' => $nids['next'],
'#url' => Url::fromUserInput('/node/' . $nids['next'], ['absolute' => TRUE])->toString(),
'#void' => empty($nids['next']),
'#nid' => $previous_next['next'],
'#url' => Url::fromUserInput('/node/' . $previous_next['next'], ['absolute' => TRUE])->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-' . $entity->bundle();
}
$build['#cache']['tags'][] = 'prevnext-' . $entity->bundle();
}
/**
......
services:
prevnext.service:
class: Drupal\prevnext\PrevnextService
arguments: ["@entity.query"]
......@@ -50,14 +50,12 @@ class PrevnextSettingsForm extends ConfigFormBase {
return parent::buildForm($form, $form_state);
}
// /**
// * {@inheritdoc}
// */
// public function validateForm(array &$form, FormStateInterface $form_state) {
// if ('foo') {
// $form_state->setErrorByName('foo_element_name', t("Bar error message"));
// }
// }
/**
* {@inheritdoc}
*/
public function validateForm(array &$form, FormStateInterface $form_state) {
parent::validateForm($form, $form_state);
}
/**
* {@inheritdoc}
......
<?php
namespace Drupal\prevnext;
use \Drupal\Core\Entity\Entity;
/**
* Class PrevnextService.
*
* @package Drupal\prevnext
*/
class PrevnextService implements PrevnextServiceInterface {
/**
* @var \Drupal\Core\Entity\Entity $entity
*/
protected $entity;
/**
* @var \Drupal\Core\Entity\Query\QueryFactory $entityQuery
*/
protected $entityQuery;
/**
* Previous / Next nids
*
* @var array
*/
public $prevnext;
/**
* PrevnextService constructor.
* @param \Drupal\Core\Entity\Query\QueryFactory $query
*/
public function __construct(\Drupal\Core\Entity\Query\QueryFactory $query) {
$this->entityQuery = $query;
}
/**
* {@inheritdoc}
*/
public function setEntity(\Drupal\Core\Entity\EntityInterface $entity) {
$this->entity = $entity;
}
/**
* {@inheritdoc}
*/
public function getPreviousNext() {
$nodes = $this->getNodesOfType();
$current_nid = $this->entity->id();
$current_key = array_search($current_nid, $nodes);
$this->prevnext['prev'] = ($current_key == 0) ? '' : $nodes[$current_key - 1];
$this->prevnext['next'] = ($current_key == count($nodes) - 1) ? '' : $nodes[$current_key + 1];
return $this->prevnext;
}
/**
* Retrieves all nodes of the same type and language of given
*
* @return array $nodes
*/
protected function getNodesOfType() {
$query = $this->entityQuery->get($this->entity->getEntityTypeId());
$nodes = $query->condition('status', NODE_PUBLISHED)
->condition('type', $this->entity->bundle())
->condition('langcode', $this->entity->language()->getId())
->execute();
return array_values($nodes);
}
}
<?php
namespace Drupal\prevnext;
/**
* Interface PrevnextServiceInterface.
*
* @package Drupal\prevnext
*/
interface PrevnextServiceInterface {
/**
* Specifies the entity to handle
*
* @param \Drupal\Core\Entity\Entity $entity
*/
public function setEntity(\Drupal\Core\Entity\EntityInterface $entity);
/**
* Retrieves previous and next nids of a given node, if they exist
*
* @return array
*/
public function getPreviousNext();
}
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