Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WidgetInterface to this module #37

Merged
merged 8 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions config/common.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ services:
parent: 'oksydan.is_imageslider.hook.abstract_cacheable_display_hook'
public: true

# Modification of the module into a widget
oksydan.is_imageslider.hook.widget_capability:
class: Oksydan\IsImageslider\Hook\WidgetCapability
parent: 'oksydan.is_imageslider.hook.abstract_cacheable_display_hook'
public: true

oksydan.is_imageslider.cache.template_cache:
class: Oksydan\IsImageslider\Cache\TemplateCache
public: true
Expand Down
17 changes: 16 additions & 1 deletion is_imageslider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
use Oksydan\IsImageslider\Hook\HookInterface;
use Oksydan\IsImageslider\Installer\ImageSliderInstaller;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Module\WidgetInterface;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;

class Is_imageslider extends Module
class Is_imageslider extends Module implements WidgetInterface
{
public $multistoreCompatibility = self::MULTISTORE_COMPATIBILITY_YES;

Expand Down Expand Up @@ -140,4 +141,18 @@ private function getHookObject($methodName)

return $hook instanceof HookInterface ? $hook : null;
}

public function renderWidget($hookName, array $configuration)
{
$widgetCapability = $this->get('oksydan.is_imageslider.hook.widget_capability');

return $widgetCapability->renderWidget($configuration);
}

public function getWidgetVariables($hookName, array $configuration)
{
$widgetCapability = $this->get('oksydan.is_imageslider.hook.widget_capability');

return $widgetCapability->getWidgetVariables($configuration);
}
}
61 changes: 61 additions & 0 deletions src/Hook/WidgetCapability.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Oksydan\IsImageslider\Hook;

class WidgetCapability extends AbstractCacheableDisplayHook
{
private const TEMPLATE_FILE = 'slider.tpl';

protected function getTemplate(): string
{
return WidgetCapability::TEMPLATE_FILE;
}

protected function getCacheKey(): string
{
return parent::getCacheKey() . '_' . ($this->context->isMobile() ? 'mobile' : 'desktop');
}

/**
* @return array
*/
private function getSlides(): array
{
$now = new \DateTime();
$slides = $this->slideRepository->getActiveSliderByLandAndStoreId(
$this->context->language->id,
$this->context->shop->id,
true,
0, // 0 means no limit
$now
);

foreach ($slides as &$slide) {
$slide = $this->slidePresenter->present($slide);
}

return $slides;
}

public function getWidgetVariables($params): array
{
return [
'homeslider' => [
'slides' => $this->getSlides(),
'speed' => $this->sliderConfiguration->getSliderSpeed(),
'pause' => $this->sliderConfiguration->getSliderPauseOnHover(),
'wrap' => $this->sliderConfiguration->getSliderWrap(),
],
];
}

protected function assignTemplateVariables(array $params)
{
$this->context->smarty->assign($this->getWidgetVariables($params));
}

public function renderWidget($params): string
{
return $this->execute($params);
}
}