<?php declare(strict_types=1);
namespace S360ListingViewSwitch;
use Shopware\Core\Framework\Plugin;
// required for all of the fancy stuff we do while installing and uninstalling
use Shopware\Core\Framework\Plugin\Context\InstallContext;
use Shopware\Core\Framework\Plugin\Context\UninstallContext;
use Shopware\Core\System\CustomField\CustomFieldTypes;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
class S360ListingViewSwitch extends Plugin
{
public const FIELDSETNAME = 's360_listingviewswitch';
public const FIELDSETLABEL = 'S360 - Listendarstellung';
public const COOKIE_KEY = 's360ListingViewMode';
public const URI_KEY = 's360view';
public const AVAILABLE_VIEWS = ['gallery', 'list'];
public function install(InstallContext $context): void
{
// TODO: remove custom field set on category level
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$customFieldSetRepository->create([[
'id' => md5(self::FIELDSETNAME),
'name' => self::FIELDSETNAME,
'config' => [
'label' => self::FIELDSETLABEL
],
'customFields' => [
[
'name' => self::FIELDSETNAME . '_method',
'type' => CustomFieldTypes::SELECT,
'componentName' => 'sw-single-select',
'translated' => false,
'config' => [
'label' => 'Gallerie oder Liste in dieser Kategorie erzwingen?',
'componentName' => 'sw-single-select',
'options' => [
[
'value' => 'gallery',
'label' => 'Gallery'
],
[
'value' => 'list',
'label' => 'List'
]
]
]
]
],
'relations' => [
[
'entityName' => 'category',
],
]
]], $context->getContext());
}
public function uninstall(UninstallContext $context): void
{
if ($context->keepUserData()) {
return;
}
$customFieldSetRepository = $this->container->get('custom_field_set.repository');
$criteria = (new Criteria())
->addFilter(new EqualsFilter('name', self::FIELDSETNAME));
// EntitySearchResult
$result = $customFieldSetRepository->search($criteria, $context->getContext());
foreach ($result->getEntities() as $entity) {
$customFieldSetRepository->delete([[
'id' => $entity->getId()
]], $context->getContext());
}
}
}