Skip to content

Instantly share code, notes, and snippets.

@msonnabaum
Created October 15, 2013 17:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msonnabaum/b88c73dbd5a910ee1db3 to your computer and use it in GitHub Desktop.
Save msonnabaum/b88c73dbd5a910ee1db3 to your computer and use it in GitHub Desktop.
<?php
/**
* Book Manager Service.
*/
class BookManager {
/**
* Constructs a BookManager object.
*/
public function __construct(BookStorageInterface $book_storage, EntityManager $entity_manager, TranslationInterface $translation, ConfigFactory $config_factory) {
$this->bookStorage = $book_storage;
$this->entityManager = $entity_manager;
$this->translation = $translation;
$this->configFactory = $config_factory;
}
/**
* Loads Books Array.
*/
protected function loadBooks() {
$this->books = array();
$book_links = $this->bookStorage->findAll();
if (!empty($book_links)) {
$nodes = $this->entityManager->getStorageController('node')->loadMultiple($nids);
foreach ($book_links as $link) {
$nid = $link['nid'];
if (isset($nodes[$nid]) && $nodes[$nid]->status) {
$link['href'] = $link['link_path'];
$link['options'] = unserialize($link['options']);
$link['title'] = $nodes[$nid]->label();
$link['type'] = $nodes[$nid]->bundle();
$this->books[$link['bid']] = $link;
}
}
}
}
}
class DatabaseBookStorage implements BookStorageInterface {
/**
* Constructs a BookManager object.
*/
public function __construct(Connection $connection) {
$this->connection = $connection;
}
function findAll() {
$nids = $this->connection->query("SELECT DISTINCT(bid) FROM {book}")->fetchCol();
$book_links = array();
if ($nids) {
$query = $this->connection->select('book', 'b', array('fetch' => \PDO::FETCH_ASSOC));
$query->join('menu_links', 'ml', 'b.mlid = ml.mlid');
$query->fields('b');
$query->fields('ml');
$query->condition('b.nid', $nids);
$query->orderBy('ml.weight');
$query->orderBy('ml.link_title');
$query->addTag('node_access');
$query->addMetaData('base_table', 'book');
$book_links = $query->execute();
}
return $book_links;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment