You are here

class BlockComponentRenderArraySubscriber in Layout Builder Styles 8

Class BlockComponentRenderArraySubscriber.

Hierarchy

Expanded class hierarchy of BlockComponentRenderArraySubscriber

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

File

src/EventSubscriber/BlockComponentRenderArraySubscriber.php, line 14

Namespace

Drupal\layout_builder_styles\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.
   */
  public function __construct(EntityTypeManagerInterface $entityTypeManager, ConfigFactoryInterface $config_factory) {
    $this->entityTypeManager = $entityTypeManager;
    $this->configFactory = $config_factory;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {

    // Layout Builder also subscribes to this event to build the initial render
    // array. We use a higher weight so that we execute after it.
    $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();

    // This shouldn't happen - Layout Builder should have already created the
    // initial build data.
    if (empty($build)) {
      return;
    }
    $selectedStyles = $event
      ->getComponent()
      ->get('layout_builder_styles_style');
    if ($selectedStyles) {

      // Convert single selection to an array for consistent processing.
      if (!is_array($selectedStyles)) {
        $selectedStyles = [
          $selectedStyles,
        ];
      }

      // Pass the selected style(s) to the render array so we can use the data
      // when adding block theme suggestions.
      // See layout_builder_styles_theme_suggestions_block_alter().
      $build['#layout_builder_style'] = $selectedStyles;

      // Retrieve all styles from selection(s).
      $grouped_classes = [];
      if (!isset($build['#attributes']['class']) || !is_array($build['#attributes']['class'])) {
        $build['#attributes']['class'] = [];
      }
      foreach ($selectedStyles as $styleId) {

        // Account for incorrectly configured component configuration which may
        // have a NULL style ID. We cannot pass NULL to the storage handler or
        // it will throw an exception.
        if (empty($styleId)) {
          continue;
        }

        /** @var \Drupal\layout_builder_styles\LayoutBuilderStyleInterface $style */
        $style = $this->entityTypeManager
          ->getStorage('layout_builder_style')
          ->load($styleId);
        if ($style) {
          $classes = \preg_split('(\\r\\n|\\r|\\n)', $style
            ->getClasses());
          $grouped_classes = array_merge($grouped_classes, $classes);
          $build['#attributes']['class'] = array_merge($build['#attributes']['class'], $grouped_classes);
          $build['#cache']['tags'][] = 'config:layout_builder_styles.style.' . $style
            ->id();
        }
      }
      $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 Returns an array of event names this subscriber wants to listen to.
BlockComponentRenderArraySubscriber::onBuildRender public function Add each component's block styles to the render array.
BlockComponentRenderArraySubscriber::__construct public function BlockComponentRenderArraySubscriber constructor.