You are here

public function GridStack::prepareRegions in GridStack 8.2

Returns regions based on available grids.

Parameters

bool $clean: The flag to exclude region containers.

Return value

array Available regions, else empty.

Overrides GridStackInterface::prepareRegions

File

src/Entity/GridStack.php, line 426

Class

GridStack
Defines the GridStack configuration entity.

Namespace

Drupal\gridstack\Entity

Code

public function prepareRegions($clean = TRUE) {
  $regions = [];
  foreach ($this
    ->getLastBreakpoint() as $delta => $data) {
    $data = array_values($data);
    $index = $delta + 1;
    $rid = $cid = GridStackDefault::regionId($delta);
    $id = empty($data[4]) ? '' : $data[4];
    $name = $id ? ' ' . str_replace('_', ' ', Unicode::ucfirst($id)) : '';
    $fw = isset($data[2]) && $data[2] == 12;
    $regions[$rid]['id'] = $id;
    $regions[$rid]['rid'] = $rid;
    $regions[$rid]['label'] = $index . $name;
    $regions[$rid]['delta'] = $delta;
    $regions[$rid]['name'] = $name;
    $regions[$rid]['node'] = $data;
    $regions[$rid]['type'] = GridStackDefault::REGION;
    $regions[$rid]['width'] = isset($data[2]) ? $data[2] : NULL;
    $regions[$rid]['_fw'] = $fw;
    $regions[$rid]['_level'] = GridStackDefault::LEVEL_ROOT_ITEM;
    $regions[$rid]['_context'] = $delta;
    $regions[$rid]['_root'] = 'grids';

    // With nested grids, its container doesn't contain contents, but grids.
    if ($grids = $this
      ->getNestedGridsByDelta($delta)) {
      $regions[$rid]['type'] = GridStackDefault::CONTAINER;
      $regions[$rid]['_level'] = GridStackDefault::LEVEL_NESTED;

      // Remove container since the actual contents are moved, if required.
      if ($clean) {
        unset($regions[$rid]);
      }
      foreach ($grids as $nid => $grid) {
        $grid = array_values($grid);
        $id = empty($grid[4]) ? '' : $grid[4];
        $name = $id ? ' ' . str_replace('_', ' ', Unicode::ucfirst($id)) : '';
        $rid = GridStackDefault::regionId($delta . '_' . $nid);
        $regions[$rid]['id'] = $id;
        $regions[$rid]['cid'] = $cid;
        $regions[$rid]['rid'] = $rid;
        $regions[$rid]['label'] = $index . ':' . ($nid + 1) . $name;
        $regions[$rid]['delta'] = $delta;
        $regions[$rid]['name'] = $name;
        $regions[$rid]['node'] = $grid;
        $regions[$rid]['nid'] = $nid;
        $regions[$rid]['type'] = GridStackDefault::REGION;
        $regions[$rid]['width'] = isset($grid[2]) ? $grid[2] : NULL;
        $regions[$rid]['_fw'] = $fw && isset($grid[2]) && $grid[2] == 12;
        $regions[$rid]['_level'] = GridStackDefault::LEVEL_NESTED_ITEM;
        $regions[$rid]['_context'] = $index . '-' . ($nid + 1);
        $regions[$rid]['_root'] = 'nested';
      }
    }
  }
  return $regions;
}