custom/static-plugins/product-downloads/src/WexoProductDownloads.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Wexo\ProductDownloads;
  3. use Shopware\Core\Content\Product\ProductDefinition;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\ContainsFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\Plugin;
  11. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  12. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  13. class WexoProductDownloads extends Plugin
  14. {
  15.     public function getParent()
  16.     {
  17.         // TODO: Implement getParent() method.
  18.     }
  19.     public function install(InstallContext $installContext): void
  20.     {
  21.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  22.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  23.         $customFieldSetRepository->create($this->getCustomFieldSet(), $installContext->getContext());
  24.     }
  25.     /**
  26.      * {@inheritDoc}
  27.      *
  28.      * @param UninstallContext $uninstallContext
  29.      * @throws InconsistentCriteriaIdsException
  30.      */
  31.     public function uninstall(UninstallContext $uninstallContext): void
  32.     {
  33.         $this->deleteCustomFieldSet($uninstallContext->getContext());
  34.         $this->deleteCustomFields($uninstallContext->getContext());
  35.     }
  36.     /**
  37.      * Helper-function to delete the advantage custom field set
  38.      *
  39.      * @param Context $context
  40.      *
  41.      * @throws InconsistentCriteriaIdsException
  42.      */
  43.     protected function deleteCustomFieldSet($context): void
  44.     {
  45.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  46.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  47.         $criteria = new Criteria();
  48.         $criteria->addFilter(
  49.             new EqualsFilter('name''wexo_product_downloads')
  50.         );
  51.         $customFieldSetId $customFieldSetRepository->searchIds($criteria$context)->firstId();
  52.         if (!empty($customFieldSetId)) {
  53.             $customFieldSetRepository->delete(
  54.                 [
  55.                     [
  56.                         'id' => $customFieldSetId
  57.                     ]
  58.                 ],
  59.                 $context
  60.             );
  61.         }
  62.     }
  63.     /**
  64.      * Helper-function to delete all relevant plugin custom fields
  65.      *
  66.      * @param Context $context
  67.      *
  68.      * @throws InconsistentCriteriaIdsException
  69.      */
  70.     protected function deleteCustomFields($context): void
  71.     {
  72.         /** @var EntityRepositoryInterface $customFieldRepository */
  73.         $customFieldRepository $this->container->get('custom_field.repository');
  74.         $criteria = new Criteria();
  75.         $criteria->addFilter(
  76.             new ContainsFilter('name''wexo_product_downloads')
  77.         );
  78.         $ids $customFieldRepository->searchIds($criteria$context)->getIds();
  79.         if (!empty($ids)) {
  80.             $data = [];
  81.             foreach ($ids as $id) {
  82.                 $data[] = [
  83.                     'id' => $id
  84.                 ];
  85.             }
  86.             $customFieldRepository->delete($data$context);
  87.         }
  88.     }
  89.     /**
  90.      * Helper-function to get the required custom field set with it's fields
  91.      *
  92.      * @return array
  93.      */
  94.     protected function getCustomFieldSet(): array
  95.     {
  96.         return [
  97.             [
  98.                 'name' => 'wexo_product_downloads',
  99.                 'config' => [
  100.                     'label' => [
  101.                         'da-DK' => 'Downloads',
  102.                         'en-GB' => 'Downloads',
  103.                     ],
  104.                 ],
  105.                 'customFields' => [
  106.                     [
  107.                         'name' => 'wexo_product_download_1',
  108.                         'type' => 'media',
  109.                         'config' => [
  110.                             'customFieldPosition' => 1,
  111.                             'componentName' => 'sw-media-field',
  112.                             'customFieldType' => 'media',
  113.                             'label' => [
  114.                                 'da-DK' => 'Download 1',
  115.                                 'en-GB' => 'Download 1',
  116.                             ],
  117.                         ],
  118.                     ],
  119.                     [
  120.                         'name' => 'wexo_product_download_2',
  121.                         'type' => 'media',
  122.                         'config' => [
  123.                             'customFieldPosition' => 2,
  124.                             'componentName' => 'sw-media-field',
  125.                             'customFieldType' => 'media',
  126.                             'label' => [
  127.                                 'da-DK' => 'Download 2',
  128.                                 'en-GB' => 'Download 2',
  129.                             ],
  130.                         ],
  131.                     ],
  132.                     [
  133.                         'name' => 'wexo_product_download_3',
  134.                         'type' => 'media',
  135.                         'config' => [
  136.                             'customFieldPosition' => 3,
  137.                             'componentName' => 'sw-media-field',
  138.                             'customFieldType' => 'media',
  139.                             'label' => [
  140.                                 'da-DK' => 'Download 3',
  141.                                 'en-GB' => 'Download 3',
  142.                             ],
  143.                         ],
  144.                     ],
  145.                     [
  146.                         'name' => 'wexo_product_download_4',
  147.                         'type' => 'media',
  148.                         'config' => [
  149.                             'customFieldPosition' => 4,
  150.                             'componentName' => 'sw-media-field',
  151.                             'customFieldType' => 'media',
  152.                             'label' => [
  153.                                 'da-DK' => 'Download 4',
  154.                                 'en-GB' => 'Download 4',
  155.                             ],
  156.                         ],
  157.                     ],
  158.                     [
  159.                         'name' => 'wexo_product_download_5',
  160.                         'type' => 'media',
  161.                         'config' => [
  162.                             'customFieldPosition' => 5,
  163.                             'componentName' => 'sw-media-field',
  164.                             'customFieldType' => 'media',
  165.                             'label' => [
  166.                                 'da-DK' => 'Download 5',
  167.                                 'en-GB' => 'Download 5',
  168.                             ],
  169.                         ],
  170.                     ],
  171.                 ],
  172.                 'relations' => [
  173.                     [
  174.                         'entityName' =>  $this->container->get(ProductDefinition::class)->getEntityName()
  175.                     ]
  176.                 ]
  177.             ]
  178.         ];
  179.     }
  180. }