custom/plugins/S360ListingViewSwitch/src/S360ListingViewSwitch.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace S360ListingViewSwitch;
  3. use Shopware\Core\Framework\Plugin;
  4. // required for all of the fancy stuff we do while installing and uninstalling
  5. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  6. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  7. use Shopware\Core\System\CustomField\CustomFieldTypes;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. class S360ListingViewSwitch extends Plugin
  11. {
  12.     public const FIELDSETNAME 's360_listingviewswitch';
  13.     public const FIELDSETLABEL 'S360 - Listendarstellung';
  14.     public const COOKIE_KEY 's360ListingViewMode';
  15.     public const URI_KEY 's360view';
  16.     public const AVAILABLE_VIEWS = ['gallery''list'];
  17.     public function install(InstallContext $context): void
  18.     {
  19.         // TODO: remove custom field set on category level
  20.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  21.         $customFieldSetRepository->create([[
  22.             'id' => md5(self::FIELDSETNAME),
  23.             'name' => self::FIELDSETNAME,
  24.             'config' => [
  25.                 'label' => self::FIELDSETLABEL
  26.             ],
  27.             'customFields' => [
  28.                 [
  29.                     'name' => self::FIELDSETNAME '_method',
  30.                     'type' => CustomFieldTypes::SELECT,
  31.                     'componentName' => 'sw-single-select',
  32.                     'translated' => false,
  33.                     'config' => [
  34.                         'label' => 'Gallerie oder Liste in dieser Kategorie erzwingen?',
  35.                         'componentName' => 'sw-single-select',
  36.                         'options' => [
  37.                             [
  38.                                 'value' => 'gallery',
  39.                                 'label' => 'Gallery'
  40.                             ],
  41.                             [
  42.                                 'value' => 'list',
  43.                                 'label' => 'List'
  44.                             ]
  45.                         ]
  46.                     ]
  47.                 ]
  48.             ],
  49.             'relations' => [
  50.                 [
  51.                     'entityName' => 'category',
  52.                 ],
  53.             ]
  54.         ]], $context->getContext());
  55.     }
  56.     public function uninstall(UninstallContext $context): void
  57.     {
  58.         if ($context->keepUserData()) {
  59.             return;
  60.         }
  61.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  62.         $criteria = (new Criteria())
  63.             ->addFilter(new EqualsFilter('name'self::FIELDSETNAME));
  64.         // EntitySearchResult
  65.         $result $customFieldSetRepository->search($criteria$context->getContext());
  66.         foreach ($result->getEntities() as $entity) {
  67.             $customFieldSetRepository->delete([[
  68.                 'id' => $entity->getId()
  69.             ]], $context->getContext());
  70.         }
  71.     }
  72. }