<?php
declare(strict_types=1);
namespace WbfkExtensions\CmsElementResolver;
use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
use Shopware\Core\Content\Cms\DataResolver\CriteriaCollection;
use Shopware\Core\Content\Cms\DataResolver\Element\ElementDataCollection;
use Shopware\Core\Content\Cms\DataResolver\FieldConfig;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\EntityResolverContext;
use Shopware\Core\Content\Cms\DataResolver\ResolverContext\ResolverContext;
use Shopware\Core\Content\Media\Cms\AbstractDefaultMediaResolver;
use Shopware\Core\Content\Media\MediaDefinition;
use Shopware\Core\Content\Media\MediaEntity;
use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilderInterface;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\System\SystemConfig\SystemConfigService;
class ImageProductSliderElementResolver extends ProductSliderCmsElementResolver
{
use SliderElementResolverTrait;
protected const ELEMENT_TYPE = 'image-product-slider';
private AbstractDefaultMediaResolver $mediaResolver;
public function __construct(ProductStreamBuilderInterface $productStreamBuilder, SystemConfigService $systemConfigService, AbstractDefaultMediaResolver $mediaResolver)
{
parent::__construct($productStreamBuilder, $systemConfigService);
$this->mediaResolver = $mediaResolver;
}
public function getType(): string
{
return static::ELEMENT_TYPE;
}
public function collect(CmsSlotEntity $slot, ResolverContext $resolverContext): ?CriteriaCollection
{
$collection = parent::collect($slot, $resolverContext);
$mediaConfig = $slot->getFieldConfig()->get('media');
if (!(
$mediaConfig === null
|| $mediaConfig->isMapped()
|| $mediaConfig->isDefault()
|| $mediaConfig->getValue() === null
)) {
$criteria = new Criteria([$mediaConfig->getStringValue()]);
$collection->add('media_' . $slot->getUniqueIdentifier(), MediaDefinition::class, $criteria);
}
return $collection->all() ? $collection : null;
}
public function enrich(CmsSlotEntity $slot, ResolverContext $resolverContext, ElementDataCollection $result): void
{
parent::enrich($slot, $resolverContext, $result);
$additionalDate = [];
$fieldConfig = $slot->getFieldConfig();
$searchResult = $result->get('media_' . $slot->getUniqueIdentifier());
if ($searchResult !== null) {
$mediaConfig = $fieldConfig->get('media');
if ($mediaConfig && $mediaConfig->getValue()) {
$this->addMediaEntity($slot, $additionalDate, $result, $mediaConfig, $resolverContext);
}
}
/*
* Additional Slider Configurations
*/
$isWbfkTwoRow = $fieldConfig->get('boxLayout')->getValue() === 'wbfkTwoRows';
$isWithImage = !empty($additionalDate['media']);
$responsive = [
'xs' => $isWbfkTwoRow ? 2 : 1,
'sm' => $isWbfkTwoRow ? 3 : 2,
'md' => $isWithImage ? ($isWbfkTwoRow ? 2 : 1) : ($isWbfkTwoRow ? 4 : 2),
'lg' => $isWithImage ? ($isWbfkTwoRow ? 2 : 1) : ($isWbfkTwoRow ? 6 : 3),
'xl' => $isWithImage ? ($isWbfkTwoRow ? 3 : 2) : ($isWbfkTwoRow ? 8 : 4),
];
$this->getSliderConfig($additionalDate, $fieldConfig, $responsive);;
$slot->assign($additionalDate);
}
private function addMediaEntity(
CmsSlotEntity $slot,
array &$additionalDate,
ElementDataCollection $result,
FieldConfig $config,
ResolverContext $resolverContext
): void {
if ($config->isDefault()) {
$media = $this->mediaResolver->getDefaultCmsMediaEntity($config->getStringValue());
if ($media) {
$additionalDate['media'] = $media;
}
}
if ($config->isMapped() && $resolverContext instanceof EntityResolverContext) {
/** @var MediaEntity|null $media */
$media = $this->resolveEntityValue($resolverContext->getEntity(), $config->getStringValue());
if ($media !== null) {
$additionalDate['mediaId'] = $media->getUniqueIdentifier();
$additionalDate['media'] = $media;
}
}
if ($config->isStatic()) {
$additionalDate['mediaId'] = $config->getStringValue();
$searchResult = $result->get('media_' . $slot->getUniqueIdentifier());
if (!$searchResult) {
return;
}
/** @var MediaEntity|null $media */
$media = $searchResult->get($config->getValue());
if (!$media) {
return;
}
$additionalDate['media'] = $media;
}
}
}