Skip to content

Commit d0272b2

Browse files
committed
feat(SlideshowManager): add custom pagination template support
- Enhanced pagination configuration to include a custom template option. - Updated `config.php`, `Install.php`, `Settings.php`, and relevant Twig templates to support the new 'custom' pagination type. - Implemented logic in `SlideshowVariable.php` to handle custom template rendering for pagination.
1 parent 805aae8 commit d0272b2

File tree

7 files changed

+65
-1
lines changed

7 files changed

+65
-1
lines changed

src/config.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,8 @@
6767
'pagination' => [
6868
'enabled' => true,
6969
'clickable' => true,
70-
'type' => 'bullets', // bullets, fraction, progressbar
70+
'type' => 'bullets', // bullets, fraction, progressbar, custom
71+
'customTemplate' => '{current} / {total}', // Used when type = custom
7172
],
7273
'paginationVisibility' => 'default', // default, hide-mobile, hide-desktop, mobile-only, desktop-only
7374

src/migrations/Install.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ protected function createTables(): void
130130
'enabled' => true,
131131
'clickable' => true,
132132
'type' => 'bullets',
133+
'customTemplate' => '{current} / {total}',
133134
],
134135
'autoplay' => [
135136
'enabled' => false,

src/models/Settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Settings extends Model
5353
'enabled' => true,
5454
'clickable' => true,
5555
'type' => 'bullets',
56+
'customTemplate' => '{current} / {total}',
5657
],
5758
'autoplay' => [
5859
'enabled' => false,

src/templates/_components/fields/SlideshowConfigField/input.twig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,18 @@
5555
{ label: 'Bullets', value: 'bullets' },
5656
{ label: 'Fraction', value: 'fraction' },
5757
{ label: 'Progressbar', value: 'progressbar' },
58+
{ label: 'Custom', value: 'custom' },
5859
],
5960
}) }}
6061

62+
{{ forms.textField({
63+
label: 'Custom Pagination Template'|t('slideshow-manager'),
64+
instructions: 'Used only when Pagination Type is Custom. Available placeholders: {current}, {total}'|t('slideshow-manager'),
65+
name: 'paginationCustomTemplate',
66+
value: config.paginationCustomTemplate ?? '{current} / {total}',
67+
placeholder: '{current} / {total}',
68+
}) }}
69+
6170
{{ forms.lightswitchField({
6271
label: 'Clickable Pagination'|t('slideshow-manager'),
6372
instructions: 'Allow clicking pagination to navigate'|t('slideshow-manager'),

src/templates/_components/fields/SlideshowField/config.twig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,26 @@
5656
}) }}
5757

5858
<div id="field-pagination-visibility-{{ uniqueId }}" class="{{ (config.pagination.enabled ?? true) ? '' : 'hidden' }}">
59+
{{ forms.selectField({
60+
label: 'Pagination Type'|t('slideshow-manager'),
61+
name: namespace ~ '[slideshowConfig][pagination][type]',
62+
value: config.pagination.type ?? 'bullets',
63+
options: [
64+
{ label: 'Bullets', value: 'bullets' },
65+
{ label: 'Fraction', value: 'fraction' },
66+
{ label: 'Progressbar', value: 'progressbar' },
67+
{ label: 'Custom', value: 'custom' },
68+
],
69+
}) }}
70+
71+
{{ forms.textField({
72+
label: 'Custom Pagination Template'|t('slideshow-manager'),
73+
instructions: 'Used only when Pagination Type is Custom. Available placeholders: {current}, {total}'|t('slideshow-manager'),
74+
name: namespace ~ '[slideshowConfig][pagination][customTemplate]',
75+
value: config.pagination.customTemplate ?? '{current} / {total}',
76+
placeholder: '{current} / {total}',
77+
}) }}
78+
5979
{{ forms.selectField({
6080
label: 'Pagination Visibility'|t('slideshow-manager'),
6181
name: namespace ~ '[slideshowConfig][paginationVisibility]',

src/templates/settings/basic.twig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,24 @@
9090
{ label: 'Bullets', value: 'bullets' },
9191
{ label: 'Fraction', value: 'fraction' },
9292
{ label: 'Progressbar', value: 'progressbar' },
93+
{ label: 'Custom', value: 'custom' },
9394
],
9495
disabled: settings.isOverriddenByConfig('defaultSwiperConfig.pagination.type'),
9596
warning: settings.isOverriddenByConfig('defaultSwiperConfig.pagination.type') ?
9697
'This is being overridden by the <code>defaultSwiperConfig.pagination.type</code> setting in <code>config/slideshow-manager.php</code>.'|t('slideshow-manager')|raw : null,
9798
}) }}
9899

100+
{{ forms.textField({
101+
label: 'Custom Pagination Template'|t('slideshow-manager'),
102+
instructions: 'Used only when Pagination Type is Custom. Available placeholders: <code>{current}</code>, <code>{total}</code>'|t('slideshow-manager')|raw,
103+
id: 'defaultSwiperConfig-pagination-customTemplate',
104+
name: 'settings[defaultSwiperConfig][pagination][customTemplate]',
105+
value: settings.defaultSwiperConfig.pagination.customTemplate ?? '{current} / {total}',
106+
disabled: settings.isOverriddenByConfig('defaultSwiperConfig.pagination.customTemplate'),
107+
warning: settings.isOverriddenByConfig('defaultSwiperConfig.pagination.customTemplate') ?
108+
'This is being overridden by the <code>defaultSwiperConfig.pagination.customTemplate</code> setting in <code>config/slideshow-manager.php</code>.'|t('slideshow-manager')|raw : null,
109+
}) }}
110+
99111
{{ forms.lightswitchField({
100112
label: 'Clickable Pagination'|t('slideshow-manager'),
101113
instructions: 'Allow clicking pagination to navigate'|t('slideshow-manager'),

src/variables/SlideshowVariable.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,10 @@ public function buildSwiperConfig(array $config, string $sliderId): array
175175
if (!empty($config['paginationType'])) {
176176
$swiperConfig['pagination']['type'] = $config['paginationType'];
177177
}
178+
179+
if (!empty($config['paginationCustomTemplate'])) {
180+
$swiperConfig['pagination']['customTemplate'] = (string)$config['paginationCustomTemplate'];
181+
}
178182
} else {
179183
$swiperConfig['pagination'] = false;
180184
}
@@ -390,6 +394,22 @@ function initSwiper_{$safeFunctionName}() {
390394
config = Object.assign({}, config, overrides);
391395
}
392396
397+
// Support pagination type "custom" via template placeholders.
398+
// Swiper requires pagination.renderCustom to be a function, which cannot be JSON encoded.
399+
if (config.pagination && typeof config.pagination === 'object' && config.pagination.type === 'custom') {
400+
const customTemplate = typeof config.pagination.customTemplate === 'string' && config.pagination.customTemplate.trim() !== ''
401+
? config.pagination.customTemplate
402+
: '{current} / {total}';
403+
404+
config.pagination.renderCustom = function(swiper, current, total) {
405+
return customTemplate
406+
.split('{current}').join(String(current))
407+
.split('{total}').join(String(total));
408+
};
409+
410+
delete config.pagination.customTemplate;
411+
}
412+
393413
if (debug) {
394414
console.log('Initializing Swiper "' + sliderId + '" with config:', config);
395415
}

0 commit comments

Comments
 (0)