custom/plugins/MillProductDownloadsTab/src/MillProductDownloadsTab.php line 16

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Mill\ProductDownloadsTab;
  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 MillProductDownloadsTab extends Plugin
  14. {
  15.     public function install(InstallContext $installContext): void
  16.     {
  17.         /**
  18.          * @var EntityRepositoryInterface $customFieldSetRepository
  19.          */
  20.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  21.         $customFieldSetRepository->create($this->getCustomFieldSet(), $installContext->getContext());
  22.     }
  23.     /**
  24.      * {@inheritDoc}
  25.      *
  26.      * @param UninstallContext $uninstallContext
  27.      * @throws InconsistentCriteriaIdsException
  28.      */
  29.     public function uninstall(UninstallContext $uninstallContext): void
  30.     {
  31.         $this->deleteCustomFieldSet($uninstallContext->getContext());
  32.         $this->deleteCustomFields($uninstallContext->getContext());
  33.     }
  34.     /**
  35.      * Helper-function to delete the advantage custom field set
  36.      *
  37.      * @param Context $context
  38.      *
  39.      * @throws InconsistentCriteriaIdsException
  40.      */
  41.     protected function deleteCustomFieldSet($context): void
  42.     {
  43.         /**
  44.          * @var EntityRepositoryInterface $customFieldSetRepository
  45.          */
  46.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  47.         $criteria = new Criteria();
  48.         $criteria->addFilter(
  49.             new EqualsFilter('name''mill_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.         /**
  73.          * @var EntityRepositoryInterface $customFieldRepository
  74.          */
  75.         $customFieldRepository $this->container->get('custom_field.repository');
  76.         $criteria = new Criteria();
  77.         $criteria->addFilter(
  78.             new ContainsFilter('name''mill_product_download')
  79.         );
  80.         $ids $customFieldRepository->searchIds($criteria$context)->getIds();
  81.         if (!empty($ids)) {
  82.             $data = [];
  83.             foreach ($ids as $id) {
  84.                 $data[] = [
  85.                     'id' => $id
  86.                 ];
  87.             }
  88.             $customFieldRepository->delete($data$context);
  89.         }
  90.     }
  91.     /**
  92.      * Helper-function to get the required custom field set with it's fields
  93.      *
  94.      * @return array
  95.      */
  96.     protected function getCustomFieldSet(): array
  97.     {
  98.         return [
  99.             [
  100.                 'name' => 'mill_product_downloads',
  101.                 'config' => [
  102.                     'label' => [
  103.                         'de-DE' => 'Downloads',
  104.                         'en-GB' => 'Downloads',
  105.                     ],
  106.                 ],
  107.                 'customFields' => [
  108.                     [
  109.                         'name' => 'mill_product_download_1',
  110.                         'type' => 'media',
  111.                         'config' => [
  112.                             'customFieldPosition' => 1,
  113.                             'componentName' => 'sw-media-field',
  114.                             'customFieldType' => 'media',
  115.                             'label' => [
  116.                                 'de-DE' => 'Download 1',
  117.                                 'en-GB' => 'Download 1',
  118.                             ],
  119.                         ],
  120.                     ],
  121.                     [
  122.                         'name' => 'mill_product_download_2',
  123.                         'type' => 'media',
  124.                         'config' => [
  125.                             'customFieldPosition' => 2,
  126.                             'componentName' => 'sw-media-field',
  127.                             'customFieldType' => 'media',
  128.                             'label' => [
  129.                                 'de-DE' => 'Download 2',
  130.                                 'en-GB' => 'Download 2',
  131.                             ],
  132.                         ],
  133.                     ],
  134.                     [
  135.                         'name' => 'mill_product_download_3',
  136.                         'type' => 'media',
  137.                         'config' => [
  138.                             'customFieldPosition' => 3,
  139.                             'componentName' => 'sw-media-field',
  140.                             'customFieldType' => 'media',
  141.                             'label' => [
  142.                                 'de-DE' => 'Download 3',
  143.                                 'en-GB' => 'Download 3',
  144.                             ],
  145.                         ],
  146.                     ],
  147.                     [
  148.                         'name' => 'mill_product_download_4',
  149.                         'type' => 'media',
  150.                         'config' => [
  151.                             'customFieldPosition' => 4,
  152.                             'componentName' => 'sw-media-field',
  153.                             'customFieldType' => 'media',
  154.                             'label' => [
  155.                                 'de-DE' => 'Download 4',
  156.                                 'en-GB' => 'Download 4',
  157.                             ],
  158.                         ],
  159.                     ],
  160.                     [
  161.                         'name' => 'mill_product_download_5',
  162.                         'type' => 'media',
  163.                         'config' => [
  164.                             'customFieldPosition' => 5,
  165.                             'componentName' => 'sw-media-field',
  166.                             'customFieldType' => 'media',
  167.                             'label' => [
  168.                                 'de-DE' => 'Download 5',
  169.                                 'en-GB' => 'Download 5',
  170.                             ],
  171.                         ],
  172.                     ],
  173.                     [
  174.                         'name' => 'mill_product_download_6',
  175.                         'type' => 'media',
  176.                         'config' => [
  177.                             'customFieldPosition' => 6,
  178.                             'componentName' => 'sw-media-field',
  179.                             'customFieldType' => 'media',
  180.                             'label' => [
  181.                                 'de-DE' => 'Download 6',
  182.                                 'en-GB' => 'Download 6',
  183.                             ],
  184.                         ],
  185.                     ],
  186.                     [
  187.                         'name' => 'mill_product_download_7',
  188.                         'type' => 'media',
  189.                         'config' => [
  190.                             'customFieldPosition' => 7,
  191.                             'componentName' => 'sw-media-field',
  192.                             'customFieldType' => 'media',
  193.                             'label' => [
  194.                                 'de-DE' => 'Download 7',
  195.                                 'en-GB' => 'Download 7',
  196.                             ],
  197.                         ],
  198.                     ],
  199.                     [
  200.                         'name' => 'mill_product_download_8',
  201.                         'type' => 'media',
  202.                         'config' => [
  203.                             'customFieldPosition' => 8,
  204.                             'componentName' => 'sw-media-field',
  205.                             'customFieldType' => 'media',
  206.                             'label' => [
  207.                                 'de-DE' => 'Download 8',
  208.                                 'en-GB' => 'Download 8',
  209.                             ],
  210.                         ],
  211.                     ],
  212.                     [
  213.                         'name' => 'mill_product_download_9',
  214.                         'type' => 'media',
  215.                         'config' => [
  216.                             'customFieldPosition' => 9,
  217.                             'componentName' => 'sw-media-field',
  218.                             'customFieldType' => 'media',
  219.                             'label' => [
  220.                                 'de-DE' => 'Download 9',
  221.                                 'en-GB' => 'Download 9',
  222.                             ],
  223.                         ],
  224.                     ],
  225.                     [
  226.                         'name' => 'mill_product_download_10',
  227.                         'type' => 'media',
  228.                         'config' => [
  229.                             'customFieldPosition' => 10,
  230.                             'componentName' => 'sw-media-field',
  231.                             'customFieldType' => 'media',
  232.                             'label' => [
  233.                                 'de-DE' => 'Download 10',
  234.                                 'en-GB' => 'Download 10',
  235.                             ],
  236.                         ],
  237.                     ]
  238.                 ],
  239.                 'relations' => [
  240.                     [
  241.                         'entityName' =>  $this->container->get(ProductDefinition::class)->getEntityName()
  242.                     ]
  243.                 ]
  244.             ]
  245.         ];
  246.     }
  247. }