custom/plugins/IcebergStorefrontChildTheme/src/IcebergStorefrontChildTheme.php line 19

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace IcebergStorefrontChildTheme;
  3. use Shopware\Core\Framework\Plugin;
  4. use Shopware\Storefront\Framework\ThemeInterface;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  13. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  14. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  15. use Shopware\Core\Framework\Uuid\Uuid;
  16. class IcebergStorefrontChildTheme extends Plugin implements ThemeInterface
  17. {
  18.     public function install(InstallContext $installContext): void
  19.     {
  20.         /**
  21.          * @var EntityRepositoryInterface $customFieldSetRepository
  22.          */
  23.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  24.         $customFieldSetRepository->create($this->getCustomFieldSet(), $installContext->getContext());
  25.     }
  26.     public function update(UpdateContext $updateContext): void
  27.     {
  28.         parent::update($updateContext);
  29.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  30.         $customFieldSetRepository->create($this->getCustomFieldSet(), $updateContext->getContext());
  31.         try {
  32.         }
  33.         catch (\Exception $ex) {
  34.             //
  35.         }
  36.     }
  37.     /**
  38.      * {@inheritDoc}
  39.      *
  40.      * @param UninstallContext $uninstallContext
  41.      * @throws InconsistentCriteriaIdsException
  42.      */
  43.     public function uninstall(UninstallContext $uninstallContext): void
  44.     {
  45.         $this->deleteCustomFieldSet($uninstallContext->getContext());
  46.         $this->deleteCustomFields($uninstallContext->getContext());
  47.     }
  48.     /**
  49.      *
  50.      * @param Context $context
  51.      *
  52.      * @throws InconsistentCriteriaIdsException
  53.      */
  54.     protected function deleteCustomFieldSet($context): void
  55.     {
  56.         /**
  57.          * @var EntityRepositoryInterface $customFieldSetRepository
  58.          */
  59.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  60.         $criteria = new Criteria();
  61.         $criteria->addFilter(
  62.             new EqualsFilter('name''cc_product_downloads'),
  63.             new ContainsFilter('name''cc_product_ingredients')
  64.         );
  65.         $customFieldSetId $customFieldSetRepository->searchIds($criteria$context)->firstId();
  66.         if (!empty($customFieldSetId)) {
  67.             $customFieldSetRepository->delete(
  68.                 [
  69.                     [
  70.                         'id' => $customFieldSetId
  71.                     ]
  72.                 ],
  73.                 $context
  74.             );
  75.         }
  76.     }
  77.     /**
  78.      *
  79.      * @param Context $context
  80.      *
  81.      * @throws InconsistentCriteriaIdsException
  82.      */
  83.     protected function deleteCustomFields($context): void
  84.     {
  85.         /**
  86.          * @var EntityRepositoryInterface $customFieldRepository
  87.          */
  88.         $customFieldRepository $this->container->get('custom_field.repository');
  89.         $criteria = new Criteria();
  90.         $criteria->addFilter(
  91.             new ContainsFilter('name''cc_product_download'),
  92.             new ContainsFilter('name''cc_product_ingredients')
  93.         );
  94.         $ids $customFieldRepository->searchIds($criteria$context)->getIds();
  95.         if (!empty($ids)) {
  96.             $data = [];
  97.             foreach ($ids as $id) {
  98.                 $data[] = [
  99.                     'id' => $id
  100.                 ];
  101.             }
  102.             $customFieldRepository->delete($data$context);
  103.         }
  104.     }
  105.     /**
  106.      *
  107.      * @return array
  108.      */
  109.     protected function getCustomFieldSet(): array
  110.     {
  111.         return [
  112.             [
  113.                 'id' => Uuid::randomHex(),
  114.                 'name' => 'cc_product_ingredients',
  115.                 'config' => [
  116.                     'label' => [
  117.                         'de-DE' => 'Product Tab Ingredients',
  118.                         'en-GB' => 'Product Tab Ingredients',
  119.                     ],
  120.                 ],
  121.                 'customFields' => [
  122.                     [
  123.                         'name' => 'cc_product_ingredients_html',
  124.                         'type' => 'html',
  125.                         'config' => [
  126.                             'customFieldPosition' => 1,
  127.                             'componentName' => 'sw-field',
  128.                             'customFieldType' => 'textEditor',
  129.                             'placeholder' => [
  130.                                 'en-GB' => 'Paste the Html content here ...',
  131.                                 'de-DE' => 'Fügen Sie den HTML-Inhalt hier ein ...',
  132.                             ],
  133.                             'label' => [
  134.                                 'en-GB' => 'Product Tab Ingredients Html',
  135.                                 'de-DE' => 'Produkt Tab Inhaltsstoffe Html',
  136.                             ],
  137.                         ],
  138.                     ],
  139.                 ],
  140.                 'relations' => [
  141.                     [
  142.                         'entityName' =>  $this->container->get(ProductDefinition::class)->getEntityName()
  143.                     ]
  144.                 ]
  145.             ]
  146.         ];
  147.     }
  148. }