custom/plugins/MagNewsletterOverlayBox/src/MagNewsletterOverlayBox.php line 12

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Mag\NewsletterOverlayBox;
  3. use Shopware\Core\System\CustomField\CustomFieldTypes;
  4. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. class MagNewsletterOverlayBox extends Plugin
  10. {
  11.     /**
  12.      * @var EntityRepositoryInterface $customFieldSetRepository
  13.      */
  14.     private $customFieldSetRepository;
  15.     public function install(InstallContext $installContext): void
  16.     {
  17.         parent::install($installContext);
  18.         $this->addCustomFields($installContext);
  19.     }
  20.     /**
  21.      * {@inheritdoc}
  22.      */
  23.     private function addCustomFields(InstallContext $installContext)
  24.     {
  25.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  26.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  27.         $criteria = new Criteria();
  28.         $criteria->addFilter(
  29.             new EqualsFilter('name''mag_newsletter_overlay_box')
  30.         );
  31.         $sets $customFieldSetRepository->search($criteria$installContext->getContext());
  32.         if (!$sets->count()) {
  33.             $customFieldSetRepository->create([[
  34.                 'name' => 'mag_newsletter_overlay_box',
  35.                 'customFields' => [
  36.                     [
  37.                         'name' => 'mag_newsletter_overlay_box_vouchercode',
  38.                         'type' => CustomFieldTypes::TEXT
  39.                     ]
  40.                 ],
  41.                 'relations' => [
  42.                     [
  43.                         'entityName' => 'newsletter_recipient'
  44.                     ]
  45.                 ],
  46.             ]], $installContext->getContext());
  47.         }
  48.     }
  49. }