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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace PixAdditionalProductDescription;
  3. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  6. use Shopware\Core\Framework\Plugin;
  7. use Shopware\Core\Framework\Plugin\Context\ActivateContext;
  8. use Shopware\Core\Framework\Plugin\Context\DeactivateContext;
  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 PixAdditionalProductDescription extends Plugin
  13. {
  14.     /**
  15.      * @param InstallContext $context
  16.      */
  17.     public function install(InstallContext $context): void
  18.     {
  19.         parent::install($context);
  20.         $this->addCustomFields($context);
  21.     }
  22.     /**
  23.      * @param UninstallContext $context
  24.      */
  25.     public function uninstall(UninstallContext $context): void
  26.     {
  27.         // Only set the payment method to inactive when uninstalling. Removing the payment method would
  28.         // cause data consistency issues, since the payment method might have been used in several orders
  29.         $this->deleteCustomFields($context);
  30.     }
  31.     /**
  32.      * @param ActivateContext $context
  33.      */
  34.     public function activate(ActivateContext $context): void
  35.     {
  36.         parent::activate($context);
  37.     }
  38.     /**
  39.      * @param DeactivateContext $context
  40.      */
  41.     public function deactivate(DeactivateContext $context): void
  42.     {
  43.         parent::deactivate($context);
  44.     }
  45.     /**
  46.      * @return string|null
  47.      */
  48.     private function customFieldExists($context$name): ?string
  49.     {
  50.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  51.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  52.         $criteria = (new Criteria())->addFilter(new EqualsFilter('name'$name));
  53.         return $customFieldSetRepository->searchIds($criteria$context->getContext())->firstId();
  54.     }
  55.     private function deleteCustomFields($context)
  56.     {
  57.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  58.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  59.         $customFieldsetId $this->customFieldExists($context'detail_additional_description');
  60.         if ($customFieldsetId) {
  61.             $customFieldSetRepository->delete([
  62.                 ['id' => $customFieldsetId],
  63.             ],$context->getContext());
  64.         }
  65.     }
  66.     private function addCustomFields($context)
  67.     {
  68.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  69.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  70.         if ($this->customFieldExists($context'detail_additional_description')) {
  71.             return;
  72.         }
  73.         $customFieldSetRepository->upsert([
  74.             [
  75.                 'name' => 'detail_additional_description',
  76.                 'config' => [
  77.                     'label' => [
  78.                         'en-GB' => 'Additional Description',
  79.                         'de-DE' => 'Zusätzliche Beschreibung',
  80.                     ],
  81.                 ],
  82.                 'customFields' => [
  83.                     [
  84.                         'name' => 'content',
  85.                         'type' => CustomFieldTypes::TEXT,
  86.                         'config' => [
  87.                             'label' => [
  88.                                 'en-GB' => 'Content',
  89.                                 'de-DE' => 'Inhalt',
  90.                             ],
  91.                             'componentName' => 'sw-text-editor',
  92.                             'customFieldType' => 'textEditor',
  93.                             'customFieldPosition' => 1,
  94.                         ],
  95.                     ],
  96.                 ],
  97.                 'relations' => [
  98.                     [
  99.                         'entityName' => 'product',
  100.                     ],
  101.                 ],
  102.             ],
  103.         ], $context->getContext());
  104.     }
  105. }