custom/plugins/FourtwosixSeparateStreetAndNumber/src/FourtwosixSeparateStreetAndNumber.php line 15

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixSeparateStreetAndNumber;
  3. use FourtwosixSeparateStreetAndNumber\Components\Constants\Constants;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\System\CustomField\CustomFieldTypes;
  12. class FourtwosixSeparateStreetAndNumber extends Plugin
  13. {
  14.     public function install(InstallContext $context): void
  15.     {
  16.         $this->addCustomFields($context->getContext());
  17.     }
  18.     public function uninstall(UninstallContext $context): void
  19.     {
  20.         parent::uninstall($context);
  21.         if ($context->keepUserData()) {
  22.             return;
  23.         }
  24.         $this->removeCustomFields($context->getContext(), [Constants::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET]);
  25.     }
  26.     private function addCustomFields(Context $context): void
  27.     {
  28.         /* Check for snippets if they exist for custom fields */
  29.         $this->checkForExistingCustomFieldSnippets($context);
  30.         $customFieldSet $this->container->get(Constants::CUSTOM_FIELD_SET_REPOSITORY);
  31.         if($customFieldSet->search((new Criteria())->addFilter(new EqualsFilter(Constants::FIELD_NAMEConstants::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET)), $context)->count() == 0) {
  32.             $customFieldSet->create([[
  33.                 Constants::FIELD_NAME => Constants::CUSTOM_FIELD_SET_NAME_ADDRESS_STREET,
  34.                 Constants::FIELD_CONFIG => [
  35.                     Constants::FIELD_LABEL => [
  36.                         Constants::FIELD_LABEL_EN_KEY => Constants::FIELD_LABEL_EN,
  37.                         Constants::FIELD_LABEL_DE_KEY => Constants::FIELD_LABEL_DE,
  38.                         Constants::FIELD_LABEL_IT_KEY => Constants::FIELD_LABEL_IT
  39.                     ]
  40.                 ],
  41.                 Constants::ADDRESS_CUSTOM_FIELD_KEY => [
  42.                     [Constants::FIELD_NAME => Constants::ADDRESS_CUSTOM_FIELD_STREET_KEYConstants::FIELD_TYPE => CustomFieldTypes::TEXT,
  43.                         Constants::FIELD_CONFIG => [
  44.                             Constants::FIELD_COMPONENT_NAME => Constants::FIELD_SW,
  45.                             Constants::FIELD_TYPE => CustomFieldTypes::TEXT,
  46.                             Constants::FIELD_CUSTOM_FIELD_TYPE => CustomFieldTypes::TEXT,
  47.                             Constants::FIELD_CUSTOM_FIELD_POSITION => 1,
  48.                             Constants::FIELD_LABEL => [
  49.                                 Constants::FIELD_LABEL_EN_KEY => Constants::FIELD_LABEL_STREET_CONFIG_EN,
  50.                                 Constants::FIELD_LABEL_DE_KEY => Constants::FIELD_LABEL_STREET_CONFIG_DE,
  51.                                 Constants::FIELD_LABEL_IT_KEY => Constants::FIELD_LABEL_STREET_CONFIG_IT
  52.                             ]
  53.                         ]],
  54.                     [Constants::FIELD_NAME => Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEYConstants::FIELD_TYPE => CustomFieldTypes::TEXT,
  55.                         Constants::FIELD_CONFIG => [
  56.                             Constants::FIELD_COMPONENT_NAME => Constants::FIELD_SW,
  57.                             Constants::FIELD_TYPE => CustomFieldTypes::TEXT,
  58.                             Constants::FIELD_CUSTOM_FIELD_TYPE => CustomFieldTypes::TEXT,
  59.                             Constants::FIELD_CUSTOM_FIELD_POSITION => 1,
  60.                             Constants::FIELD_LABEL => [
  61.                                 Constants::FIELD_LABEL_EN_KEY => Constants::FIELD_LABEL_HOUSE_NUMBER_CONFIG_EN,
  62.                                 Constants::FIELD_LABEL_DE_KEY => Constants::FIELD_LABEL_HOUSE_NUMBER_CONFIG_DE,
  63.                                 Constants::FIELD_LABEL_IT_KEY => Constants::FIELD_LABEL_HOUSE_NUMBER_CONFIG_IT
  64.                             ]
  65.                         ]]
  66.                 ],
  67.             ]], $context);
  68.         };
  69.     }
  70.     private function removeCustomFields(Context $context, array $setNames): void
  71.     {
  72.         /* Check for snippets if they exist for custom fields */
  73.         $this->checkForExistingCustomFieldSnippets($context);
  74.         $customFieldSet $this->container->get(Constants::CUSTOM_FIELD_SET_REPOSITORY);
  75.         foreach ($setNames as $setName) {
  76.             $id $customFieldSet->searchIds((new Criteria())->addFilter(new EqualsFilter(Constants::FIELD_NAME$setName)), $context)->firstId();
  77.             if($id$customFieldSet->delete([[Constants::ID_KEY => $id]], $context);
  78.         }
  79.     }
  80.     private function checkForExistingCustomFieldSnippets(Context $context)
  81.     {
  82.         $snippetRepository $this->container->get(Constants::SNIPPET_REPOSITORY);
  83.         $criteria = new Criteria();
  84.         $criteria->addFilter(new MultiFilter(MultiFilter::CONNECTION_OR, [
  85.             new EqualsFilter(Constants::SNIPPET_TRANSLATION_KEYConstants::ADDRESS_CUSTOM_FIELD_KEY.Constants::POINT.Constants::ADDRESS_CUSTOM_FIELD_STREET_KEY),
  86.             new EqualsFilter(Constants::SNIPPET_TRANSLATION_KEYConstants::ADDRESS_CUSTOM_FIELD_KEY.Constants::POINT.Constants::ADDRESS_CUSTOM_FIELD_HOUSE_NUMBER_KEY)
  87.         ]));
  88.         $searchResult $snippetRepository->search($criteria$context);
  89.         if ($searchResult->count() > 0) {
  90.             $snippetIds = [];
  91.             foreach ($searchResult->getEntities()->getElements() as $snippet) {
  92.                 $snippetIds[] = [
  93.                     Constants::ID_KEY => $snippet->getId()
  94.                 ];
  95.             }
  96.             if (!empty($snippetIds)) {
  97.                 $snippetRepository->delete($snippetIds$context);
  98.             }
  99.         }
  100.     }
  101. }