function template_preprocess_gridstack in GridStack 8
Same name and namespace in other branches
- 8.2 templates/gridstack.theme.inc \template_preprocess_gridstack()
Prepares variables for gridstack.html.twig templates.
1 call to template_preprocess_gridstack()
- template_preprocess_gridstack_admin in templates/
gridstack.theme.inc - Prepares variables for gridstack-admin.html.twig templates.
File
- templates/
gridstack.theme.inc, line 15 - Hooks and preprocess functions for the GridStack module.
Code
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);
}
}