You are here

class BlockComponentRenderArraySubscriber in Layout Builder Blocks 1.0.x

Class BlockComponentRenderArraySubscriber.

Hierarchy

Expanded class hierarchy of BlockComponentRenderArraySubscriber

1 string reference to 'BlockComponentRenderArraySubscriber'
layout_builder_blocks.services.yml in ./layout_builder_blocks.services.yml
layout_builder_blocks.services.yml
1 service uses BlockComponentRenderArraySubscriber
layout_builder_blocks.render_block_component_subscriber in ./layout_builder_blocks.services.yml
Drupal\layout_builder_blocks\EventSubscriber\BlockComponentRenderArraySubscriber

File

src/EventSubscriber/BlockComponentRenderArraySubscriber.php, line 15

Namespace

Drupal\layout_builder_blocks\EventSubscriber
View source
class BlockComponentRenderArraySubscriber implements EventSubscriberInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Drupal\Core\Config\ConfigFactoryInterface definition.
   *
   * @var \Drupal\Core\Config\ConfigFactoryInterface
   */
  protected $configFactory;

  /**
   * BlockComponentRenderArraySubscriber constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Access configuration.
   * @param \Drupal\bootstrap_styles\StylesGroup\StylesGroupManager $styles_group_manager
   *   The styles group plugin manager.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory, StylesGroupManager $styles_group_manager) {
    $this->entityTypeManager = $entityTypeManager;
    $this->configFactory = $config_factory;
    $this->stylesGroupManager = $styles_group_manager;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[LayoutBuilderEvents::SECTION_COMPONENT_BUILD_RENDER_ARRAY] = [
      'onBuildRender',
      50,
    ];
    return $events;
  }

  /**
   * Add each component's block styles to the render array.
   *
   * @param \Drupal\layout_builder\Event\SectionComponentBuildRenderArrayEvent $event
   *   The section component render event.
   */
  public function onBuildRender(SectionComponentBuildRenderArrayEvent $event) {
    $build = $event
      ->getBuild();
    $block_style_configs = [];
    if ($bootstrap_styles = $event
      ->getComponent()
      ->get('bootstrap_styles')) {
      if (isset($bootstrap_styles['block_style'])) {
        $block_style_configs = $bootstrap_styles['block_style'];
      }
    }

    // Build dynamic styles.
    if (count($block_style_configs) > 0) {
      $build = $this->stylesGroupManager
        ->buildStyles($build, $block_style_configs, NULL);
    }
    $event
      ->setBuild($build);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BlockComponentRenderArraySubscriber::$configFactory protected property Drupal\Core\Config\ConfigFactoryInterface definition.
BlockComponentRenderArraySubscriber::$entityTypeManager protected property The entity type manager.
BlockComponentRenderArraySubscriber::getSubscribedEvents public static function
BlockComponentRenderArraySubscriber::onBuildRender public function Add each component's block styles to the render array.
BlockComponentRenderArraySubscriber::__construct public function BlockComponentRenderArraySubscriber constructor.