You are here

public function GridStackLayout::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 LayoutDefault::build

File

src/Layout/GridStackLayout.php, line 51

Class

GridStackLayout
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
    ->get('optionset');
  $optionset = GridStack::load($name);
  $grids = $optionset
    ->getEndBreakpointGrids();
  $ipe_exists = $this->manager
    ->getModuleHandler()
    ->moduleExists('panels_ipe');
  $is_builder = $this->manager
    ->getModuleHandler()
    ->moduleExists('layout_builder');

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

  /** @var \Drupal\Core\Routing\AdminContext $admin_context */

  // @todo $admin_context = \Drupal::service('router.admin_context');
  // @todo $admin_context->isAdminRoute()
  if ($is_builder && $this->currentUser
    ->hasPermission('configure any layout')) {
    $config['_layout_builder'] = 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,
    'layout' => $definition,
  ];
  return empty($items) ? [] : $this->manager
    ->build($build);
}