You are here

public function GridStackManager::build in GridStack 8

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

Returns a cacheable renderable array of a single gridstack instance.

Parameters

array $build: An associative array containing:

  • items: An array of gridstack contents: text, image or media.
  • options: An array of key:value pairs of custom JS options.
  • optionset: The cached optionset object to avoid multiple invocations.
  • settings: An array of key:value pairs of HTML/layout related settings.

Return value

array The cacheable renderable array of a gridstack instance, or empty array.

Overrides GridStackManagerInterface::build

File

src/GridStackManager.php, line 406

Class

GridStackManager
Implements GridStackManagerInterface.

Namespace

Drupal\gridstack

Code

public function build(array $build = []) {
  foreach ([
    'attached',
    'grids',
    'items',
    'optionset',
    'settings',
  ] as $key) {
    $build[$key] = isset($build[$key]) ? $build[$key] : [];
  }
  if (empty($build['items'])) {
    return [];
  }
  $layout = isset($build['layout']) ? $build['layout'] : [];
  $gridstack = [
    '#theme' => 'gridstack',
    '#items' => [],
    '#build' => $build,
    '#pre_render' => [
      [
        $this,
        'preRenderGridStack',
      ],
    ],
    '#layout' => $layout,
  ];
  if ($layout) {
    foreach ($layout
      ->getRegions() as $region => $info) {
      $gridstack[$region] = [];
    }
  }
  else {

    // Satisfy CTools blocks as per 2017/04/06: 2804165 which expects children
    // only, but not #theme, #type, #markup properties.
    // @todo: Remove when CTools is more accommodative.
    $gridstack['items'] = [];
  }
  $settings = $build['settings'];
  if (!empty($settings['_layout_builder'])) {

    // @todo layout builder integration.
  }

  // Only enable cache under production mode, or intentionally provided.
  // @todo: Remove when static layout supports configurable cache.
  $max_age = $this
    ->configLoad('cache.page.max_age', 'system.performance');
  $max_age = empty($settings['cache']) ? $max_age : $settings['cache'];
  $id = empty($settings['id']) ? 'gridstack-' . $settings['optionset'] : $settings['id'];
  $settings['id'] = $id = GridStack::getHtmlId('gridstack', $id);
  $suffixes[] = count($build['items']);
  $suffixes[] = count(array_filter($settings));
  $suffixes[] = $max_age;
  $cache['tags'] = Cache::buildTags('gridstack:' . $id, $suffixes, '.');
  $cache['contexts'] = [
    'languages',
  ];
  $cache['max-age'] = $max_age;
  $cache['keys'] = isset($settings['cache_metadata']['keys']) ? $settings['cache_metadata']['keys'] : [
    $id,
  ];
  if (!empty($settings['cache_tags'])) {
    $cache['tags'] = Cache::mergeTags($cache['tags'], $settings['cache_tags']);
  }
  $gridstack['#cache'] = $cache;
  return $gridstack;
}