Newer
Older
Dries Buytaert
committed
<?php
namespace Drupal\book\Plugin\Block;
Dries Buytaert
committed
use Drupal\Core\Block\BlockBase;
use Drupal\book\BookManagerInterface;
use Drupal\Core\Cache\Cache;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
catch
committed
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\node\NodeInterface;
catch
committed
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
Alex Pott
committed
use Drupal\Core\Entity\EntityStorageInterface;
Dries Buytaert
committed
/**
* Provides a 'Book navigation' block.
*
* @Block(
Dries Buytaert
committed
* id = "book_navigation",
* admin_label = @Translation("Book navigation"),
* category = @Translation("Menus")
Dries Buytaert
committed
* )
*/
catch
committed
class BookNavigationBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The request object.
*
* @var \Symfony\Component\HttpFoundation\RequestStack
catch
committed
*/
protected $requestStack;
catch
committed
/**
* The book manager.
*
* @var \Drupal\book\BookManagerInterface
*/
protected $bookManager;
Alex Pott
committed
/**
* The node storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $nodeStorage;
catch
committed
/**
* Constructs a new BookNavigationBlock instance.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param mixed $plugin_definition
catch
committed
* The plugin implementation definition.
* @param \Symfony\Component\HttpFoundation\RequestStack $request_stack
* The request stack object.
* @param \Drupal\book\BookManagerInterface $book_manager
* The book manager.
Alex Pott
committed
* @param \Drupal\Core\Entity\EntityStorageInterface $node_storage
* The node storage.
catch
committed
*/
Alex Pott
committed
public function __construct(array $configuration, $plugin_id, $plugin_definition, RequestStack $request_stack, BookManagerInterface $book_manager, EntityStorageInterface $node_storage) {
catch
committed
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->requestStack = $request_stack;
$this->bookManager = $book_manager;
Alex Pott
committed
$this->nodeStorage = $node_storage;
catch
committed
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
catch
committed
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('request_stack'),
Alex Pott
committed
$container->get('book.manager'),
$container->get('entity.manager')->getStorage('node')
catch
committed
);
}
Dries Buytaert
committed
/**
* {@inheritdoc}
Dries Buytaert
committed
*/
public function defaultConfiguration() {
return [
Dries Buytaert
committed
'block_mode' => "all pages",
];
Dries Buytaert
committed
}
/**
Dries Buytaert
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
public function blockForm($form, FormStateInterface $form_state) {
$options = [
Alex Pott
committed
'all pages' => $this->t('Show block on all pages'),
'book pages' => $this->t('Show block only on book pages'),
];
$form['book_block_mode'] = [
Dries Buytaert
committed
'#type' => 'radios',
Alex Pott
committed
'#title' => $this->t('Book navigation block display'),
Dries Buytaert
committed
'#options' => $options,
'#default_value' => $this->configuration['block_mode'],
Alex Pott
committed
'#description' => $this->t("If <em>Show block on all pages</em> is selected, the block will contain the automatically generated menus for all of the site's books. If <em>Show block only on book pages</em> is selected, the block will contain only the one menu corresponding to the current page's book. In this case, if the current page is not in a book, no block will be displayed. The <em>Page specific visibility settings</em> or other visibility settings can be used in addition to selectively display this block."),
];
Dries Buytaert
committed
return $form;
}
/**
Dries Buytaert
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
Dries Buytaert
committed
public function blockSubmit($form, FormStateInterface $form_state) {
Alex Pott
committed
$this->configuration['block_mode'] = $form_state->getValue('book_block_mode');
Dries Buytaert
committed
}
/**
Alex Pott
committed
* {@inheritdoc}
Dries Buytaert
committed
*/
Alex Pott
committed
public function build() {
Dries Buytaert
committed
$current_bid = 0;
if ($node = $this->requestStack->getCurrentRequest()->get('node')) {
Dries Buytaert
committed
$current_bid = empty($node->book['bid']) ? 0 : $node->book['bid'];
}
if ($this->configuration['block_mode'] == 'all pages') {
$book_menus = [];
$pseudo_tree = [0 => ['below' => FALSE]];
foreach ($this->bookManager->getAllBooks() as $book_id => $book) {
Dries Buytaert
committed
if ($book['bid'] == $current_bid) {
// If the current page is a node associated with a book, the menu
// needs to be retrieved.
$data = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
$book_menus[$book_id] = $this->bookManager->bookTreeOutput($data);
Dries Buytaert
committed
}
else {
// Since we know we will only display a link to the top node, there
// is no reason to run an additional menu tree query for each book.
$book['in_active_trail'] = FALSE;
// Check whether user can access the book link.
Alex Pott
committed
$book_node = $this->nodeStorage->load($book['nid']);
$book['access'] = $book_node->access('view');
Dries Buytaert
committed
$pseudo_tree[0]['link'] = $book;
$book_menus[$book_id] = $this->bookManager->bookTreeOutput($pseudo_tree);
Dries Buytaert
committed
}
$book_menus[$book_id] += [
Angie Byron
committed
'#book_title' => $book['title'],
];
Dries Buytaert
committed
}
if ($book_menus) {
return [
Dries Buytaert
committed
'#theme' => 'book_all_books_block',
] + $book_menus;
Dries Buytaert
committed
}
}
elseif ($current_bid) {
// Only display this block when the user is browsing a book and do
// not show unpublished books.
$nid = \Drupal::entityQuery('node')
->condition('nid', $node->book['bid'], '=')
->condition('status', NodeInterface::PUBLISHED)
->execute();
Alex Pott
committed
Dries Buytaert
committed
// Only show the block if the user has view access for the top-level node.
if ($nid) {
$tree = $this->bookManager->bookTreeAllData($node->book['bid'], $node->book);
Dries Buytaert
committed
// There should only be one element at the top level.
$data = array_shift($tree);
$below = $this->bookManager->bookTreeOutput($data['below']);
Dries Buytaert
committed
if (!empty($below)) {
return $below;
Dries Buytaert
committed
}
}
}
return [];
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
catch
committed
public function getCacheContexts() {
Alex Pott
committed
return Cache::mergeContexts(parent::getCacheContexts(), ['route.book_navigation']);
}
catch
committed
/**
* {@inheritdoc}
*
* @todo Make cacheable in https://www.drupal.org/node/2483181
catch
committed
*/
public function getCacheMaxAge() {
return 0;
}