entity_reference_hierarchy_book_nav-1.0.0/src/Plugin/Block/BookBlockBase.php
src/Plugin/Block/BookBlockBase.php
<?php
declare(strict_types=1);
namespace Drupal\entity_reference_hierarchy_book_nav\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\entity_reference_hierarchy_book_nav\Book;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a book block base.
*/
abstract class BookBlockBase extends BlockBase implements ContainerFactoryPluginInterface {
/**
* Constructs the plugin instance.
*/
public function __construct(
array $configuration,
$plugin_id,
$plugin_definition,
protected Book $book,
) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition): self {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container->get('entity_reference_hierarchy_book_nav.book'),
);
}
/**
* {@inheritdoc}
*/
public function build(): array {
$node = $this->getContextValue('node');
if (!$node) {
return [];
}
$book = $this->book->getBook($node);
if (!$book) {
return [];
}
return $this->bookBuild($build, $node, $book);
}
/**
* {@inheritdoc}
*/
public function getCacheContexts() {
return Cache::mergeContexts(parent::getCacheContexts(), ['url.path', 'languages']);
}
/**
* {@inheritdoc}
*/
public function getCacheMaxAge() {
return 1;
}
/**
* Generate the block contents.
*/
abstract public function bookBuild(&$build, $page, $book);
}
