GridStackLayout.php in GridStack 8.2
File
src/Plugin/Layout/GridStackLayout.php
View source
<?php
namespace Drupal\gridstack\Plugin\Layout;
use Drupal\Core\Render\Element;
use Drupal\gridstack\GridStackDefault;
use Drupal\gridstack\Entity\GridStack;
class GridStackLayout extends GridStackLayoutBase {
public function build(array $regions) {
ksort($regions);
$settings = $this
->getConfiguration();
$definition = $this
->getPluginDefinition();
$name = $definition
->get('optionset');
$optionset = GridStack::loadWithFallback($name);
$variant = $this->manager
->getEngine($settings, 'variant')
->override($optionset, $settings);
$settings['optionset'] = $name;
$settings['_stylizer'] = TRUE;
$settings = array_filter($settings);
$build = [
'items' => $this
->interpolateItems($optionset, $settings, $regions),
'optionset' => $optionset,
'settings' => $settings,
'layout' => $definition,
'variant' => $variant,
];
if ($library = $this->pluginDefinition
->getLibrary()) {
$build['attached']['library'][] = $library;
}
return $this->manager
->build($build);
}
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 = [];
$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);
$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 {
$box[$id] = isset($regions[$rid]) && !Element::isEmpty($regions[$rid]) ? $regions[$rid] : [];
}
$items[] = $box;
unset($box);
}
return $items;
}
}