You are here

function gridstack_update_8102 in GridStack 8.2

Same name and namespace in other branches
  1. 8 gridstack.install \gridstack_update_8102()

Update optionsets grids to use new breakpoints array.

File

./gridstack.install, line 81
Installation actions for GridStack.

Code

function gridstack_update_8102() {
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory
    ->listAll('gridstack.optionset.') as $optionset_name) {

    // Map old grids array into breakpoints array.
    $optionset = $config_factory
      ->getEditable($optionset_name);
    $options = $optionset
      ->get('options');
    $grids = $optionset
      ->get('options.grids');
    $new_grids = $optionset
      ->get('options.breakpoints.lg');
    if (empty($new_grids['grids']) && !empty($grids)) {
      $main = $nested = [];
      foreach ($grids as $grid) {
        $main[] = isset($grid['node']) ? $grid['node'] : [];
        $nested_items = isset($grid['nested']) ? $grid['nested'] : [];
        $nested[] = empty($nested_items) ? [] : Json::decode($nested_items);
      }
      $main = array_filter($main);
      if (!empty($main)) {
        $optionset
          ->set('options.breakpoints.lg.grids', Json::encode($main));
        $optionset
          ->clear('options.grids');
        $optionset
          ->clear('json.grids');
        if (!empty($nested)) {
          $check = array_filter($nested);

          // Preserve indices even if empty.
          if (!empty($check)) {
            $optionset
              ->set('options.breakpoints.lg.nested', Json::encode($nested));
          }
        }
      }
      if (isset($options['settings']['isNested'])) {
        $optionset
          ->clear('options.settings.isNested');
      }
      if (!empty($options['use_framework'])) {
        $optionset
          ->set('options.settings', []);
      }
    }
    $optionset
      ->save(TRUE);
  }
}