<?php declare(strict_types=1);
namespace Nimbits\NimbitsArticlePDFsNext\Subscriber;
use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\Content\Product\ProductEvents;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryForwardCompatibilityDecorator;
use Shopware\Core\Content\Cms\CmsPageEntity;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpKernel\Event\KernelEvent;
use Shopware\Storefront\Event\StorefrontRenderEvent;
use Shopware\Storefront\Pagelet\Footer\FooterPageletLoadedEvent;
use Nimbits\NimbitsArticlePDFsNext\Setting\Service\SettingService;
use Shopware\Core\Content\Media\DataAbstractionLayer\MediaRepositoryDecorator;
use Shopware\Core\Framework\Struct\ArrayEntity;
class Subscriber implements EventSubscriberInterface
{
/** @var SettingService $settingsService */
protected $settingsService;
/** @var EntityRepositoryForwardCompatibilityDecorator $mediaRepository */
protected $mediaRepository;
public function __construct(
SettingService $settingsService,
EntityRepositoryForwardCompatibilityDecorator $mediaRepository
)
{
$this->settingsService = $settingsService;
$this->mediaRepository = $mediaRepository;
}
public static function getSubscribedEvents(): array
{
// Return the events to listen to as array like this: <event to listen to> => <method to execute>
return [
ProductPageLoadedEvent::class => 'onProductPageLoaded'
];
}
public function onProductPageLoaded(ProductPageLoadedEvent $event)
{
$customFields = $event->getPage()->getProduct()->getCustomFields();
$ids = [];
for($i = 1; $i <= 10; $i++)
{
if(!isset($customFields['custom_nimbits_pdftab'.$i.'_media']))
continue;
$ids[] = $customFields['custom_nimbits_pdftab'.$i.'_media'];
}
if(!empty($ids)){
/** @var EntityCollection $entities */
$mediaFiles = $this->mediaRepository->search(
new Criteria($ids),
\Shopware\Core\Framework\Context::createDefaultContext()
);
$event->getPage()->getProduct()->addExtension('articlepdfs', new ArrayEntity($mediaFiles->getElements()));
}
else{
$event->getPage()->getProduct()->addExtension('articlepdfs', new ArrayEntity([]));
}
$event->getPage()->getProduct()->addExtension('nimbitsArticlePdfSettings', $this->settingsService->getSettingsAsStruct($event->getSalesChannelContext()->getSalesChannel()->getId()));
}
}
?>