gridstack-8.x-2.5/src/Plugin/Layout/GridStackLayout.php
src/Plugin/Layout/GridStackLayout.php
<?php
namespace Drupal\gridstack\Plugin\Layout;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Render\Element;
use Drupal\gridstack\GridStackDefault;
use Drupal\gridstack\Entity\GridStack;
/**
* Provides a GridStack class for Layout plugins.
*/
class GridStackLayout extends GridStackLayoutBase {
/**
* {@inheritdoc}
*/
public function build(array $regions) {
$config = $this->getConfiguration();
$definition = $this->getPluginDefinition();
$name = $definition->get('optionset');
$optionset = GridStack::loadWithFallback($name);
$grids = $optionset->getEndBreakpointGrids();
$ipe_exists = $this->manager->getModuleHandler()->moduleExists('panels_ipe');
$lbr_exists = $this->manager->getModuleHandler()->moduleExists('layout_builder');
// Only check that Panels IPE is granted. Further access check is not here.
// @see \Drupal\gridstack\GridStackManager::preRenderGridStack()
$settings = [
'optionset' => $name,
'extras' => empty($config['extras']) ? [] : Json::decode($config['extras']),
'_panels' => $ipe_exists && $this->currentUser->hasPermission('access panels in-place editing'),
'_layouts' => $lbr_exists && $this->currentUser->hasPermission('configure any layout'),
] + GridStackDefault::fixedSettings() + $config;
// Converts gridstack breakpoint grids from stored JSON into array.
// @todo $optionset->gridsJsonToArray($settings);
ksort($regions);
$item_id = $settings['item_id'];
$items = [];
foreach ($grids as $delta => $grid) {
$rid = GridStackDefault::regionId($delta);
$box = [];
$box_settings = $settings;
// Remove top level attributes settings.
unset($box_settings['attributes'], $box_settings['wrapper'], $box_settings['wrapper_classes']);
$box['settings'] = $box_settings;
$nested_grids = $optionset->getNestedGridsByDelta($delta);
$is_nested = array_filter($nested_grids);
if (!empty($is_nested)) {
foreach ($nested_grids as $key => $nested_grid) {
$rid = GridStackDefault::regionId($delta . '_' . $key);
// Preserves indices even if empty.
$box[$item_id][$key][$item_id] = isset($regions[$rid]) && !Element::isEmpty($regions[$rid]) ? $regions[$rid] : [];
}
}
else {
// Preserves indices even if empty.
$box[$item_id] = isset($regions[$rid]) && !Element::isEmpty($regions[$rid]) ? $regions[$rid] : [];
}
$items[] = $box;
unset($box);
}
$build = [
'items' => $items,
'optionset' => $optionset,
'settings' => $settings,
'layout' => $definition,
];
return $this->manager->build($build);
}
}
