You are here

class PrevNextHelper in Previous/Next API 8.2

Defines an PrevNextHelper service.

Hierarchy

Expanded class hierarchy of PrevNextHelper

1 string reference to 'PrevNextHelper'
prev_next.services.yml in ./prev_next.services.yml
prev_next.services.yml
1 service uses PrevNextHelper
prev_next.helper in ./prev_next.services.yml
Drupal\prev_next\PrevNextHelper

File

src/PrevNextHelper.php, line 11

Namespace

Drupal\prev_next
View source
class PrevNextHelper implements PrevNextHelperInterface {

  /**
   * The database connection.
   *
   * @var \Drupal\Core\Database\Connection
   */
  protected $database;

  /**
   * The config object.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * Constructs an PrevNextHelper object.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database connection.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The Configuration Factory.
   */
  public function __construct(Connection $database, ConfigFactoryInterface $config_factory) {
    $this->database = $database;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public function getBundleNames() {
    $bundle_names = [];
    foreach ($this->configFactory
      ->listAll('prev_next.node_type.') as $config) {
      $contents = explode('.', $config);
      $bundle_names[] = end($contents);
    }
    return $bundle_names;
  }

  /**
   * {@inheritdoc}
   */
  public function loadBundle($bundle_name) {
    return $this->configFactory
      ->get('prev_next.node_type.' . $bundle_name);
  }

  /**
   * {@inheritdoc}
   */
  public function getPrevnextId($entity_id, $op = 'next') {
    switch ($op) {
      case 'prev':
        return $this
          ->getPrevId($entity_id);
      case 'next':
        return $this
          ->getNextId($entity_id);
      default:
        return 0;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getPrevId($entity_id) {
    return $this->database
      ->query("SELECT prev_nid FROM {prev_next_node} WHERE nid = :nid", array(
      ':nid' => $entity_id,
    ))
      ->fetchField();
  }

  /**
   * {@inheritdoc}
   */
  public function getNextId($entity_id) {
    return $this->database
      ->query("SELECT next_nid FROM {prev_next_node} WHERE nid = :nid", array(
      ':nid' => $entity_id,
    ))
      ->fetchField();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
PrevNextHelper::$configFactory protected property The config object.
PrevNextHelper::$database protected property The database connection.
PrevNextHelper::getBundleNames public function Determine if connection should be refreshed. Overrides PrevNextHelperInterface::getBundleNames
PrevNextHelper::getNextId public function Callable API function to retun the next id of a given entity id. Overrides PrevNextHelperInterface::getNextId
PrevNextHelper::getPrevId public function Callable API function to retun the prev id of a given entity id. Overrides PrevNextHelperInterface::getPrevId
PrevNextHelper::getPrevnextId public function Callable API function to get the next/prev id of a given entity id. Overrides PrevNextHelperInterface::getPrevnextId
PrevNextHelper::loadBundle public function Loads the Prev/next bundle configuration. Overrides PrevNextHelperInterface::loadBundle
PrevNextHelper::__construct public function Constructs an PrevNextHelper object.