custom/plugins/LinkMobilityShopwareSmsApiPlugin/src/LinkMobilityShopwareSmsApiPlugin.php line 20

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace LinkMobility\ShopwareSmsApiPlugin;
  4. if (file_exists(dirname(__DIR__) . '/vendor/autoload.php')) {
  5.     require_once dirname(__DIR__) . '/vendor/autoload.php';
  6. }
  7. use Shopware\Core\Checkout\Customer\CustomerDefinition;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Plugin;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  14. use Shopware\Core\System\CustomField\CustomFieldTypes;
  15. class LinkMobilityShopwareSmsApiPlugin extends Plugin
  16. {
  17.     public const CUSTOM_FIELD_SET_ID '6548179eb61a4e6a895badc15b2d7196';
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         parent::install($installContext);
  21.         $this->createCustomFields($installContext->getContext());
  22.     }
  23.     public function uninstall(UninstallContext $uninstallContext): void
  24.     {
  25.         parent::uninstall($uninstallContext);
  26.         if ($uninstallContext->keepUserData()) {
  27.             return;
  28.         }
  29.         $this->removeCustomFields($uninstallContext->getContext());
  30.     }
  31.     private function createCustomFields(Context $context): void
  32.     {
  33.         if ($this->customFieldSetExists($context)) {
  34.             return;
  35.         }
  36.         /** @var EntityRepository $customFieldSetRepository */
  37.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  38.         $customFieldSetRepository->create([
  39.             [
  40.                 'id' => self::CUSTOM_FIELD_SET_ID,
  41.                 'name' => 'marketingConsent',
  42.                 'global' => true,
  43.                 'config' => [
  44.                     'label' => [
  45.                         'en-GB' => 'Consent',
  46.                     ],
  47.                 ],
  48.                 'customFields' => [
  49.                     [
  50.                         'name' => 'link_mobility_shopware_sms_api_plugin_consent',
  51.                         'type' => CustomFieldTypes::BOOL,
  52.                         'config' => [
  53.                             'label' => [
  54.                                 'en-GB' => 'I consent for marketing',
  55.                             ],
  56.                             'customFieldPosition' => 1,
  57.                         ],
  58.                     ],
  59.                     [
  60.                         'name' => 'link_mobility_shopware_sms_api_plugin_customer_id',
  61.                         'type' => CustomFieldTypes::TEXT,
  62.                         'config' => [
  63.                             'label' => [
  64.                                 'en-GB' => 'customerSmsApiId',
  65.                             ],
  66.                             'customFieldPosition' => 2,
  67.                         ],
  68.                     ],
  69.                 ],
  70.                 'relations' => [
  71.                     [
  72.                         'entityName' => CustomerDefinition::ENTITY_NAME,
  73.                     ],
  74.                 ],
  75.             ],
  76.         ], $context);
  77.     }
  78.     private function removeCustomFields(Context $context): void
  79.     {
  80.         if (!$this->customFieldSetExists($context)) {
  81.             return;
  82.         }
  83.         /** @var EntityRepository $customFieldSetRepository */
  84.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  85.         $customFieldSetRepository->delete([
  86.             ['id' => self::CUSTOM_FIELD_SET_ID],
  87.         ], $context);
  88.     }
  89.     private function customFieldSetExists(Context $context): bool
  90.     {
  91.         /** @var EntityRepository $customFieldSetRepository */
  92.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  93.         return null !== $customFieldSetRepository->search(new Criteria([self::CUSTOM_FIELD_SET_ID]), $context)->first();
  94.     }
  95. }