You are here

gridstack.module in GridStack 8

Same filename and directory in other branches
  1. 8.2 gridstack.module

Provides GridStack integration to have multi-column grids with drag-and-drop.

File

gridstack.module
View source
<?php

/**
 * @file
 * Provides GridStack integration to have multi-column grids with drag-and-drop.
 */

/**
 * Implements hook_theme().
 */
function gridstack_theme() {
  $themes = [];
  foreach ([
    'gridstack',
    'box',
    'admin',
    'dummy',
    'preview',
  ] as $item) {
    $key = $item == 'gridstack' ? $item : 'gridstack_' . $item;
    $themes[$key] = [
      'render element' => $item == 'preview' ? 'content' : 'element',
      'file' => 'templates/gridstack.theme.inc',
    ];
  }
  return $themes;
}

/**
 * Implements hook_library_info_build().
 */
function gridstack_library_info_build() {
  return \Drupal::service('gridstack.manager')
    ->libraryInfoBuild();
}

/**
 * Implements hook_field_formatter_info_alter().
 */
function gridstack_field_formatter_info_alter(array &$info) {
  \Drupal::service('gridstack.manager')
    ->fieldFormatterInfoAlter($info);
}

/**
 * Implements hook_hook_info().
 */
function gridstack_hook_info() {
  $hooks['gridstack_skins_info'] = [
    'group' => 'gridstack',
  ];
  return $hooks;
}

/**
 * Implements hook_library_info_alter().
 */
function gridstack_library_info_alter(&$libraries, $extension) {
  if ($extension === 'gridstack' && function_exists('libraries_get_path')) {
    $libraries['gridstack']['js'] = [
      '/' . libraries_get_path('gridstack') . '/dist/gridstack.min.js' => [],
    ];
  }
}

/**
 * Implements hook_layout_alter().
 */
function gridstack_layout_alter(&$definitions) {

  // Core and contrib layouts don't co-exist, safe to check for each presence.
  $handler = \Drupal::service('gridstack.manager')
    ->getModuleHandler();

  // This requires Drupal >= 8.x-3 with core layout_discovery.module.
  if ($handler
    ->moduleExists('layout_discovery')) {
    \Drupal\gridstack\Layout\GridStackLayout::layoutAlter($definitions);
  }
  elseif ($handler
    ->moduleExists('layout_plugin')) {
    \Drupal\gridstack\Layout\GridStackLayoutDeprecated::layoutAlter($definitions);
  }
}

/**
 * Implements hook_config_schema_info_alter().
 */
function gridstack_config_schema_info_alter(array &$definitions) {
  if (isset($definitions['layout_plugin.settings']) || isset($definitions['core.entity_view_display.*.*.*.third_party.ds'])) {
    \Drupal::service('gridstack.manager')
      ->configSchemaInfoAlter($definitions);
  }
}

/**
 * Implements hook_theme_suggestions_HOOK_alter().
 */
function gridstack_theme_suggestions_gridstack_alter(array &$suggestions, array $variables) {
  $settings = isset($variables['element']['#settings']) ? $variables['element']['#settings'] : [];
  if (!empty($settings['root']) && !empty($settings['optionset'])) {
    $suggestions[] = 'gridstack__' . $settings['optionset'];

    // This currently applies to DS only.
    // @todo: Make this work for field formatters, and Views style plugin.
    if (!empty($settings['extras']) && isset($settings['extras']['bundle'])) {
      $extras = $settings['extras'];
      $suggestions[] = 'gridstack__' . $extras['entity_type'];
      $suggestions[] = 'gridstack__' . $extras['entity_type'] . '_' . $extras['bundle'];
      $suggestions[] = 'gridstack__' . $extras['entity_type'] . '_' . $extras['bundle'] . '_' . $extras['view_mode'];
      $suggestions[] = 'gridstack__' . $extras['entity_type'] . '_' . $extras['bundle'] . '_' . $settings['optionset'];
    }
  }
}