function layout_builder_styles_preprocess_layout in Layout Builder Styles 8
Implements hook_preprocess_HOOK().
File
- ./
layout_builder_styles.module, line 235 - Layout Builder Styles module file.
Code
function layout_builder_styles_preprocess_layout(&$variables) {
// Apply a configured style to a layout by adding the style's CSS classes.
if (isset($variables['settings']['layout_builder_styles_style'])) {
$selected = $variables['settings']['layout_builder_styles_style'];
/** @var \Drupal\layout_builder_styles\LayoutBuilderStyleInterface $style */
// Convert single selection to an array for consistent processing.
if (!is_array($selected)) {
$selected = [
$selected,
];
}
// Retrieve all styles from selection(s).
$grouped_classes = [];
foreach ($selected as $stylename) {
// Account for incorrectly configured section configuration which may
// have a NULL style ID. We cannot pass NULL to the storage handler or
// it will throw an exception.
if (empty($stylename)) {
continue;
}
if ($layout_style = \Drupal::entityTypeManager()
->getStorage('layout_builder_style')
->load($stylename)) {
$classes = \preg_split('(\\r\\n|\\r|\\n)', $layout_style
->getClasses());
$grouped_classes = array_merge($grouped_classes, $classes);
$variables['#cache']['tags'][] = 'config:layout_builder_styles.style.' . $layout_style
->id();
}
}
if (!empty($grouped_classes)) {
if (!isset($variables['attributes']['class']) || !is_array($variables['attributes']['class'])) {
$variables['attributes']['class'] = [];
}
$variables['attributes']['class'] = array_merge($variables['attributes']['class'], $grouped_classes);
}
}
}