1414use Contao \BackendTemplate ;
1515use Contao \ContentElement ;
1616use Contao \ContentModel ;
17- use Contao \LayoutModel ;
18- use Contao \PageModel ;
19- use Contao \StringUtil ;
2017use 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