Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow block rendering using Block core Wrapper - Issue 3110310 #39

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions bamboo_twig_loader/src/TwigExtension/Render.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,31 @@ public function getName() {
* The ID of the block to render.
* @param array $params
* (optional) An array of parameters passed to the block.
* @param bool $wrapper
* (Optional) Whether or not use block template for rendering.
*
* @return null|array
* A render array for the block or NULL if the block does not exist.
*/
public function renderBlock($block_id, array $params = []) {
public function renderBlock($block_id, array $params = [], $wrapper = FALSE) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the question about the default value is still open.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to change the current working process, and so set it to false :)

$instance = $this->getPluginManagerBlock()->createInstance($block_id, $params);
return $instance->build($params);
$content = $instance->build($params);
$build = $content;

if ($wrapper) {
$build = [
'#theme' => 'block',
'#attributes' => [],
'#contextual_links' => [],
'#configuration' => $instance->getConfiguration(),
'#plugin_id' => $instance->getPluginId(),
'#base_plugin_id' => $instance->getBaseId(),
'#derivative_plugin_id' => $instance->getDerivativeId(),
'content' => $content,
];
}

return $build;
}

/**
Expand Down
81 changes: 81 additions & 0 deletions tests/src/Kernel/Loader/EntityBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

namespace Drupal\Tests\bamboo_twig\Kernel\Loader;

use Drupal\block\Entity\Block;
use Drupal\block_content\Entity\BlockContent;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\bamboo_twig\Traits\BlockCreationTrait as BambooBlockCreationTrait;

/**
* @coversDefaultClass \Drupal\bamboo_twig_loader\TwigExtension\Loader
*
* @group bamboo_twig
* @group bamboo_twig_loader
*/
class EntityBlockTest extends KernelTestBase {
use BlockCreationTrait;
use BambooBlockCreationTrait;

/**
* {@inheritdoc}
*/
public static $modules = [
'system',
'user',
'block',
'block_content',
'block_test',
'field',
'text',
'bamboo_twig',
'bamboo_twig_loader',
];

/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

$this->installEntitySchema('user');
$this->installEntitySchema('block_content');
$this->installConfig(['block_content']);
$this->installConfig(['block_test']);

$this->createBlockContentType('basic');

/** @var \Drupal\bamboo_twig_loader\TwigExtension\Loader $loaderExtension */
$this->loaderExtension = $this->container->get('bamboo_twig_loader.twig.loader');
}

/**
* @covers ::loadEntity
*
* Cover the usage of {{ bamboo_render_entity('block', 'stark_branding') }}.
*/
public function testLoaderBlockEntity() {
$entity = $this->loaderExtension->loadEntity('block', 'test_block');
$this->assertInstanceOf(Block::class, $entity);
}

/**
* @covers ::loadEntity
*
* Cover the usage of {{ bamboo_render_entity('block_content', 1) }}.
*/
public function testLoaderBlockContentEntity() {
$block = $this->createBlockContent();
$entity = $this->loaderExtension->loadEntity('block_content', $block->id());
$this->assertInstanceOf(BlockContent::class, $entity);
}

}
71 changes: 71 additions & 0 deletions tests/src/Kernel/Render/ContentBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Drupal\Tests\bamboo_twig\Kernel\Render;

use Drupal\block_content\Entity\BlockContent;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\block\Traits\BlockCreationTrait;
use Drupal\Tests\bamboo_twig\Traits\BlockCreationTrait as BambooBlockCreationTrait;

