You are here

gridstack.install in GridStack 8.2

Same filename and directory in other branches
  1. 8 gridstack.install

Installation actions for GridStack.

File

gridstack.install
View source
<?php

/**
 * @file
 * Installation actions for GridStack.
 */
use Drupal\Component\Serialization\Json;
use Symfony\Component\Yaml\Yaml;
use Drupal\Core\Config\Entity\ConfigEntityType;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\gridstack\GridStackDefault;
use Drupal\gridstack\Entity\GridStack;

/**
 * Implements hook_requirements().
 */
function gridstack_requirements($phase) {
  if ($phase != 'runtime') {
    return [];
  }
  $path = gridstack_libraries_get_path('gridstack') ?: \Drupal::root() . '/libraries/gridstack';
  $exists = is_file($path . '/dist/gridstack.min.js');
  return [
    'gridstack_library' => [
      'title' => t('GridStack library'),
      'description' => $exists ? '' : t('The <a href=":url">GridStack library</a> should be installed at <strong>/libraries/gridstack/dist/gridstack.min.js</strong>, or any path supported by core D8.9 library finder, or libraries.module if installed. Check out file or folder permissions if troubled.', [
        ':url' => 'https://github.com/gridstack/gridstack.js',
      ]),
      'severity' => $exists ? REQUIREMENT_OK : REQUIREMENT_ERROR,
      'value' => $exists ? t('Installed') : t('Not installed'),
    ],
  ];
}

/**
 * Update config settings, and optionsets.
 */
function gridstack_update_8101() {

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config */
  $config = \Drupal::service('config.factory');

  // Remove deprecated jQuery UI option as per v0.3.0.
  $config
    ->getEditable('gridstack.settings')
    ->clear('jquery_ui')
    ->save();

  // Clear the caches.
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();

  // Cleanup unused options.
  $config_factory = \Drupal::configFactory();
  foreach ($config_factory
    ->listAll('gridstack.optionset.') as $optionset_name) {
    $optionset = $config_factory
      ->getEditable($optionset_name);

    // Remove options.settings.framework for options.use_framework.
    $optionset
      ->clear('options.settings.framework');

    // Loop through each grid.
    $grids = $optionset
      ->get('options.grids');
    foreach ($grids as $key => $grid) {

      // Remove unused empty nested options.
      if (isset($grid['nested']) && empty($grid['nested'])) {
        $optionset
          ->clear('options.grids.' . $key . '.nested');
      }
    }
    $optionset
      ->save(TRUE);
  }

  // Import new optionsets.
  foreach ([
    'bootstrap',
    'foundation',
  ] as $key) {
    $config_path = drupal_get_path('module', 'gridstack') . '/config/install/gridstack.optionset.' . $key . '.yml';
    $data = Yaml::parseFile($config_path);
    $config_factory
      ->getEditable('gridstack.optionset.' . $key)
      ->setData($data)
      ->save(TRUE);
  }
}

/**
 * Update optionsets grids to use new breakpoints array.
 */
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);
  }
}

/**
 * Added a new service gridstack.hook for Drupal hooks.
 */
function gridstack_update_8103() {

  // Do nothing to clear cache.
}

/**
 * Moved gridstack admin templates into gridstack UI module.
 */
function gridstack_update_8200() {

  // Do nothing to clear cache.
}

/**
 * Added a new service GridStackSkinManager.
 */
function gridstack_update_8201() {

  // Do nothing to clear cache.
}

/**
 * Updated GridStackFormatter service to extend BlazyFormatter per blazy:rc7+.
 */
function gridstack_update_8202() {

  // Do nothing to clear cache.
}

/**
 * Updated optionsets to GridStack library v1.1+.
 */
