custom/plugins/econsorsetup/src/EconsorSetup.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace EconsorSetup;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\IdSearchResult;
  9. use Shopware\Core\Framework\Plugin;
  10. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\System\CustomField\CustomFieldTypes;
  13. use Shopware\Core\System\SystemConfig\SystemConfigService;
  14. /** @psalm-suppress PropertyNotSetInConstructor */
  15. class EconsorSetup extends Plugin
  16. {
  17.     public function install(InstallContext $installContext): void
  18.     {
  19.         /** @var SystemConfigService $systemConfigService */
  20.         $systemConfigService $this->container->get(SystemConfigService::class);
  21.         $systemConfigService->set('core.basicInformation.acceptAllCookies'true);
  22.         $this->getCustomFieldSetRepository()->upsert([
  23.             [
  24.                 'name' => 'custom_setup',
  25.                 'config' => [
  26.                     'label' => [
  27.                         'en-GB' => 'ECONSOR Setup',
  28.                         'de-DE' => 'ECONSOR Setup'
  29.                     ],
  30.                 ],
  31.                 'customFields' => [
  32.                     [
  33.                         'name' => 'custom_meta_robots',
  34.                         'type' => CustomFieldTypes::BOOL,
  35.                         'config' => [
  36.                             'componentName' => 'sw-field',
  37.                             'type' => 'checkbox',
  38.                             'customFieldType' => 'checkbox',
  39.                             'customFieldPosition' => 1,
  40.                             'label' => [
  41.                                 'en-GB' => 'Noindex?',
  42.                                 'de-DE' => 'Noindex?'
  43.                             ],
  44.                         ]
  45.                     ],
  46.                 ],
  47.                 'relations' => [
  48.                     [
  49.                         'entityName' => 'sales_channel'
  50.                     ],
  51.                     [
  52.                         'entityName' => 'category'
  53.                     ],
  54.                     [
  55.                         'entityName' => 'product'
  56.                     ],
  57.                 ]
  58.             ]
  59.         ], $installContext->getContext());
  60.     }
  61.     public function uninstall(UninstallContext $uninstallContext): void
  62.     {
  63.         if ($uninstallContext->keepUserData()) {
  64.             parent::uninstall($uninstallContext);
  65.             return;
  66.         }
  67.         $this->removeCustomField($uninstallContext);
  68.         parent::uninstall($uninstallContext);
  69.     }
  70.     private function removeCustomField(UninstallContext $uninstallContext): void
  71.     {
  72.         $fieldIds $this->customFieldsExist($uninstallContext->getContext());
  73.         if ($fieldIds) {
  74.             $this
  75.                 ->getCustomFieldSetRepository()
  76.                 ->delete(array_values($fieldIds->getData()), $uninstallContext->getContext());
  77.         }
  78.     }
  79.     private function customFieldsExist(Context $context): ?IdSearchResult
  80.     {
  81.         $criteria = new Criteria();
  82.         $criteria->addFilter(new EqualsAnyFilter('name', ['custom_setup']));
  83.         $ids $this->getCustomFieldSetRepository()->searchIds($criteria$context);
  84.         return $ids->getTotal() > $ids null;
  85.     }
  86.     private function getCustomFieldSetRepository(): EntityRepository
  87.     {
  88.         $repository $this->container->get('custom_field_set.repository');
  89.         assert($repository instanceof EntityRepository);
  90.         return $repository;
  91.     }
  92. }