gridstack-8.x-2.5/gridstack.install
gridstack.install
<?php
/**
* @file
* Installation actions for GridStack.
*/
use Drupal\Component\Serialization\Json;
use Symfony\Component\Yaml\Yaml;
/**
* 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/troolee/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.
}
