You are here

protected function GridStackLayout::interpolateItems in GridStack 8.2

Interpolate data from Layout Builder to match original construct.

We do this because we don't manually put regions into templates. Instead structured with nested grids, if any, to have one template for any known layout possibility: one or two dimensional layouts. With everything being cached at D8, this shouldn't make much different than hard-coded layouts at YML files which still need parsing anyway.

1 call to GridStackLayout::interpolateItems()
GridStackLayout::build in src/Plugin/Layout/GridStackLayout.php
Build a render array for layout with regions.

File

src/Plugin/Layout/GridStackLayout.php, line 57

Class

GridStackLayout
Provides a GridStack class for Layout plugins.

Namespace

Drupal\gridstack\Plugin\Layout

Code

protected function interpolateItems($optionset, array &$settings, array $regions = []) {
  $id = 'box';
  $items = [];
  $config = isset($settings['regions']) ? $settings['regions'] : [];
  unset($settings['regions']);
  foreach (array_keys($optionset
    ->getLastBreakpoint()) as $delta) {
    $rid = GridStackDefault::regionId($delta);
    $box = [];

    // Remove top level settings to avoid leaking due to similarity.
    $box['settings'] = array_diff_key($settings, GridStackDefault::regionSettings());
    $box['settings'] = isset($config[$rid]) ? array_merge($box['settings'], $config[$rid]) : $box['settings'];
    if ($grids = $optionset
      ->getNestedGridsByDelta($delta)) {
      foreach (array_keys($grids) as $nid) {
        $rid = GridStackDefault::regionId($delta . '_' . $nid);

        // @todo recheck $box['settings'] = array_diff_key($box['settings'], GridStackDefault::regionSettings());
        // Preserves indices even if empty so to layout for Layout Builder.
        $box[$id][$nid][$id] = isset($regions[$rid]) && !Element::isEmpty($regions[$rid]) ? $regions[$rid] : [];
        $box[$id][$nid]['settings'] = isset($config[$rid]) ? array_merge($box['settings'], $config[$rid]) : $box['settings'];
      }
    }
    else {

      // Preserves indices even if empty so to layout for Layout Builder.
      $box[$id] = isset($regions[$rid]) && !Element::isEmpty($regions[$rid]) ? $regions[$rid] : [];
    }
    $items[] = $box;
    unset($box);
  }
  return $items;
}