View source
<?php
namespace Drupal\blazy;
class BlazyGrid {
public static function build(array $items = [], array $settings = []) {
$settings += BlazyDefault::htmlSettings();
$style = empty($settings['style']) ? '' : $settings['style'];
$is_grid = isset($settings['_grid']) ? $settings['_grid'] : !empty($settings['style']) && !empty($settings['grid']);
$class_item = $is_grid ? 'grid' : 'blazy__item';
$contents = [];
foreach ($items as $item) {
$attributes = isset($item['attributes']) ? $item['attributes'] : [];
$item_settings = isset($item['settings']) ? $item['settings'] : $settings;
$item_settings = isset($item['#build']) && isset($item['#build']['settings']) ? $item['#build']['settings'] : $item_settings;
unset($item['settings'], $item['attributes'], $item['item']);
$classes = empty($item_settings['grid_content_class']) ? [] : (array) $item_settings['grid_content_class'];
$content['content'] = $is_grid ? [
'#theme' => 'container',
'#children' => $item,
'#attributes' => [
'class' => array_merge([
'grid__content',
], $classes),
],
] : $item;
if (!empty($item_settings['grid_item_class'])) {
$attributes['class'][] = $item_settings['grid_item_class'];
}
$classes = isset($attributes['class']) ? $attributes['class'] : [];
$attributes['class'] = array_merge([
$class_item,
], $classes);
$content['#wrapper_attributes'] = $attributes;
$contents[] = $content;
}
$settings['count'] = empty($settings['count']) ? count($contents) : $settings['count'];
$wrapper = [
'item-list--blazy',
'item-list--blazy-' . $style,
];
$wrapper = $style ? $wrapper : [
'item-list--blazy',
];
$wrapper = array_merge([
'item-list',
], $wrapper);
$element = [
'#theme' => 'item_list',
'#items' => $contents,
'#context' => [
'settings' => $settings,
],
'#attributes' => [],
'#wrapper_attributes' => [
'class' => $wrapper,
],
];
if (empty($settings['use_field']) && isset($settings['label'], $settings['label_display']) && $settings['label_display'] != 'hidden') {
$element['#title'] = $settings['label'];
}
self::attributes($element['#attributes'], $settings);
return $element;
}
public static function attributes(array &$attributes, array $settings = []) {
$style = empty($settings['style']) ? '' : $settings['style'];
$is_gallery = !empty($settings['lightbox']) && !empty($settings['gallery_id']);
$is_grid = isset($settings['_grid']) ? $settings['_grid'] : !empty($settings['style']) && !empty($settings['grid']);
Blazy::containerAttributes($attributes, $settings);
if (!empty($settings['id'])) {
$attributes['id'] = $is_gallery ? $settings['gallery_id'] : $settings['id'];
}
if ($is_grid) {
$attributes['class'][] = 'blazy--grid block-' . $style . ' block-count-' . $settings['count'];
if ($settings['grid_large'] = $settings['grid']) {
foreach ([
'small',
'medium',
'large',
] as $grid) {
if (!empty($settings['grid_' . $grid])) {
$attributes['class'][] = $grid . '-block-' . $style . '-' . $settings['grid_' . $grid];
}
}
}
}
}
}