Skip to content

Commit

Permalink
Added plugin for contextual filter for selecting 2 second level in bo…
Browse files Browse the repository at this point in the history
…ok, used DI in Book Title block
  • Loading branch information
artemboyko43 committed Nov 1, 2023
1 parent e754b87 commit 46e06a2
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 7 deletions.
23 changes: 16 additions & 7 deletions modules/quanthub_book/src/Plugin/Block/QuanthubBookTitle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -19,6 +19,13 @@
*/
class QuanthubBookTitle extends BlockBase implements ContainerFactoryPluginInterface {

/**
* The node storage controller.
*
* @var \Drupal\node\NodeStorageInterface
*/
protected $nodeStorage;

/**
* The route match service.
*
Expand All @@ -34,32 +41,34 @@ 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')
);
}

/**
* {@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;
}

/**
* {@inheritdoc}
*/
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' => '<h1 class="quanthub_book_title display-3 xs:header-2 page-title mb-6">' . $book_main_node->getTitle() . '</h1>',
];
}

$build['quanthub_book_title'] = [
'#markup' => '<h1 class="quanthub_book_title display-3 xs:header-2 page-title mb-6">' . $book_main_node->getTitle() . '</h1>',
];

return $build;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace Drupal\quanthub_book\Plugin\views\argument_default;

use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\node\NodeStorageInterface;
use Drupal\node\Plugin\views\argument_default\Node;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
* Default argument plugin to get the current node's second level active page.
*
* @ViewsArgumentDefault(
* id = "book_second_level_active",
* title = @Translation("Book Second level active")
* )
*/
class BookSecondLevelActive extends Node {

/**
* The node storage controller.
*
* @var \Drupal\node\NodeStorageInterface
*/
protected $nodeStorage;

/**
* Constructs a BookSecondLevelActive object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
* @param string $plugin_id
* The plugin_id for the plugin instance.
* @param array $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
* @param \Drupal\node\NodeStorageInterface $node_storage
* The node storage controller.
*/
public function __construct(array $configuration, $plugin_id, array $plugin_definition, RouteMatchInterface $route_match, NodeStorageInterface $node_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition, $route_match);
$this->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'];
}
}
}

}

0 comments on commit 46e06a2

Please sign in to comment.