SectionStyles.php in Block Style Plugins 8.2
File
src/EventSubscriber/SectionStyles.php
View source
<?php
namespace Drupal\block_style_plugins\EventSubscriber;
use Drupal\block_style_plugins\Plugin\BlockStyleManager;
use Drupal\layout_builder\Event\SectionBuildRenderArrayEvent;
use Drupal\layout_builder\LayoutBuilderEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class SectionStyles implements EventSubscriberInterface {
protected $blockStyleManager;
public function __construct(BlockStyleManager $blockStyleManager) {
$this->blockStyleManager = $blockStyleManager;
}
public static function getSubscribedEvents() {
$events = [];
if (class_exists('\\Drupal\\layout_builder\\LayoutBuilderEvents')) {
$events[LayoutBuilderEvents::SECTION_BUILD_RENDER_ARRAY] = 'onBuildRender';
}
return $events;
}
public function onBuildRender(SectionBuildRenderArrayEvent $event) {
$block_styles = $event
->getSection()
->getThirdPartySettings('block_style_plugins');
if ($block_styles) {
$build = $event
->getBuild();
$available_plugins = $this->blockStyleManager
->getSectionDefinitions();
foreach ($block_styles as $plugin_id => $configuration) {
if (array_key_exists($plugin_id, $available_plugins)) {
$plugin = $this->blockStyleManager
->createInstance($plugin_id, $configuration);
$build = $plugin
->build($build);
}
}
$event
->setBuild($build);
}
}
}