Browse Source

Optional contextual argument for views blocks

merge-requests/45/head
kensae 2 years ago
parent
commit
850d83e097
  1. 7
      src/TwigTweakExtension.php
  2. 12
      src/View/BlockViewBuilder.php

7
src/TwigTweakExtension.php

@ -145,8 +145,11 @@ class TwigTweakExtension extends AbstractExtension {
/**
* Builds the render array for a block.
*/
public static function drupalBlock(string $id, array $configuration = [], bool $wrapper = TRUE): array {
return \Drupal::service('twig_tweak.block_view_builder')->build($id, $configuration, $wrapper);
public static function drupalBlock(string $id, array $configuration = [], array | string $arguments = [] bool $wrapper = TRUE): array {
if (is_string($arguments)) {
$arguments = explode(',', $arguments);
}
return \Drupal::service('twig_tweak.block_view_builder')->build($id, $configuration, $arguments, $wrapper);
}
/**

12
src/View/BlockViewBuilder.php

@ -13,6 +13,7 @@ use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\views\Plugin\Block\ViewsBlock;
use Symfony\Component\HttpFoundation\RequestStack;
/**
@ -103,7 +104,7 @@ class BlockViewBuilder {
* @return array
* A renderable array representing the content of the block.
*/
public function build(string $id, array $configuration = [], bool $wrapper = TRUE): array {
public function build(string $id, array $configuration = [], array $arguments = [], bool $wrapper = TRUE): array {
$configuration += ['label_display' => BlockPluginInterface::BLOCK_LABEL_VISIBLE];
@ -131,6 +132,15 @@ class BlockViewBuilder {
}
}
// Overrride contextual arguments if arguments parameter is being passed
// and the instance is of type ViewBlock
if ($block_plugin instanceof ViewsBlock) {
if (!empty($arguments)) {
$view = $block_plugin->getViewExecutable();
$view->args = $arguments;
}
}
// Place the content returned by the block plugin into a 'content' child
// element, as a way to allow the plugin to have complete control of its
// properties and rendering (for instance, its own #theme) without

Loading…
Cancel
Save