function gridstack_update_8203() {
  $config_factory = \Drupal::configFactory();

  // Remove deprecated settings: customized.
  $config_factory
    ->getEditable('gridstack.settings')
    ->clear('customized')
    ->save();

  // Remove and update deprecated settings: width and height.
  foreach ($config_factory
    ->listAll('gridstack.optionset.') as $optionset_name) {
    $optionset = $config_factory
      ->getEditable($optionset_name);
    $options = $optionset
      ->get('options');

    // Settings are empty when using Bootstrap/ Foundation, skip.
    if (!empty($options['use_framework'])) {
      continue;
    }

    // Update with the new settings.
    $width = $optionset
      ->get('options.settings.width') ?: 0;
    if (!empty($width)) {
      $optionset
        ->set('options.settings.column', $width);
      $optionset
        ->set('options.settings.maxRow', $optionset
        ->get('options.settings.height'));

      // Remove deprecated settings: width, height.
      $optionset
        ->clear('options.settings.width');
      $optionset
        ->clear('options.settings.height');
      $settings = $optionset
        ->get('options.settings');

      // Remove this, since we'll make this a dynamic multi-breakpoint column.
      unset($settings['column']);
      $optionset
        ->set('json.settings', Json::encode($settings));

      // Finally save it.
      $optionset
        ->save(TRUE);
    }
  }

  // Clear the caches.
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
}

/**
 * Cleared caches to ensure schema and settings are updated.
 */
function gridstack_update_8204() {

  // Do nothing to clear cache.
}

/**
 * Moved GridStackLayout into src/Plugin/Layout.
 */
function gridstack_update_8205() {

  // Do nothing to clear cache.
}

/**
 * Added GridStackEngineManager service to manage layout engines.
 */
function gridstack_update_8206() {
  $config = \Drupal::configFactory()
    ->getEditable('gridstack.settings');

  // Update framework to use strict plugin definition due to versions.
  if ($framework = $config
    ->get('framework')) {
    if ($framework == 'bootstrap') {
      $config
        ->set('framework', 'bootstrap4');
    }
    elseif ($framework == 'foundation') {
      $config
        ->set('framework', 'foundation5');
    }
    $config
      ->save(TRUE);
  }
}

/**
 * Simplified grid storage to just array values like seen at frontend.
 */
function gridstack_update_8211() {
  $config_factory = \Drupal::configFactory();

  // Simplify and remove keys:
  // Original: [{"x":1,"y":0,"width":2,"height":8}.
  // Now: [[1,0,2,8].
  $prefix = 'gridstack.optionset.';
  foreach ($config_factory
    ->listAll($prefix) as $name) {
    $id = str_replace($prefix, '', $name);
    if ($optionset = GridStack::load($id)) {
      $storage = $config_factory
        ->getEditable($name);
      $breakpoints = $storage
        ->get('options.breakpoints');
      foreach (array_keys(GridStackDefault::breakpoints()) as $breakpoint) {
        if (!isset($breakpoints[$breakpoint])) {
          continue;
        }
        $key = 'options.breakpoints.' . $breakpoint;
        if ($data = $storage
          ->get($key)) {
          if (isset($data['grids']) && ($grids = $data['grids'])) {
            $simplified = $optionset
              ->getJsonSummaryBreakpoints($breakpoint, $grids, FALSE);
            $storage
              ->set($key . '.grids', $simplified);

            // Removed image style for Responsive image at formatter level.
            if (isset($data['image_style'])) {
              $storage
                ->clear($key . '.image_style');
            }
            if (isset($data['nested']) && ($nested_grids = $data['nested'])) {
              $simplified = $optionset
                ->getJsonSummaryNestedBreakpoints($breakpoint, $nested_grids, $grids);
              $storage
                ->set($key . '.nested', $simplified);
            }
          }
        }
      }

      // Finally save it.
      $storage
        ->save(TRUE);
    }
  }

  // Clear the caches.
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
}

/**
 * Removed unhappy experiments, things that don't work out well.
 */
function gridstack_update_8212() {

  // Do nothing to clear cache.
}

/**
 * Removed jQueryUI-related settings for core Drupal and library deprecation.
 */