/**
* @coversDefaultClass \Drupal\bamboo_twig_loader\TwigExtension\Render
*
* @group bamboo_twig
* @group bamboo_twig_render
*/
class ContentBlockTest extends KernelTestBase {
use BlockCreationTrait;
use BambooBlockCreationTrait;

/**
* {@inheritdoc}
*/
public static $modules = [
'system',
'user',
'block',
'block_content',
'field',
'text',
'bamboo_twig',
'bamboo_twig_loader',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installEntitySchema('user');
$this->installEntitySchema('block_content');
$this->installConfig(['block_content']);

$this->createBlockContentType('basic');

/** @var \Drupal\bamboo_twig_loader\TwigExtension\Render $renderExtension */
$this->renderExtension = $this->container->get('bamboo_twig_loader.twig.render');
}

/**
* @covers ::renderBlock
*
* Cover the usage of
* {{ bamboo_render_block('block_content:ca1f2401-16a3-474b') }}.
* {{ bamboo_render_block('block_content:ca1f2401-16a3-474b', [], FALSE) }}.
*/
public function testRenderContentBlock() {
$block = $this->createBlockContent();
$this->placeBlock('block_content:' . $block->uuid());

// Ensure {{ bamboo_render_block('block_content:ca1f2401-1') }}.
$renderer = $this->renderExtension->renderBlock('block_content:' . $block->uuid(), [], FALSE);
$this->assertArrayHasKey('#block_content', $renderer);
$this->assertInstanceOf(BlockContent::class, $renderer['#block_content']);

// Ensure {{ bamboo_render_block('block_content:ca1f2401-1', [], FALSE) }}.
$renderer = $this->renderExtension->renderBlock('block_content:' . $block->uuid(), [], TRUE);
$this->assertArrayHasKey('#theme', $renderer);
$this->assertEquals('block', $renderer['#theme']);
}

}
122 changes: 122 additions & 0 deletions tests/src/Kernel/Render/PluginBlockTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php

namespace Drupal\Tests\bamboo_twig\Kernel\Render;

use Drupal\block\Entity\Block;
use Drupal\Core\Render\Markup;
use Drupal\KernelTests\KernelTestBase;

/**
* @coversDefaultClass \Drupal\bamboo_twig_loader\TwigExtension\Render
*
* @group bamboo_twig
* @group bamboo_twig_render
*/
class PluginBlockTest extends KernelTestBase {

/**
* The renderer service.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected $renderer;

/**
* {@inheritdoc}
*/
public static $modules = [
'system',
'user',
'block',
'block_test',
'bamboo_twig',
'bamboo_twig_loader',
];

/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();

/** @var \Drupal\bamboo_twig_loader\TwigExtension\Render $renderExtension */
$this->renderExtension = $this->container->get('bamboo_twig_loader.twig.render');

$this->renderer = $this->container->get('renderer');
}

/**
* @covers ::renderBlock
*
* Cover the usage of {{ bamboo_render_block('system_powered_by_block') }}.
*/
public function testRenderSystemPluginBlock() {
Block::create([
'id' => $this->randomMachineName(),
'plugin' => 'system_powered_by_block',
]);

// Ensure {{ bamboo_render_block('system_powered_by_block') }}.
$renderer = $this->renderExtension->renderBlock('system_powered_by_block', [], FALSE);
$this->assertSame(['#markup'], array_keys($renderer));
$markup = $this->renderer->renderRoot($renderer);
$this->assertEquals('<span>Powered by <a href="https://www.drupal.org">Drupal</a></span>', $markup->__toString());

// Ensure {{ bamboo_render_block('test_settings_validation', [], TRUE) }}.
$renderer = $this->renderExtension->renderBlock('system_powered_by_block', [], TRUE);
$this->assertSame([
'#theme',
'#attributes',
'#contextual_links',
'#configuration',
'#plugin_id',
'#base_plugin_id',
'#derivative_plugin_id',
'content',
], array_keys($renderer));
$markup = $this->renderer->renderRoot($renderer);
$this->assertEquals('<div role="complementary">


<span>Powered by <a href="https://www.drupal.org">Drupal</a></span>
</div>
', $markup->__toString());
}

/**
* @covers ::renderBlock
*
* Cover the usage of {{ bamboo_render_block('test_settings_validation') }}.
*/
public function testRenderCustomPluginBlock() {
// Ensure {{ bamboo_render_block('test_settings_validation') }}.
$renderer = $this->renderExtension->renderBlock('test_settings_validation');
$this->assertSame(['#markup'], array_keys($renderer));
$markup = $this->renderer->renderRoot($renderer);
$this->assertArrayHasKey('#markup', $renderer);
$this->assertInstanceOf(Markup::class, $markup);
$this->assertEquals('foo', $markup->__toString());

// Ensure {{ bamboo_render_block('test_settings_validation', [], TRUE) }}.
$renderer = $this->renderExtension->renderBlock('test_settings_validation', [], TRUE);
$this->assertSame([
'#theme',
'#attributes',
'#contextual_links',
'#configuration',
'#plugin_id',
'#base_plugin_id',
'#derivative_plugin_id',
'content',
], array_keys($renderer));
$markup = $this->renderer->renderRoot($renderer);
$this->assertInstanceOf(Markup::class, $markup);
$this->assertEquals('<div>


foo
</div>
', $markup->__toString());
}

}
Loading