You are here

ui_patterns_layout_builder.module in UI Patterns Layout Builder 8

UI Patterns Layout Builder module file.

File

ui_patterns_layout_builder.module
View source
<?php

/**
 * @file
 * UI Patterns Layout Builder module file.
 */
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\ui_patterns\UiPatterns;
use Drupal\ui_patterns_layouts\Plugin\Layout\PatternLayout;
use Drupal\Core\Render\Element;

/**
 * Implements hook_element_info_alter().
 */
function ui_patterns_layout_builder_element_info_alter(array &$info) {
  if (isset($info['pattern'])) {
    $info['pattern']['#pre_render'][] = [
      "Drupal\\ui_patterns_layout_builder\\Element\\PatternLayoutBuilder",
      "processLayoutBuilderRegions",
    ];
  }
}

/**
 * Implements hook_layout_alter().
 */
function ui_patterns_layout_builder_layout_alter(&$definitions) {
  foreach (UiPatterns::getPatternDefinitions() as $pattern_definition) {
    if (isset($definitions['pattern_' . $pattern_definition
      ->id()])) {

      /** @var \Drupal\Core\Layout\LayoutDefinition $layout */
      $layout = $definitions['pattern_' . $pattern_definition
        ->id()];
      $additional = $pattern_definition
        ->getAdditional();
      if (isset($additional['icon_map'])) {
        $layout
          ->setIconMap($additional['icon_map']);
      }
      if (isset($additional['icon_path'])) {
        $layout
          ->setIconPath($additional['icon_path']);
      }
      $layout
        ->setClass('\\Drupal\\ui_patterns_layout_builder\\Plugin\\Layout\\PatternLayoutBuilder');
    }
  }
}

/**
 * Implements hook_entity_view_alter().
 */
function ui_patterns_layout_builder_entity_view_alter(array &$build, EntityInterface $entity, EntityViewDisplayInterface $display) {
  if (isset($build['_layout_builder'])) {
    $layout_builder =& $build['_layout_builder'];
    foreach (Element::children($layout_builder) as $key) {
      $child =& $layout_builder[$key];
      if (isset($child['#layout'])) {

        /** @var \Drupal\ui_patterns_layouts\Plugin\Layout\PatternLayout $layout */
        $layout = $child['#layout'];
        if ($layout instanceof PatternLayout) {
          $child['#context'] = [
            'type' => 'layout',
            'entity_type' => $entity
              ->getEntityTypeId(),
            'bundle' => $entity
              ->bundle(),
            'view_mode' => $build['#view_mode'],
            'entity_id' => $entity
              ->id(),
            'entity' => $entity,
          ];
        }
      }
    }
  }
}

/**
 * Implements hook_builder_module_implements_alter().
 */
function ui_patterns_layout_builder_module_implements_alter(&$implementations, $hook) {
  if ($hook === 'layout_alter') {

    // Ensure that this module's implementation of hook_layout_alter() runs
    // last so that ui_patterns_layout run before it.
    $group = $implementations['ui_patterns_layout_builder'];
    unset($implementations['ui_patterns_layout_builder']);
    $implementations['ui_patterns_layout_builder'] = $group;
  }
}

/**
 * Implements hook_theme_registry_alter().
 *
 * Add region_attributes to the pattern theme.
 */
function ui_patterns_layout_builder_theme_registry_alter(&$theme_registry) {
  foreach (UiPatterns::getManager()
    ->getPatterns() as $pattern) {
    $definition = $pattern
      ->getPluginDefinition();
    if (isset($theme_registry['pattern_' . $definition
      ->id()])) {
      $theme_registry['pattern_' . $definition
        ->id()]['variables']['region_attributes'] = [];
    }
  }
}