Skip to content
Snippets Groups Projects
Commit 680ae9f0 authored by danielveza's avatar danielveza Committed by Lee Rowlands
Browse files

Issue #3399376 by DanielVeza, larowlan: Auto attach libraries added by block/section plugins

parent a76fa461
No related branches found
No related tags found
1 merge request!1[#3399376] - Attach libraries for blocks added via LB
......@@ -9,6 +9,8 @@ use Drupal\Core\Field\FormatterPluginManager;
use Drupal\Core\Field\WidgetPluginManager;
use Drupal\Core\Layout\LayoutDefinition;
use Drupal\Core\Layout\LayoutPluginManagerInterface;
use Drupal\Core\Render\RenderContext;
use Drupal\Core\Render\RendererInterface;
use Drupal\layout_builder\Element\LayoutBuilder;
use Drupal\layout_builder\SectionStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
......@@ -46,6 +48,13 @@ final class DecoupledLayoutBuilder extends LayoutBuilder {
*/
protected WidgetPluginManager $widgetPluginManager;
/**
* Renderer.
*
* @var \Drupal\Core\Render\RendererInterface
*/
protected RendererInterface $renderer;
/**
* {@inheritdoc}
*/
......@@ -55,6 +64,7 @@ final class DecoupledLayoutBuilder extends LayoutBuilder {
$instance->formatterPluginManager = $container->get('plugin.manager.field.formatter');
$instance->widgetPluginManager = $container->get('plugin.manager.field.widget');
$instance->layoutPluginManager = $container->get('plugin.manager.core.layout');
$instance->renderer = $container->get('renderer');
return $instance;
}
......@@ -97,6 +107,17 @@ final class DecoupledLayoutBuilder extends LayoutBuilder {
$output['#attributes']['data-section-storage-type'] = $section_storage->getStorageType();
$output['#attributes']['data-section-storage'] = $section_storage->getStorageId();
$output['#cache']['max-age'] = 0;
$context = new RenderContext();
// Get all libraries attached to the sections in the storage.
foreach ($section_storage->getSections() as $section) {
$render = $section->toRenderArray($this->getPopulatedContexts($section_storage));
$this->renderer->executeInRenderContext($context, fn() => $this->renderer->render($render));
}
// Attach the collated libraries.
$output['#attached']['library'] = array_merge(
$output['#attached']['library'],
array_unique($context->pop()->getAttachments()['library']) ?? []
);
return $output;
}
......
<?php
namespace Drupal\decoupled_lb_api_test\Plugin\Block;
use Drupal\Core\Block\Attribute\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/**
* Provides a block that attaches a library.
*/
#[Block(
id: "test_attach_library_block",
admin_label: new TranslatableMarkup("Test block for attaching libraries."),
)]
class BlockWithLibrary extends BlockBase {
public function build() {
return [
'#markup' => '<p>Test for attaching libraries.</p>',
'#attached' => [
'library' => [
'core/drupal.dialog.ajax',
],
],
];
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\decoupled_lb_api\Kernel;
use Drupal\Core\Plugin\Context\Context;
use Drupal\Core\Plugin\Context\ContextDefinition;
use Drupal\Core\Plugin\Context\EntityContext;
/**
* Test that libraries are attached during LB rendering
*
* @group decoupled_lb_api
*/
final class LibrariesAttachedTest extends DecoupledLbApiKernelTestBase {
/**
* {@inheritdoc}
*/
protected static $modules = [
'decoupled_lb_api_test',
];
public function testLibrariesAttached(): void {
$this->enableLayoutBuilderForEntityTypeAndBundle('user', 'user');
$user = $this->createUser();
// And a block with a library.
$this->insertBlockIntoPageLayout($user, 'test_attach_library_block');
$storage = \Drupal::service('plugin.manager.layout_builder.section_storage');
$section_storage = $storage->load('overrides', [
'entity' => EntityContext::fromEntity($user),
'view_mode' => new Context(new ContextDefinition('string'), 'default'),
]);
// Render Layout builder.
$build = [
'#type' => 'layout_builder',
'#section_storage' => $section_storage,
];
\Drupal::service('renderer')->renderRoot($build);
// Test the library was attached.
$this->assertContains('core/drupal.dialog.ajax', $build['#attached']['library']);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment