vendor/store.shopware.com/memopostcodeplugin/src/memoPostcodePlugin.php line 21

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Memo\PostcodenlPlugin;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Checkout\Customer\Aggregate\CustomerAddress\CustomerAddressDefinition;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Framework\Adapter\Cache\CacheClearer;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  13. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  14. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  15. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  16. use Shopware\Core\Framework\Uuid\Uuid;
  17. use Shopware\Core\System\CustomField\CustomFieldTypes;
  18. class memoPostcodePlugin extends Plugin
  19. {
  20.     public function install(InstallContext $installContext): void
  21.     {
  22.         parent::install($installContext);
  23.         $this->createCustomFields();
  24.     }
  25.     public function uninstall(UninstallContext $uninstallContext): void
  26.     {
  27.         $this->deleteCustomFields();
  28.         parent::uninstall($uninstallContext);
  29.         if ($uninstallContext->keepUserData()) {
  30.             return;
  31.         }
  32.     }
  33.     private function createCustomFields()
  34.     {
  35.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  36.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  37.         $postcodeUuid Uuid::randomHex();
  38.         $customFieldSetRepository->upsert([
  39.             [
  40.                 'id' => $postcodeUuid,
  41.                 'name' => 'postcodenl',
  42.                 'config' => [
  43.                     'label' => [
  44.                         'en-GB' => 'Postcode NL',
  45.                         'nl-NL' => 'Postcode NL'
  46.                     ]
  47.                 ],
  48.                 'customFields' => [
  49.                     [
  50.                         'id' => Uuid::randomHex(),
  51.                         'name' => 'postcodenl_streetname',
  52.                         'type' => CustomFieldTypes::TEXT,
  53.                         'config' => [
  54.                             'componentName' => 'sw-text',
  55.                             'customFieldType' => 'text',
  56.                             'customFieldPosition' => 1,
  57.                             'disabled' => true,
  58.                             'label' => [
  59.                                 'en-GB' => 'Street',
  60.                                 'nl-NL' => 'Straat'
  61.                             ]
  62.                         ]
  63.                     ],
  64.                     [
  65.                         'id' => Uuid::randomHex(),
  66.                         'name' => 'postcodenl_housenumber',
  67.                         'type' => CustomFieldTypes::TEXT,
  68.                         'config' => [
  69.                             'componentName' => 'sw-text',
  70.                             'customFieldType' => 'text',
  71.                             'customFieldPosition' => 2,
  72.                             'disabled' => true,
  73.                             'label' => [
  74.                                 'en-GB' => 'Housenumber',
  75.                                 'nl-NL' => 'Huisnummer'
  76.                             ]
  77.                         ]
  78.                     ],
  79.                     [
  80.                         'id' => Uuid::randomHex(),
  81.                         'name' => 'postcodenl_housenumber_addition',
  82.                         'type' => CustomFieldTypes::TEXT,
  83.                         'config' => [
  84.                             'componentName' => 'sw-text',
  85.                             'customFieldType' => 'text',
  86.                             'customFieldPosition' => 3,
  87.                             'disabled' => true,
  88.                             'label' => [
  89.                                 'en-GB' => 'Housenumber Addition',
  90.                                 'nl-NL' => 'Huisnummer Toevoeging'
  91.                             ]
  92.                         ]
  93.                     ],
  94.                     [
  95.                         'id' => Uuid::randomHex(),
  96.                         'name' => 'postcodenl_zipcode',
  97.                         'type' => CustomFieldTypes::TEXT,
  98.                         'config' => [
  99.                             'componentName' => 'sw-text',
  100.                             'customFieldType' => 'text',
  101.                             'customFieldPosition' => 4,
  102.                             'disabled' => true,
  103.                             'label' => [
  104.                                 'en-GB' => 'Zipcode',
  105.                                 'nl-NL' => 'Postcode'
  106.                             ]
  107.                         ]
  108.                     ],
  109.                     [
  110.                         'id' => Uuid::randomHex(),
  111.                         'name' => 'postcodenl_city',
  112.                         'type' => CustomFieldTypes::TEXT,
  113.                         'config' => [
  114.                             'componentName' => 'sw-text',
  115.                             'customFieldType' => 'text',
  116.                             'customFieldPosition' => 5,
  117.                             'disabled' => true,
  118.                             'label' => [
  119.                                 'en-GB' => 'City',
  120.                                 'nl-NL' => 'Plaats'
  121.                             ]
  122.                         ]
  123.                     ],
  124.                     [
  125.                         'id' => Uuid::randomHex(),
  126.                         'name' => 'postcodenl_autocomplete_support',
  127.                         'type' => CustomFieldTypes::JSON,
  128.                         'config' => [
  129.                             'componentName' => 'sw-hidden',
  130.                             'customFieldType' => 'text',
  131.                             'customFieldPosition' => 10,
  132.                             'disabled' => true,
  133.                             'label' => [
  134.                                 'en-GB' => 'Autocomplete support',
  135.                                 'nl-NL' => 'Autocomplete support'
  136.                             ]
  137.                         ]
  138.                     ]
  139.                 ],
  140.                 'relations' => [
  141.                     [
  142.                         'id' => $postcodeUuid,
  143.                         'entityName' => $this->container->get(CustomerAddressDefinition::class)->getEntityName()
  144.                     ],
  145.                 ]
  146.             ]
  147.         ], Context::createDefaultContext());
  148.     }
  149.     private function deleteCustomFields()
  150.     {
  151.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  152.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  153.         $entityIds $customFieldSetRepository->search(
  154.             (new Criteria())->addFilter(new EqualsFilter('name''postcodenl')),
  155.             Context::createDefaultContext()
  156.         )->getEntities()->getIds();
  157.         if (count($entityIds) < 1) {
  158.             return;
  159.         }
  160.         $entityIds array_map(function ($element) {
  161.             return ['id' => $element];
  162.         }, array_values($entityIds));
  163.         $customFieldSetRepository->delete(
  164.             $entityIds,
  165.             Context::createDefaultContext()
  166.         );
  167.     }
  168. }