You are here

public function GridStackManager::buildItems in GridStack 8

Modifies GridStack boxes to support nested grids for Bootstrap/ Foundation.

The nested grids require extra tools like DS, Panelizer, or Widget, to arrange them into their relevant container, e.g.: DS region, Widget block.

Parameters

array $build: An associative array containing:

  • items: An array of gridstack contents: text, image or media.
  • options: An array of key:value pairs of custom JS options.
  • optionset: The cached optionset object to avoid multiple invocations.
  • settings: An array of key:value pairs of HTML/layout related settings.

array $regions: The available region attributes normally provided by Panels.

Return value

array The renderable array of a GridStack instance, or empty array.

1 call to GridStackManager::buildItems()
GridStackManager::preRenderGridStack in src/GridStackManager.php

File

src/GridStackManager.php, line 267

Class

GridStackManager
Implements GridStackManagerInterface.

Namespace

Drupal\gridstack

Code

public function buildItems(array $build = [], array $regions = []) {
  $optionset = $build['optionset'];
  $items = $build['items'];
  $settings = $build['settings'];
  $grids = $optionset
    ->getEndBreakpointGrids();
  $content = [];
  $settings['wrapper'] = '';
  foreach ($items as $delta => $item) {
    $clone_box = isset($item['box']) ? $item['box'] : [];
    $attributes = isset($item['attributes']) ? $item['attributes'] : [];
    $settings = isset($item['settings']) ? array_merge($settings, $item['settings']) : $settings;
    $region = 'gridstack_' . $delta;
    $clone = [
      'box' => $clone_box,
      'caption' => isset($item['caption']) ? $item['caption'] : [],
    ];
    $settings['delta'] = $delta;
    if ($grids) {

      // Skips if more than we can chew, otherwise broken grid anyway.
      if (!isset($grids[$delta])) {
        continue;
      }

      // Layout build admin integration.
      if (isset($regions[$region]['#attributes']) && is_array($regions[$region]['#attributes'])) {
        $attributes = array_merge($attributes, $regions[$region]['#attributes']);
        $attributes['class'][] = 'layout-builder--layout__region layout__region layout__region--' . $region;
        if (isset($regions[$region]['layout_builder_add_block'])) {
          $clone['box'][] = $regions[$region]['layout_builder_add_block'];
        }
      }

      // Node contains the main grids/boxes.
      // @todo: See if to check for $settings['root'] for $nested_grids.
      $main_attributes = $this
        ->setBoxAttributes($settings, 'grids', $optionset);
      $nested_grids = $optionset
        ->getNestedGridsByDelta($delta);
      $is_nested = array_filter($nested_grids);
      $root_settings = $settings;
      $clone['attributes'] = NestedArray::mergeDeep($attributes, $main_attributes);
      $root_settings['wrapper'] = '';
      if (isset($settings['regions']) && !empty($settings['regions'][$region]['wrapper'])) {
        $root_settings['wrapper'] = $settings['regions'][$region]['wrapper'];
      }

      // Panels IPE integration only output for granted users.
      if (!empty($clone['box']) && !empty($settings['_access_ipe']) && isset($regions[$region])) {
        $this
          ->setIpeAttributes($clone['box'], $regions[$region]);
      }
      $clone['settings'] = $root_settings;

      // Nested grids/ boxes with preserved indices even if empty.
      if (!empty($is_nested)) {
        $clone['attributes']['class'][] = 'box--nester';
        $settings['root'] = FALSE;
        $settings['nested'] = $root_settings['nested'] = TRUE;
        $settings['nester'] = FALSE;
        $clone['settings'] = $root_settings;
        $nested = $clone_box;

        // The nested elements.
        if (!empty($nested) && isset($nested[0]['box'])) {
          foreach ($nested_grids as $nid => $nested_grid) {
            $nested_region = $delta . '_' . $nid;
            $region = 'gridstack_' . $nested_region;

            // Panels IPE integration only output for granted users.
            if (!empty($nested[$nid]['box']) && !empty($settings['_access_ipe']) && isset($regions[$region])) {
              $this
                ->setIpeAttributes($nested[$nid]['box'], $regions[$region]);
            }
            $settings['nested_delta'] = $nid;
            $nested[$nid]['settings'] = isset($nested[$nid]['settings']) ? array_merge($settings, $nested[$nid]['settings']) : $settings;
            $nested[$nid]['settings']['wrapper'] = '';
            if (isset($settings['regions'][$region]) && !empty($settings['regions'][$region]['wrapper'])) {
              $nested[$nid]['settings']['wrapper'] = $settings['regions'][$region]['wrapper'];
            }
            $nested[$nid]['settings']['nested_id'] = $delta + 1 . '-' . ($nid + 1);
            $nested_attributes = isset($nested[$nid]['attributes']) ? $nested[$nid]['attributes'] : [];

            // Layout build admin integration.
            if (isset($regions[$region]['#attributes']) && is_array($regions[$region]['#attributes'])) {
              $nested_attributes = array_merge($nested_attributes, $regions[$region]['#attributes']);
              $nested_attributes['class'][] = 'layout-builder--layout__region layout__region layout__region--' . $region;
              if (isset($regions[$region]['layout_builder_add_block'])) {
                $nested[$nid]['box'][] = $regions[$region]['layout_builder_add_block'];
              }
            }
            $nested[$nid]['attributes'] = NestedArray::mergeDeep($nested_attributes, $this
              ->setBoxAttributes($settings, 'nested', $optionset));
            $nested[$nid]['attributes']['class'][] = 'box--nested';
          }

          // Adds prefix for admin actions, etc.
          $box = [];
          if (isset($item['prefix'])) {
            $box['prefix'] = $item['prefix'];
          }

          // Provides nested gridstack if so configured.
          $box['content'] = [
            '#theme' => 'gridstack',
            '#items' => $nested,
            '#grids' => $nested_grids,
            '#optionset' => $optionset,
            '#settings' => $settings,
            '#attributes' => isset($item['wrapper_attributes']) ? $item['wrapper_attributes'] : [],
          ];
          $clone['box'] = $box;
          $clone['settings'] = $root_settings;
        }
      }
    }
    $content[] = $clone;
    unset($clone);
  }
  return $content;
}