You are here

gridstack.install in GridStack 8

Same filename and directory in other branches
  1. 8.2 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;

/**
 * Implements hook_requirements().
 */
function gridstack_requirements($phase) {
  if ($phase != 'runtime') {
    return [];
  }
  if (function_exists('libraries_get_path')) {
    $path = libraries_get_path('gridstack') . '/dist/gridstack.min.js';
  }
  else {
    $path = DRUPAL_ROOT . '/libraries/gridstack/dist/gridstack.min.js';
  }
  $exists = is_file($path);
  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 libraries.module if installed.', [
        ':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::parse($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);
  }
}

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.