gridstack.theme.inc in GridStack 8
Same filename and directory in other branches
Hooks and preprocess functions for the GridStack module.
File
templates/gridstack.theme.incView source
<?php
/**
* @file
* Hooks and preprocess functions for the GridStack module.
*/
use Drupal\Core\Template\Attribute;
use Drupal\Component\Serialization\Json;
use Drupal\gridstack\Entity\GridStack;
/**
* Prepares variables for gridstack.html.twig templates.
*/
function template_preprocess_gridstack(&$variables) {
$element = $variables['element'];
$attributes =& $variables['attributes'];
$defaults = GridStack::htmlSettings();
$settings = isset($element['#settings']) ? array_merge($defaults, $element['#settings']) : $defaults;
$optionset = isset($element['#optionset']) ? $element['#optionset'] : GridStack::load($settings['optionset']);
$id = empty($settings['id']) ? 'gridstack-' . $settings['optionset'] : $settings['id'];
// Pass settings and attributes to twig.
$settings['id'] = $id = GridStack::getHtmlId('gridstack', $id);
$settings['class_by_id'] = strpos($id, 'gridstack-') !== FALSE ? str_replace('gridstack-', 'gridstack--', $id) : '';
$variables['wrapper'] = 'div';
if (empty($settings['breakpoints'])) {
$optionset
->gridsJsonToArray($settings);
}
if ($settings['use_framework']) {
$settings['use_framework'] = empty($settings['_admin']);
}
if (!empty($settings['root'])) {
// Prepare attributes, and add the configuration as JSON object.
$blazy = empty($settings['blazy_data']) ? '' : Json::encode($settings['blazy_data']);
$columns = isset($element['#columns']) ? $element['#columns'] : $optionset
->getJson('breakpoints');
$js = isset($element['#options']) ? $element['#options'] : $optionset
->getJson('settings');
if (!empty($blazy)) {
$attributes['class'][] = 'blazy';
$attributes['data-blazy'] = $blazy;
}
if (!empty($settings['media_switch'])) {
$switch = str_replace('_', '-', $settings['media_switch']);
$attributes['data-' . $switch . '-gallery'] = TRUE;
}
// Provides attributes for GridStack JS, but not Bootstrap/ Foundation.
$settings['nester'] = !empty($settings['use_framework']);
if (empty($settings['use_framework'])) {
// Only reasonable for GridStack JS.
$attributes['id'] = $id;
// {"320":1,"640":2,"768":3,"1140":12} to update columns by window widths.
// @todo: Remove condition after another proper update.
$attributes['data-breakpoints'] = strpos($columns, '{"":12}') !== FALSE ? '' : $columns;
$attributes['data-config'] = $js;
// Gets options.breakpoints.sm.[width, column, image_style, grids], etc.
$responsives = $optionset
->getBreakpoints();
$exclude_image_style = empty($settings['_admin']);
if (!empty($responsives)) {
foreach ($optionset::getConstantBreakpoints() as $breakpoint) {
$responsive_grids = $optionset
->getJsonSummaryBreakpoints($breakpoint, $exclude_image_style);
$has_width = isset($responsives[$breakpoint]['width']) && $responsives[$breakpoint]['width'] > -1;
if ($has_width && $responsive_grids) {
$attributes['data-' . $breakpoint . '-width'] = $responsives[$breakpoint]['width'];
$attributes['data-' . $breakpoint . '-grids'] = $responsive_grids;
}
}
}
// Breakpoint related data-attributes helpers.
if (!empty($settings['minWidth'])) {
$attributes['data-min-width'] = $settings['minWidth'];
}
}
$variables['wrapper'] = empty($settings['wrapper']) ? 'div' : $settings['wrapper'];
}
$variables['settings'] = $settings;
// Do not leak container wrapper.
unset($settings['wrapper']);
// Process individual item.
$variables['items'] = [];
foreach ($element['#items'] as $delta => $item) {
// Prevents complication with nested grids.
if (!isset($item['box'])) {
continue;
}
$item_attributes = isset($item['attributes']) ? $item['attributes'] : [];
$item_settings = isset($item['settings']) ? array_merge($settings, $item['settings']) : $settings;
unset($item['attributes'], $item['settings']);
$box = [
'#theme' => 'gridstack_box',
'#item' => $item,
'#delta' => $delta,
'#attributes' => $item_attributes,
'#settings' => $item_settings,
];
$variables['items'][$delta] = $box;
unset($box);
}
}
/**
* Prepares variables for gridstack-box.html.twig templates.
*/
function template_preprocess_gridstack_box(&$variables) {
foreach ([
'attributes',
'delta',
'item',
'settings',
] as $key) {
$variables[$key] = isset($variables['element']["#{$key}"]) ? $variables['element']["#{$key}"] : [];
}
$content_attributes =& $variables['content_attributes'];
$settings =& $variables['settings'];
$item =& $variables['item'];
// Boxes may have captions.
foreach ([
'alt',
'data',
'link',
'title',
] as $key) {
$item['caption'][$key] = isset($item['caption']) && isset($item['caption'][$key]) ? $item['caption'][$key] : [];
}
$item['box'] = isset($item['box']) ? $item['box'] : [];
$item['caption'] = array_filter($item['caption']);
$settings['type'] = isset($settings['type']) ? $settings['type'] : '';
$variables['wrapper'] = empty($settings['wrapper']) ? 'div' : $settings['wrapper'];
$variables['content_attributes'] = new Attribute($content_attributes);
}
/**
* Prepares variables for gridstack-admin.html.twig templates.
*/
function template_preprocess_gridstack_admin(&$variables) {
template_preprocess_gridstack($variables);
$element = $variables['element'];
foreach ([
'content',
'preview',
'wrapper',
] as $key) {
$variables[$key . '_attributes'] = isset($element["#{$key}" . '_attributes']) ? $element["#{$key}" . '_attributes'] : [];
}
$settings =& $variables['settings'];
$content_attributes =& $variables['content_attributes'];
$preview_attributes =& $variables['preview_attributes'];
$wrapper_attributes =& $variables['wrapper_attributes'];
$settings['id'] = 'gridstack-' . $settings['breakpoint'];
$content_attributes['id'] = $settings['id'];
$variables['wrapper_attributes']['id'] = 'gridstack-wrapper-' . $settings['breakpoint'];
$variables['wrapper_attributes']['data-breakpoint'] = $settings['breakpoint'];
$variables['content_attributes'] = new Attribute($content_attributes);
$variables['preview_attributes'] = new Attribute($preview_attributes);
$variables['wrapper_attributes'] = new Attribute($wrapper_attributes);
$variables['main_button_texts'] = [];
$settings['is_main_preview'] = FALSE;
if ($settings['breakpoint'] == $settings['icon_breakpoint']) {
$settings['is_main_preview'] = TRUE;
$variables['main_button_texts'] = [
'save' => t('Update icon'),
'add' => t('Add grid'),
];
}
}
/**
* Prepares variables for gridstack-admin.html.twig templates.
*/
function template_preprocess_gridstack_dummy(&$variables) {
$element = $variables['element'];
$all = [
'delta',
'dummies',
'image_style',
'item',
'nested_grids',
'settings',
];
foreach ($all as $key) {
$variables[$key] = isset($element["#{$key}"]) ? $element["#{$key}"] : [];
}
$variables['index'] = (int) $variables['delta'] + 1;
$variables['content'] = $element['#children'];
foreach ([
'box',
'button_add',
'button_remove',
'nested',
] as $key) {
$variables[$key . '_attributes'] = new Attribute();
}
}
/**
* Prepares variables for blazy-preview.html.twig templates.
*/
function template_preprocess_gridstack_preview(&$variables) {
$element = $variables['element'];
$variables['settings'] = isset($element['#settings']) ? $element['#settings'] : [];
}
Functions
Name | Description |
---|---|
template_preprocess_gridstack | Prepares variables for gridstack.html.twig templates. |
template_preprocess_gridstack_admin | Prepares variables for gridstack-admin.html.twig templates. |
template_preprocess_gridstack_box | Prepares variables for gridstack-box.html.twig templates. |
template_preprocess_gridstack_dummy | Prepares variables for gridstack-admin.html.twig templates. |
template_preprocess_gridstack_preview | Prepares variables for blazy-preview.html.twig templates. |