You are here

class PrevnextService in Prevnext 2.0.x

Same name and namespace in other branches
  1. 8 src/PrevnextService.php \Drupal\prevnext\PrevnextService
  2. 2.x src/PrevnextService.php \Drupal\prevnext\PrevnextService

Class PrevnextService.

@package Drupal\prevnext

Hierarchy

Expanded class hierarchy of PrevnextService

1 string reference to 'PrevnextService'
prevnext.services.yml in ./prevnext.services.yml
prevnext.services.yml
1 service uses PrevnextService
prevnext.service in ./prevnext.services.yml
Drupal\prevnext\PrevnextService

File

src/PrevnextService.php, line 14

Namespace

Drupal\prevnext
View source
class PrevnextService implements PrevnextServiceInterface {

  /**
   * The entity manager.
   *
   * @var Drupal\Core\Entity\EntityTypeManager
   */
  protected $entityTypeManager;

  /**
   * Previous / Next nids.
   *
   * @var array
   */
  public $prevnext;

  /**
   * PrevnextService constructor.
   *
   * @param EntityTypeManager $entityTypeManager
   *   The entity type manager instance.
   */
  public function __construct(EntityTypeManager $entityTypeManager) {
    $this->entityTypeManager = $entityTypeManager;
  }

  /**
   * {@inheritdoc}
   */
  public function getPreviousNext(Node $node) {
    $nodes = $this
      ->getNodesOfType($node);
    $current_nid = $node
      ->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.
   *
   * @param \Drupal\node\Entity\Node $node
   *   The node entity.
   *
   * @return array
   *   An array of nodes filtered by type, status and language.
   */
  protected function getNodesOfType(Node $node) {
    $query = $this->entityTypeManager
      ->getStorage('node')
      ->getQuery();
    $bundle = $node
      ->bundle();
    $langcode = $node
      ->language()
      ->getId();
    $nodes = $query
      ->condition('status', NodeInterface::PUBLISHED)
      ->condition('type', $bundle)
      ->condition('langcode', $langcode)
      ->addMetaData('type', $bundle)
      ->addMetaData('langcode', $langcode)
      ->addTag('prev_next_nodes_type')
      ->execute();
    return array_values($nodes);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrevnextService::$entityTypeManager protected property The entity manager.
PrevnextService::$prevnext public property Previous / Next nids.
PrevnextService::getNodesOfType protected function Retrieves all nodes of the same type and language of given.
PrevnextService::getPreviousNext public function Retrieves previous and next nids of a given node, if they exist. Overrides PrevnextServiceInterface::getPreviousNext
PrevnextService::__construct public function PrevnextService constructor.