You are here

function block_style_plugins_preprocess_layout in Block Style Plugins 8.2

Implements hook_preprocess_layout().

@todo Replace this with https://www.drupal.org/project/drupal/issues/3062862 Until this is done, only Nodes will be supported.

File

./block_style_plugins.module, line 121
Contains block_style_plugins.module.

Code

function block_style_plugins_preprocess_layout(&$variables) {
  $block_styles = NULL;

  /** @var \Drupal\layout_builder\SectionStorage\SectionStorageInterface */
  $section_storage = \Drupal::routeMatch()
    ->getParameters()
    ->get('section_storage');

  /** @var \Drupal\node\Entity\Node */
  $node = \Drupal::routeMatch()
    ->getParameters()
    ->get('node');
  if ($section_storage && isset($variables['content']['#attributes']['data-layout-delta'])) {

    // Cast to string since the attribute may be an AttributeValue object and
    // getSection() expects a numeric value.
    $delta = (string) $variables['content']['#attributes']['data-layout-delta'];

    /** @var \Drupal\layout_builder\Section $section */
    $section = $section_storage
      ->getSection($delta);
  }
  elseif ($node) {

    // Remove attributes so we have just the content array.
    $content = array_filter($variables['content'], function ($key) {
      return substr($key, 0, 1) !== '#';
    }, ARRAY_FILTER_USE_KEY);

    // Exit if the current section has no content.
    if (empty($content)) {
      return;
    }

    /** @var \Drupal\layout_builder\Field\LayoutSectionItemList $layout */
    $layout = $node
      ->get('layout_builder__layout');
    $sections = $layout
      ->getSections();

    // Find the current section being processed by comparing its components to
    // the ones in storage.
    foreach ($sections as $possible_section) {
      $component_keys = array_keys($possible_section
        ->getComponents());
      foreach ($content as $content_data) {
        $content_keys = array_filter(array_keys($content_data), function ($id) {

          // Remove attributes.
          return substr($id, 0, 1) !== '#';
        });

        // Check if the current section is the one being preprocessed.
        if ($component_keys == $content_keys) {
          $section = $possible_section;
          break 2;
        }
      }
    }
  }
  if (empty($section)) {
    return;
  }
  $block_styles = $section
    ->getThirdPartySettings('block_style_plugins');
  if ($block_styles) {

    // Retrieve a list of style plugin definitions.

    /** @var Drupal\block_style_plugins\Plugin\BlockStyleManager $plugin_manager */
    $plugin_manager = \Drupal::service('plugin.manager.block_style.processor');
    $available_plugins = $plugin_manager
      ->getSectionDefinitions();
    foreach ($block_styles as $name => $configuration) {

      // Only instantiate plugins that are available.
      if (array_key_exists($name, $available_plugins)) {

        /** @var \Drupal\block_style_plugins\Plugin\BlockStyleInterface $plugin */
        $plugin = $plugin_manager
          ->createInstance($name, $configuration);
        $variables = $plugin
          ->build($variables);
      }
    }
  }
}