From 46e06a2168b7cf828e906ff0fc2f9274322604d7 Mon Sep 17 00:00:00 2001 From: Artem Boyko Date: Wed, 1 Nov 2023 20:13:06 +0200 Subject: [PATCH] Added plugin for contextual filter for selecting 2 second level in book, used DI in Book Title block --- .../src/Plugin/Block/QuanthubBookTitle.php | 23 ++++-- .../BookSecondLevelActive.php | 74 +++++++++++++++++++ 2 files changed, 90 insertions(+), 7 deletions(-) create mode 100644 modules/quanthub_book/src/Plugin/views/argument_default/BookSecondLevelActive.php diff --git a/modules/quanthub_book/src/Plugin/Block/QuanthubBookTitle.php b/modules/quanthub_book/src/Plugin/Block/QuanthubBookTitle.php index acc521e..e2b1762 100755 --- a/modules/quanthub_book/src/Plugin/Block/QuanthubBookTitle.php +++ b/modules/quanthub_book/src/Plugin/Block/QuanthubBookTitle.php @@ -5,8 +5,8 @@ use Drupal\Core\Block\BlockBase; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\node\Entity\Node; use Drupal\node\NodeInterface; +use Drupal\node\NodeStorageInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -19,6 +19,13 @@ */ class QuanthubBookTitle extends BlockBase implements ContainerFactoryPluginInterface { + /** + * The node storage controller. + * + * @var \Drupal\node\NodeStorageInterface + */ + protected $nodeStorage; + /** * The route match service. * @@ -34,6 +41,7 @@ public static function create(ContainerInterface $container, array $configuratio $configuration, $plugin_id, $plugin_definition, + $container->get('entity_type.manager')->getStorage('node'), $container->get('current_route_match') ); } @@ -41,9 +49,10 @@ public static function create(ContainerInterface $container, array $configuratio /** * {@inheritdoc} */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, RouteMatchInterface $currentRouteMatch) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, NodeStorageInterface $node_storage, RouteMatchInterface $currentRouteMatch) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->currentRouteMatch = $currentRouteMatch; + $this->nodeStorage = $node_storage; } /** @@ -51,15 +60,15 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition */ public function build() { $node = $this->currentRouteMatch->getParameter('node'); + $build = []; if ($node instanceof NodeInterface && $node->getType() == 'book' && !empty($node->book['bid'])) { - $book_main_node = Node::load($node->book['bid']); + $this->nodeStorage->load($node->book['bid']); + $build['quanthub_book_title'] = [ + '#markup' => '

' . $book_main_node->getTitle() . '

', + ]; } - $build['quanthub_book_title'] = [ - '#markup' => '

' . $book_main_node->getTitle() . '

', - ]; - return $build; } diff --git a/modules/quanthub_book/src/Plugin/views/argument_default/BookSecondLevelActive.php b/modules/quanthub_book/src/Plugin/views/argument_default/BookSecondLevelActive.php new file mode 100644 index 0000000..30d0f6a --- /dev/null +++ b/modules/quanthub_book/src/Plugin/views/argument_default/BookSecondLevelActive.php @@ -0,0 +1,74 @@ +nodeStorage = $node_storage; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('current_route_match'), + $container->get('entity_type.manager')->getStorage('node') + ); + } + + /** + * {@inheritdoc} + */ + public function getArgument() { + // Use the argument_default_node plugin to get the nid argument. + $nid = parent::getArgument(); + if (!empty($nid)) { + $node = $this->nodeStorage->load($nid); + // Get second level active book page. + if (isset($node->book['p2']) && $node->book['p2'] > 0) { + return $node->book['p2']; + } + } + } + +}