custom/plugins/AcrisSeparateStreetCS/src/AcrisSeparateStreetCS.php line 17

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\SeparateStreet;
  3. use Shopware\Core\Framework\Context;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  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\Snippet\SnippetEntity;
  14. class AcrisSeparateStreetCS extends Plugin
  15. {
  16.     const CUSTOM_FIELD_SET_NAME_ADDRESS_STREET 'acris_separate_street_address';
  17.     public function install(InstallContext $context): void
  18.     {
  19.         $this->addCustomFields($context->getContext());
  20.     }
  21.     public function uninstall(UninstallContext $context): void
  22.     {
  23.         parent::uninstall($context);
  24.         if ($context->keepUserData()) {
  25.             return;
  26.         }
  27.         $this->removeCustomFields($context->getContext(), [self::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET]);
  28.     }
  29.     /**
  30.      * @param Context $context
  31.      */
  32.     private function addCustomFields(Context $context): void
  33.     {
  34.         /* Check for snippets if they exist for custom fields */
  35.         $this->checkForExistingCustomFieldSnippets($context);
  36.         $customFieldSet $this->container->get('custom_field_set.repository');
  37.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter('name'self::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET)), $context)->count() == 0) {
  38.             $customFieldSet->create([[
  39.                 'name' => self::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET,
  40.                 'config' => [
  41.                     'label' => [
  42.                         'en-GB' => 'ACRIS Separate Street Address',
  43.                         'de-DE' => 'ACRIS Getrennte Straßenadresse'
  44.                     ]
  45.                 ],
  46.                 'customFields' => [
  47.                     ['name' => 'acris_separate_street_address_street''type' => CustomFieldTypes::TEXT,
  48.                         'config' => [
  49.                             'componentName' => 'sw-field',
  50.                             'type' => CustomFieldTypes::TEXT,
  51.                             'customFieldType' => CustomFieldTypes::TEXT,
  52.                             'customFieldPosition' => 1,
  53.                             'label' => [
  54.                                 'en-GB' => 'Street',
  55.                                 'de-DE' => 'Straße'
  56.                             ]
  57.                         ]],
  58.                     ['name' => 'acris_separate_street_address_house_number''type' => CustomFieldTypes::TEXT,
  59.                         'config' => [
  60.                             'componentName' => 'sw-field',
  61.                             'type' => CustomFieldTypes::TEXT,
  62.                             'customFieldType' => CustomFieldTypes::TEXT,
  63.                             'customFieldPosition' => 1,
  64.                             'label' => [
  65.                                 'en-GB' => 'House number',
  66.                                 'de-DE' => 'Hausnummer'
  67.                             ]
  68.                         ]]
  69.                 ],
  70.             ]], $context);
  71.         };
  72.     }
  73.     /**
  74.      * @param Context $context
  75.      * @param array $setNames
  76.      */
  77.     private function removeCustomFields(Context $context, array $setNames): void
  78.     {
  79.         /* Check for snippets if they exist for custom fields */
  80.         $this->checkForExistingCustomFieldSnippets($context);
  81.         $customFieldSet $this->container->get('custom_field_set.repository');
  82.         foreach ($setNames as $setName) {
  83.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter('name'$setName)), $context)->firstId();
  84.             if($id$customFieldSet->delete([['id' => $id]], $context);
  85.         }
  86.     }
  87.     private function checkForExistingCustomFieldSnippets(Context $context)
  88.     {
  89.         /** @var EntityRepositoryInterface $snippetRepository */
  90.         $snippetRepository $this->container->get('snippet.repository');
  91.         $criteria = new Criteria();
  92.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  93.             new EqualsFilter('translationKey''customFields.' 'acris_separate_street_address_street'),
  94.             new EqualsFilter('translationKey''customFields.' 'acris_separate_street_address_house_number')
  95.         ]));
  96.         /** @var EntitySearchResult $searchResult */
  97.         $searchResult $snippetRepository->search($criteria$context);
  98.         if ($searchResult->count() > 0) {
  99.             $snippetIds = [];
  100.             /** @var SnippetEntity $snippet */
  101.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  102.                 $snippetIds[] = [
  103.                     'id' => $snippet->getId()
  104.                 ];
  105.             }
  106.             if (!empty($snippetIds)) {
  107.                 $snippetRepository->delete($snippetIds$context);
  108.             }
  109.         }
  110.     }
  111. }