You are here

public function GridStackLayoutDeprecated::build in GridStack 8

Build a render array for layout with regions.

Parameters

array $regions: An associative array keyed by region name, containing render arrays representing the content that should be placed in each region.

Return value

array Render array for the layout with regions.

Overrides LayoutBase::build

File

src/Layout/GridStackLayoutDeprecated.php, line 52

Class

GridStackLayoutDeprecated
Provides a GridStack class for Layout plugins.

Namespace

Drupal\gridstack\Layout

Code

public function build(array $regions) {
  $config = $this
    ->getConfiguration();
  $definition = $this
    ->getPluginDefinition();
  $id = $definition['id'];
  $item_id = 'box';
  $name = $definition['optionset'];
  $optionset = GridStack::load($name);
  $grids = $optionset
    ->getEndBreakpointGrids();
  $ipe_exists = $this->manager
    ->getModuleHandler()
    ->moduleExists('panels_ipe');

  // Only check that Panels IPE is granted. Further access check is not here.
  // @see \Drupal\gridstack\GridStackManager::preRenderGridStack()
  $config['_ipe'] = FALSE;
  if ($ipe_exists && $this->currentUser
    ->hasPermission('access panels in-place editing')) {
    $config['_ipe'] = TRUE;
  }

  // Converts string to array.
  $config['extras'] = empty($config['extras']) ? [] : Json::decode($config['extras']);

  // Defines settings.
  $settings = [
    'id' => $id,
    'item_id' => $item_id,
    'namespace' => 'gridstack',
    'optionset' => $name,
  ] + $config;

  // Converts gridstack breakpoint grids from stored JSON into array.
  $optionset
    ->gridsJsonToArray($settings);
  $items = [];
  foreach ($grids as $delta => $grid) {
    $region = 'gridstack_' . $delta;
    $box = [];
    $box_settings = $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) {
        $nested_id = $delta . '_' . $key;
        $region = 'gridstack_' . $nested_id;

        // Preserves indices even if empty.
        $box[$item_id][$key][$item_id] = isset($regions[$region]) && !Element::isEmpty($regions[$region]) ? $regions[$region] : [];
      }
    }
    else {

      // Preserves indices even if empty.
      $box[$item_id] = isset($regions[$region]) && !Element::isEmpty($regions[$region]) ? $regions[$region] : [];
    }
    $items[] = $box;
    unset($box);
  }
  $build = [
    'items' => $items,
    'optionset' => $optionset,
    'settings' => $settings,
  ];
  return empty($items) ? [] : $this->manager
    ->build($build);
}