custom/plugins/EconsorAtBitConfiguratorConnector/src/AtBitConfiguratorConnector.php line 19

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Econsor\Shopware\AtBitConfiguratorConnector;
  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. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. class AtBitConfiguratorConnector extends Plugin
  16. {
  17.     public function install(InstallContext $installContext): void
  18.     {
  19.         if($this->customFieldsExist($installContext->getContext())) {
  20.             return;
  21.         }
  22.         assert($this->container instanceof ContainerInterface);
  23.         /** @var SystemConfigService $systemConfigService */
  24.         $systemConfigService $this->container->get(SystemConfigService::class);
  25.         $this->getCustomFieldSetRepository()->upsert([
  26.             [
  27.                 'name' => 'ec_atbit',
  28.                 'config' => [
  29.                     'label' => [
  30.                         'en-GB' => 'ECONSOR AtBit Integration',
  31.                         'de-DE' => 'ECONSOR AtBit Integration'
  32.                     ],
  33.                 ],
  34.                 'customFields' => [
  35.                     [
  36.                         'name' => 'ec_atbit_enable',
  37.                         'type' => CustomFieldTypes::BOOL,
  38.                         'config' => [
  39.                             'componentName' => 'sw-field',
  40.                             'type' => 'checkbox',
  41.                             'customFieldType' => 'checkbox',
  42.                             'customFieldPosition' => 1,
  43.                             'label' => [
  44.                                 'en-GB' => 'Enable AtBit Link?',
  45.                                 'de-DE' => 'AtBit Verlinkung aktivieren?'
  46.                             ],
  47.                         ]
  48.                     ],
  49.                     [
  50.                         'name' => 'ec_atbit_name',
  51.                         'type' => CustomFieldTypes::TEXT,
  52.                         'config' => [
  53.                             'componentName' => 'sw-field',
  54.                             'customFieldPosition' => 1,
  55.                             'customFieldType' => CustomFieldTypes::TEXT,
  56.                             'type' => CustomFieldTypes::TEXT,
  57.                             'label' => [
  58.                                 'en-GB' => 'AtBit Name (For URL)',
  59.                                 'de-DE' => 'AtBit Name (Für URL)'
  60.                             ]
  61.                         ]
  62.                     ]
  63.                 ],
  64.                 'relations' => [
  65.                     [
  66.                         'entityName' => 'product'
  67.                     ],
  68.                 ]
  69.             ]
  70.         ], $installContext->getContext());
  71.     }
  72.     public function uninstall(UninstallContext $uninstallContext): void
  73.     {
  74.         if ($uninstallContext->keepUserData()) {
  75.             parent::uninstall($uninstallContext);
  76.             return;
  77.         }
  78.         $this->removeCustomField($uninstallContext);
  79.         parent::uninstall($uninstallContext);
  80.     }
  81.     private function removeCustomField(UninstallContext $uninstallContext): void
  82.     {
  83.         $fieldIds $this->customFieldsExist($uninstallContext->getContext());
  84.         if ($fieldIds) {
  85.             $cFieldRepo $this->getCustomFieldSetRepository();
  86.             $fields array_values($fieldIds->getData());
  87.             $cFieldRepo->delete($fields$uninstallContext->getContext());
  88.         }
  89.     }
  90.     private function customFieldsExist(Context $context): ?IdSearchResult
  91.     {
  92.         $criteria = new Criteria();
  93.         $criteria->addFilter(new EqualsAnyFilter('name', ['ec_atbit']));
  94.         $ids $this->getCustomFieldSetRepository()->searchIds($criteria$context);
  95.         return $ids->getTotal() > $ids null;
  96.     }
  97.     private function getCustomFieldSetRepository(): EntityRepository
  98.     {
  99.         $container $this->container;
  100.         assert($container instanceof ContainerInterface);
  101.         $repository $container->get('custom_field_set.repository');
  102.         assert($repository instanceof EntityRepository);
  103.         return $repository;
  104.     }
  105. }