function gridstack_update_8214() {
  $config_factory = \Drupal::configFactory();
  $ui_settings = [
    'staticGrid',
    'draggable',
    'resizable',
    'disableDrag',
    'disableResize',
    'alwaysShowResizeHandle',
  ];

  // Revert old default optionsets.
  foreach ([
    'bootstrap',
    'default',
    'foundation',
    'frontend',
  ] as $key) {
    $config_path = drupal_get_path('module', 'gridstack') . '/config/install/gridstack.optionset.' . $key . '.yml';
    $data = Yaml::parseFile($config_path);
    $config_factory
      ->getEditable('gridstack.optionset.' . $key)
      ->setData($data)
      ->save(TRUE);
  }

  // Revert old sample optionsets.
  if (gridstack()
    ->getModuleHandler()
    ->moduleExists('gridstack_example')) {
    foreach ([
      'paz',
      'tagore',
    ] as $key) {
      $config_path = drupal_get_path('module', 'gridstack_example') . '/config/install/gridstack.optionset.' . $key . '.yml';
      $data = Yaml::parseFile($config_path);
      $config_factory
        ->getEditable('gridstack.optionset.' . $key)
        ->setData($data)
        ->save(TRUE);
    }
  }

  // Updates the rest.
  $prefix = 'gridstack.optionset.';
  foreach ($config_factory
    ->listAll($prefix) as $name) {
    $storage = $config_factory
      ->getEditable($name);
    $settings = $storage
      ->get('options.settings');

    // CSS Framework has no JS settings, of course, skip.
    if (empty($settings)) {
      continue;
    }

    // Removed jQuery UI related settings due to being deprecated.
    foreach ($ui_settings as $key) {
      if (isset($settings[$key])) {
        $storage
          ->clear('options.settings.' . $key);
      }
    }

    // Finally save it.
    $storage
      ->save(TRUE);

    // Also update front-end string storage.
    $settings = $storage
      ->get('options.settings');

    // Remove this, since we'll make this a dynamic multi-breakpoint column.
    unset($settings['column']);
    $storage
      ->set('json.settings', Json::encode($settings));

    // Finally save it.
    $storage
      ->save(TRUE);
  }

  // Clear the caches.
  \Drupal::entityTypeManager()
    ->clearCachedDefinitions();
}

/**
 * Added stylizer service for configurable styling live preview.
 */
function gridstack_update_8215() {

  // Do nothing to clear cache.
}

/**
 * Added layout variants, or a subset of layout.
 */
function gridstack_update_8216() {

  // Do nothing to clear cache.
}

/**
 * Updated GridStack variant entity type schema.
 */
function gridstack_update_8217() {
  \Drupal::entityDefinitionUpdateManager()
    ->installEntityType(new ConfigEntityType([
    'id' => 'gridstack_variant',
    'label' => new TranslatableMarkup('GridStack variant'),
    'config_prefix' => 'variant',
    'admin_permission' => 'administer gridstack variant',
    'list_path' => 'admin/structure/gridstack/variant',
    'entity_keys' => [
      'id' => 'name',
      'label' => 'label',
      'status' => 'status',
      'weight' => 'weight',
      'source' => 'source',
    ],
    'config_export' => [
      'id',
      'name',
      'label',
      'description',
      'status',
      'weight',
      'source',
      'options',
    ],
  ]));
}

/**
 * Fixed for D8.9 to address Library finder overrides.
 */
function gridstack_update_8218() {

  // Do nothing to clear cache.
}

Functions

Namesort descending Description
gridstack_requirements Implements hook_requirements().
gridstack_update_8101 Update config settings, and optionsets.
gridstack_update_8102 Update optionsets grids to use new breakpoints array.
gridstack_update_8103 Added a new service gridstack.hook for Drupal hooks.
gridstack_update_8200 Moved gridstack admin templates into gridstack UI module.
gridstack_update_8201 Added a new service GridStackSkinManager.
gridstack_update_8202 Updated GridStackFormatter service to extend BlazyFormatter per blazy:rc7+.
gridstack_update_8203 Updated optionsets to GridStack library v1.1+.
gridstack_update_8204 Cleared caches to ensure schema and settings are updated.
gridstack_update_8205 Moved GridStackLayout into src/Plugin/Layout.
gridstack_update_8206 Added GridStackEngineManager service to manage layout engines.
gridstack_update_8211 Simplified grid storage to just array values like seen at frontend.
gridstack_update_8212 Removed unhappy experiments, things that don't work out well.
gridstack_update_8214 Removed jQueryUI-related settings for core Drupal and library deprecation.
gridstack_update_8215 Added stylizer service for configurable styling live preview.
gridstack_update_8216 Added layout variants, or a subset of layout.
gridstack_update_8217 Updated GridStack variant entity type schema.
gridstack_update_8218 Fixed for D8.9 to address Library finder overrides.