feat(main): Add slider block type
This commit is contained in:
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Websmid\CustomHtml\Module;
|
||||
|
||||
use Websmid\CustomHtml\Config\Config;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlBlock;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlFaq;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlGroup;
|
||||
use Websmid\CustomHtml\Model\CustomHtmlSlide;
|
||||
use Context;
|
||||
|
||||
trait DisplayHooks
|
||||
@@ -70,6 +72,7 @@ trait DisplayHooks
|
||||
$blocksByGroup = CustomHtmlBlock::getActiveByGroupIds(array_column($groups, 'id_ws_customhtml_group'));
|
||||
$blocksByGroup = $this->attachFaqsToBlocks($blocksByGroup);
|
||||
$blocksByGroup = $this->attachImageUrls($blocksByGroup);
|
||||
$blocksByGroup = $this->attachSlidesToBlocks($blocksByGroup);
|
||||
|
||||
$groups = array_map(static function (array $group) use ($blocksByGroup) {
|
||||
$group['blocks'] = $blocksByGroup[(int) $group['id_ws_customhtml_group']] ?? [];
|
||||
@@ -107,4 +110,67 @@ trait DisplayHooks
|
||||
}, $blocks);
|
||||
}, $blocksByGroup);
|
||||
}
|
||||
|
||||
private function attachSlidesToBlocks(array $blocksByGroup): array
|
||||
{
|
||||
return array_map(function (array $blocks) {
|
||||
return array_map(function (array $block) {
|
||||
if ($block['type'] === 'slider') {
|
||||
$block['slides'] = $this->loadSlidesForBlock((int) $block['id_ws_customhtml']);
|
||||
$block['slider_config'] = $this->buildSliderConfig($block);
|
||||
$block['slider_desktop_column_width'] = $this->buildSliderDesktopColumnWidth($block);
|
||||
}
|
||||
|
||||
return $block;
|
||||
}, $blocks);
|
||||
}, $blocksByGroup);
|
||||
}
|
||||
|
||||
private function loadSlidesForBlock(int $idBlock): array
|
||||
{
|
||||
return array_map(static function (array $slide) {
|
||||
$slide['image_url'] = CustomHtmlBlock::getBannerImageUrl($slide['image']);
|
||||
$slide['show_title'] = true;
|
||||
|
||||
return $slide;
|
||||
}, CustomHtmlSlide::getByBlockId($idBlock));
|
||||
}
|
||||
|
||||
private function buildSliderConfig(array $block): string
|
||||
{
|
||||
$config = [
|
||||
'slidesPerView' => (float) $block['slides_per_view_mobile'],
|
||||
'spaceBetween' => 16,
|
||||
'speed' => 500,
|
||||
'watchOverflow' => true,
|
||||
];
|
||||
|
||||
if ($block['slider_mode'] === 'responsive') {
|
||||
$config['breakpoints'] = [
|
||||
Config::SLIDER_RESPONSIVE_BREAKPOINT => ['slidesPerView' => (float) $block['slides_per_view_tablet']],
|
||||
];
|
||||
} else {
|
||||
$config['breakpoints'] = [
|
||||
768 => ['slidesPerView' => (float) $block['slides_per_view_tablet']],
|
||||
992 => ['slidesPerView' => (float) $block['slides_per_view_desktop']],
|
||||
];
|
||||
}
|
||||
|
||||
if ((int) $block['autoplay_delay'] > 0) {
|
||||
$config['loop'] = true;
|
||||
$config['autoplay'] = [
|
||||
'delay' => (int) $block['autoplay_delay'] * 1000,
|
||||
'disableOnInteraction' => false,
|
||||
];
|
||||
}
|
||||
|
||||
return json_encode($config);
|
||||
}
|
||||
|
||||
private function buildSliderDesktopColumnWidth(array $block): string
|
||||
{
|
||||
$columns = max(1, (int) round((float) $block['slides_per_view_desktop']));
|
||||
|
||||
return round(100 / $columns, 4) . '%';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user