You are here

public function GridStackManager::preRenderGridStack in GridStack 8

Same name and namespace in other branches
  1. 8.2 src/GridStackManager.php \Drupal\gridstack\GridStackManager::preRenderGridStack()

File

src/GridStackManager.php, line 469

Class

GridStackManager
Implements GridStackManagerInterface.

Namespace

Drupal\gridstack

Code

public function preRenderGridStack($element) {
  $build = $element['#build'];
  unset($element['#build']);
  if (empty($build['items'])) {
    return [];
  }

  // Build gridstack elements.
  $defaults = GridStack::htmlSettings();
  $settings = $build['settings'] ? array_merge($defaults, $build['settings']) : $defaults;
  $optionset = $build['optionset'] ?: GridStack::load($settings['optionset']);

  // Ensures deleted optionset while being used doesn't screw up.
  if (empty($optionset)) {
    $optionset = $build['optionset'] = GridStack::load('default');
  }

  // Use static grid framework if so configured.
  $settings = array_merge($settings, $optionset
    ->getOptions('settings'));
  $is_admin = !empty($settings['_layout_builder']) || !empty($settings['_ipe']);
  $is_builder = !empty($settings['_layout_builder']) && isset($element['#attributes']) && isset($element['#attributes']['data-layout-delta']);
  $is_ipe = !empty($settings['_ipe']) && isset($element['#prefix']) && strpos($element['#prefix'], 'panels-ipe-content') !== FALSE;
  $settings['use_js'] = empty($settings['_admin']);
  $settings['framework'] = $this
    ->configLoad('framework', 'gridstack.settings');
  $settings['library'] = $is_admin ? $this
    ->configLoad('library', 'gridstack.settings') : '';
  $settings['use_framework'] = !empty($settings['framework']) && $optionset
    ->getOption('use_framework');
  $settings['_access_ipe'] = FALSE;
  if ($settings['use_framework']) {
    $settings['background'] = $settings['use_js'] = FALSE;
    $settings['use_framework'] = empty($settings['_admin']);
  }
  $attachments = $this
    ->attach($settings);
  if (empty($settings['breakpoints'])) {
    $optionset
      ->gridsJsonToArray($settings);
  }
  if (!empty($settings['root'])) {
    if (empty($build['grids'])) {
      $build['grids'] = $optionset
        ->getEndBreakpointGrids();
    }

    // Adds wrapper attributes.
    if (!empty($settings['attributes'])) {
      $element['#attributes'] = $optionset::parseAttributes($settings['attributes']);
    }

    // Adds wrapper classes.
    if (!empty($settings['use_framework'])) {
      if (!empty($settings['wrapper_classes']) && is_string($settings['wrapper_classes'])) {
        $classes = explode(' ', $settings['wrapper_classes']);
        foreach ($classes as $class) {
          $element['#attributes']['class'][] = trim($class);
        }
      }
    }
  }

  // Do not leak container attributes as already processed above.
  $settings['attributes'] = $settings['wrapper_classes'] = '';

  // Panels IPE integration.
  $regions = [];
  $children = Element::children($element);
  if (isset($element['#attached'])) {
    $build['attached'] = NestedArray::mergeDeep($build['attached'], $element['#attached']);
  }
  if ($is_admin) {

    // Panels IPE only outputs its markup for granted users.
    // Hence this acts more like #access property.
    $settings['_access_ipe'] = $is_ipe || $is_builder;
    foreach ($children as $child) {
      if ($child == 'items') {
        continue;
      }
      $regions[$child] = $element[$child];
    }
    $build['attached']['library'][] = 'gridstack/admin_base';
  }
  $element['#optionset'] = $optionset;
  $element['#grids'] = $build['grids'];
  $element['#settings'] = $build['settings'] = $settings;
  $element['#columns'] = empty($build['columns']) ? $optionset
    ->getJson('breakpoints') : $build['columns'];
  $element['#attached'] = empty($build['attached']) ? $attachments : NestedArray::mergeDeep($build['attached'], $attachments);
  $element['#items'] = $this
    ->buildItems($build, $regions);

  // CTools, Panels and Panels IPE are happy, safe to free up wasted children.
  if ($children) {
    foreach ($children as $child) {
      unset($element[$child]);
    }
  }
  return $element;
}