Skip to content

Commit 46cef1c

Browse files
authored
Add renderer service (#23)
* Add renderer service to make methods of elements reusable * CS * Update dependencies * CS * CS * CS * Replace redundant swiperId * Fix loading of current page * Update loading of the pageModel
1 parent f332a55 commit 46cef1c

File tree

7 files changed

+288
-151
lines changed

7 files changed

+288
-151
lines changed

composer.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,14 @@
1919
"forum": "https://community.contao.org/de"
2020
},
2121
"require": {
22-
"contao/core-bundle": "~4.4",
22+
"php": "^7.2 || ^8.0",
23+
"contao/core-bundle": "^4.9",
2324
"fiedsch/contao-jsonwidget": "^0.4",
24-
"menatwork/contao-multicolumnwizard": "^3.3"
25+
"menatwork/contao-multicolumnwizard": "^3.3",
26+
"symfony/config": "^4.4 || ^5.2",
27+
"symfony/dependency-injection": "^4.4 || ^5.2",
28+
"symfony/http-foundation": "^4.4 || ^5.2",
29+
"symfony/http-kernel": "^4.4 || ^5.2"
2530
},
2631
"require-dev": {
2732
"contao/manager-plugin": "^2.0"

src/ContaoManager/Plugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Plugin implements BundlePluginInterface
2828
/**
2929
* {@inheritdoc}
3030
*/
31-
public function getBundles(ParserInterface $parser)
31+
public function getBundles(ParserInterface $parser): array
3232
{
3333
return [
3434
BundleConfig::create(ContaoSwiperBundle::class)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
/*
5+
* This file is part of the ContaoSwiper Bundle.
6+
*
7+
* (c) Fritz Michael Gschwantner <https://github.com/fritzmg>
8+
*
9+
* For the full copyright and license information, please view the LICENSE
10+
* file that was distributed with this source code.
11+
*/
12+
13+
namespace ContaoSwiperBundle\DependencyInjection;
14+
15+
use Symfony\Component\Config\FileLocator;
16+
use Symfony\Component\DependencyInjection\ContainerBuilder;
17+
use Symfony\Component\DependencyInjection\Extension\Extension;
18+
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
19+
20+
class ContaoSwiperExtension extends Extension
21+
{
22+
/**
23+
* @throws \Exception
24+
*/
25+
public function load(array $configs, ContainerBuilder $container): void
26+
{
27+
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
28+
$loader->load('services.yaml');
29+
}
30+
}

src/Elements/WrapperStart.php

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Contao\BackendTemplate;
1515
use Contao\ContentElement;
16+
use Contao\System;
1617

1718

1819
/**
@@ -32,15 +33,12 @@ class WrapperStart extends ContentElement
3233

3334
/**
3435
* Display a wildcard in the back end.
35-
*
36-
* @return string
3736
*/
38-
public function generate()
37+
public function generate(): string
3938
{
40-
if (TL_MODE == 'BE')
39+
if (TL_MODE === 'BE')
4140
{
42-
$objTemplate = new BackendTemplate('be_wildcard');
43-
return $objTemplate->parse();
41+
return (new BackendTemplate('be_wildcard'))->parse();
4442
}
4543

4644
return parent::generate();
@@ -50,27 +48,11 @@ public function generate()
5048
/**
5149
* Generate the content element
5250
*/
53-
protected function compile()
51+
protected function compile(): void
5452
{
5553
// additional css classes
5654
$arrClasses = explode(' ', $this->cssID[1]);
57-
58-
if ($this->sliderButtons)
59-
{
60-
$arrClasses[] = 'has-buttons';
61-
}
62-
if ($this->sliderPagination)
63-
{
64-
$arrClasses[] = 'has-pagination';
65-
}
66-
if ($this->sliderPaginationType)
67-
{
68-
$arrClasses[] = 'pagination-'.$this->sliderPaginationType;
69-
}
70-
if ($this->sliderSlidesPerView)
71-
{
72-
$arrClasses[] = 'slides-per-view-'.$this->sliderSlidesPerView;
73-
}
55+
System::getContainer()->get('fritzmg.contao_swiper.renderer')->addCssClasses($this, $arrClasses);
7456

7557
// set classes
7658
$arrCssID = $this->cssID;

src/Elements/WrapperStop.php

Lines changed: 8 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,6 @@
1414
use Contao\BackendTemplate;
1515
use Contao\ContentElement;
1616
use Contao\ContentModel;
17-
use Contao\LayoutModel;
18-
use Contao\PageModel;
19-
use Contao\StringUtil;
2017
use Contao\System;
2118

2219

@@ -37,15 +34,12 @@ class WrapperStop extends ContentElement
3734

3835
/**
3936
* Display a wildcard in the back end.
40-
*
41-
* @return string
4237
*/
43-
public function generate()
38+
public function generate(): string
4439
{
45-
if (TL_MODE == 'BE')
40+
if (TL_MODE === 'BE')
4641
{
47-
$objTemplate = new BackendTemplate('be_wildcard');
48-
return $objTemplate->parse();
42+
return (new BackendTemplate('be_wildcard'))->parse();
4943
}
5044

5145
return parent::generate();
@@ -55,7 +49,7 @@ public function generate()
5549
/**
5650
* Generate the content element
5751
*/
58-
protected function compile()
52+
protected function compile(): void
5953
{
6054
$t = ContentModel::getTable();
6155
$objContent = ContentModel::findAll([
@@ -76,120 +70,10 @@ protected function compile()
7670
'return' => 'Model'
7771
]);
7872

79-
if (null !== $objContent)
80-
{
81-
// default effect
82-
$objContent->sliderEffect = $objContent->sliderEffect ?: 'slide';
83-
84-
// prepare parameters for swiper
85-
$arrParams = array();
86-
87-
// string for swiper id
88-
$swiperId = 'swiper-' . $objContent->id;
89-
90-
// process parameters
91-
if ($objContent->sliderDelay) $arrParams['autoplay'] = ['delay' => (int) $objContent->sliderDelay];
92-
if ($objContent->sliderSpeed) $arrParams['speed'] = (int) $objContent->sliderSpeed;
93-
if ($objContent->sliderSlidesPerView && (is_numeric($objContent->sliderSlidesPerView) || $objContent->sliderSlidesPerView == 'auto'))
94-
$arrParams['slidesPerView'] = is_numeric($objContent->sliderSlidesPerView) ? (int) $objContent->sliderSlidesPerView : $objContent->sliderSlidesPerView;
95-
if ($objContent->sliderSpaceBetween) $arrParams['spaceBetween'] = (int) $objContent->sliderSpaceBetween;
96-
if ('crossfade' === $objContent->sliderEffect) {
97-
$arrParams['effect'] = 'fade';
98-
$arrParams['fadeEffect'] = ['crossFade' => true];
99-
} elseif ($objContent->sliderEffect) {
100-
$arrParams['effect'] = $objContent->sliderEffect;
101-
}
102-
if ($objContent->sliderContinuous) $arrParams['loop'] = true;
103-
if ($objContent->sliderButtons)
104-
{
105-
$arrParams['navigation'] = [
106-
'nextEl' => '#' . $swiperId . ' .swiper-button-next',
107-
'prevEl' => '#' . $swiperId . ' .swiper-button-prev',
108-
];
109-
}
110-
if ($objContent->sliderPagination)
111-
{
112-
$arrParams['pagination'] = [
113-
'el' => '#' . $swiperId . ' .swiper-pagination',
114-
'clickable' => true,
115-
];
116-
117-
if ($objContent->sliderPaginationType)
118-
{
119-
// backwards compatibility
120-
$objContent->sliderPaginationType = $objContent->sliderPaginationType === 'progress' ? 'progressbar' : $objContent->sliderPaginationType;
121-
$arrParams['pagination']['type'] = $objContent->sliderPaginationType;
122-
}
123-
}
124-
125-
if ($objContent->sliderScrollbar) {
126-
$arrParams['scrollbar'] = [
127-
'el' => '#' . $swiperId . ' .swiper-scrollbar',
128-
'draggable' => true,
129-
];
130-
}
131-
132-
$arrBreakpoints = StringUtil::deserialize($objContent->sliderBreakpoints, true);
133-
134-
if (!empty($arrBreakpoints)) {
135-
foreach ($arrBreakpoints as $arrBreakpoint) {
136-
$arrSettings = array();
137-
if (is_numeric($arrBreakpoint['slidesPerView']) || 'auto' == $arrBreakpoint['slidesPerView']) {
138-
$arrSettings['slidesPerView'] = is_numeric($arrBreakpoint['slidesPerView']) ? (int)$arrBreakpoint['slidesPerView'] : 'auto';
139-
}
140-
if ($arrBreakpoint['spaceBetween']) {
141-
$arrSettings['spaceBetween'] = (int)$arrBreakpoint['spaceBetween'];
142-
}
143-
if (!empty($arrSettings)) {
144-
$arrParams['breakpoints'][(int)$arrBreakpoint['breakpoint']] = $arrSettings;
145-
}
146-
}
147-
}
148-
149-
if ($objContent->sliderAutoheight) $arrParams['autoHeight'] = true;
150-
151-
if ($objContent->sliderCenteredSlides) {
152-
$arrParams['centeredSlides'] = true;
153-
}
154-
155-
if ($objContent->sliderCustomOptions && null !== ($customOptions = json_decode($objContent->sliderCustomOptions, true))) {
156-
$arrParams = array_replace_recursive($arrParams, $customOptions);
157-
}
158-
159-
$this->Template->sliderButtons = $objContent->sliderButtons;
160-
$this->Template->sliderPagination = $objContent->sliderPagination;
161-
$this->Template->sliderScrollbar = $objContent->sliderScrollbar;
162-
$this->Template->wrapperClass = $objContent->sliderWrapperClass;
163-
$this->Template->parameters = $arrParams;
164-
$this->Template->sliderId = 'swiper-' . $objContent->id; // unique name for an entry in the sliderConfig-variable
165-
166-
// check if the scripts should be combined
167-
$combine = '';
168-
169-
// get the current page
170-
$page = null;
171-
$request = System::getContainer()->get('request_stack')->getCurrentRequest();
172-
if ($request) {
173-
/** @var PageModel $page */
174-
$page = $request->attributes->get('pageModel');
175-
}
176-
// if there is no request or "pageModel" is not part of the request-attributes
177-
// use the $GLOBALS['objPage']
178-
if ($page === null) {
179-
$page = $GLOBALS['objPage'];
180-
}
181-
// check if the page has a layout
182-
if ($page && $page->layout) {
183-
// get the current layout-model of the page
184-
if (null !== ($layout = LayoutModel::findById((int)$page->layout)) && $layout->add_swiper_scripts) {
185-
$combine = '|static';
186-
}
187-
}
188-
189-
// add CSS and JS
190-
$GLOBALS['TL_CSS']['swiper'] = 'bundles/contaoswiper/swiper-bundle.min.css' . $combine;
191-
$GLOBALS['TL_JAVASCRIPT']['swiper'] = 'bundles/contaoswiper/swiper-bundle.min.js' . $combine; // load swiper
192-
$GLOBALS['TL_JAVASCRIPT']['swiper_init'] = 'bundles/contaoswiper/contao-swiper.min.js' . $combine; // load custom script to initialize the sliders
73+
if (null !== $objContent) {
74+
$swiperRenderer = System::getContainer()->get('fritzmg.contao_swiper.renderer');
75+
$swiperRenderer->addParamsToTemplate($this->Template, $objContent);
76+
$swiperRenderer->addAssets();
19377
}
19478
}
19579
}

0 commit comments

Comments
 (0)