gridstack-8.x-2.5/src/GridStackBuilder.php
src/GridStackBuilder.php
<?php
namespace Drupal\gridstack;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Render\Element;
/**
* Provides layout builder or panels integration.
*/
class GridStackBuilder {
/**
* Returns admin regions.
*/
public static function adminRegions(array &$build, array $element, array $settings) {
foreach (Element::children($element) as $child) {
if ($child == 'items') {
continue;
}
$regions[$child] = $element[$child];
}
return $regions;
}
/**
* Provides Layout Builder and Panels IPE attributes if available.
*
* GridStackLayout has no knowledge of IPE, and IPE expects region keys which
* are not provided by GridStack, hence rebuild needed attributes.
*
* @param array $box
* The box being modified.
* @param array $attributes
* The attributes being modified.
* @param array $content_attributes
* The content attributes being modified.
* @param array $settings
* The settings.
* @param array $regions
* The region attributes provided by Layout Builder or Panels for admin.
* @param string $rid
* The region ID.
*/
public static function adminAttributes(array &$box, array &$attributes, array &$content_attributes, array $settings, array $regions = [], $rid = NULL) {
$region = isset($regions[$rid]) ? $regions[$rid] : [];
// Layout Builder integration.
if (!empty($settings['_layouts']) && !empty($region['#attributes']) && is_array($region['#attributes'])) {
$content_attributes = NestedArray::mergeDeep($content_attributes, $region['#attributes']);
// Provides add block and contextual links.
if (isset($region['layout_builder_add_block'])) {
$link = $region['layout_builder_add_block'];
$link['#attributes']['class'][] = 'gridstack__action';
$link['#attributes']['data-gs-region'] = $rid;
if (!empty($box)) {
if (isset($link['link'], $link['link']['#url'])) {
$params = $link['link']['#url']->getRouteParameters();
foreach (Element::children($box) as $uuid) {
$box[$uuid]['#attributes']['class'][] = 'js-layout-builder-block layout-builder-block';
$box[$uuid]['#attributes']['data-layout-block-uuid'] = $uuid;
$box[$uuid]['#attributes']['data-layout-builder-highlight-id'] = $uuid;
$box[$uuid]['#contextual_links'] = [
'layout_builder_block' => [
'route_parameters' => ['uuid' => $uuid] + $params,
'metadata' => ['operations' => 'move:update:remove'],
],
];
}
}
}
$box['add_block'] = $link;
}
}
// Panels IPE integration.
if (!empty($settings['_panels']) && !empty($region['#prefix'])) {
$box['#prefix'] = $region['#prefix'];
foreach (Element::children($box) as $bid) {
if (isset($region[$bid]['#attributes']['data-block-id'])) {
$box[$bid]['#attributes']['data-block-id'] = $region[$bid]['#attributes']['data-block-id'];
}
}
}
}
}
