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

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace LoyxxFullWidthProductListing;
  3. use Shopware\Core\Framework\Context;
  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\InstallContext;
  8. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  9. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  10. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  11. use Shopware\Core\System\CustomField\CustomFieldEntity;
  12. use Shopware\Core\System\CustomField\CustomFieldTypes;
  13. class LoyxxFullWidthProductListing extends Plugin
  14. {
  15.     /**
  16.      * Bundle Name
  17.      * @var string
  18.      */
  19.     public const BUNDLE_NAME 'LoyxxFullWidthProductListing';
  20.     /**
  21.      * Author Name
  22.      * @var string
  23.      */
  24.     public const AUTHOR 'Loy GmbH';
  25.     /**
  26.      * CustomFieldSet Name
  27.      * @var string
  28.      */
  29.     public const PRODUCT_CUSTOM_FIELD_GROUP 'loyxx_product_list';
  30.     /**
  31.      * Product Benefits custom field
  32.      * @var string
  33.      */
  34.     public const PRODUCT_CUSTOM_FIELD 'loyxx_product_list_benefits';
  35.     /**
  36.      *
  37.      */
  38.     public const PRODUCT_CUSTOM_FIELD_BENEFITS_LIMIT 5;
  39.     public function install(InstallContext $installContext): void
  40.     {
  41.         parent::install($installContext);
  42.         $this->setupCustomField($installContext->getContext());
  43.     }
  44.     public function update(UpdateContext $updateContext): void
  45.     {
  46.         parent::update($updateContext);
  47.         $this->setupCustomField($updateContext->getContext());
  48.     }
  49.     private function setupCustomField(Context $context)
  50.     {
  51.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  52.         //check if the custom field set is defined
  53.         $criteria = new Criteria();
  54.         $criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
  55.         $criteria->addAssociation('customFields');
  56.         /** @var CustomFieldSetEntity $customFieldSetEntity */
  57.         $customFieldSetEntity $customFieldSetRepository->search($criteria$context)->first();
  58.         $customFields = [];
  59.         $customFieldSet = [
  60.             'name' => static::PRODUCT_CUSTOM_FIELD_GROUP,
  61.         ];
  62.         for ($i 1$i <= static::PRODUCT_CUSTOM_FIELD_BENEFITS_LIMIT$i++) {
  63.             $customFields[] = [
  64.                 'name' => static::PRODUCT_CUSTOM_FIELD '_' $i,
  65.                 'type' => CustomFieldTypes::TEXT,
  66.                 'config' => [
  67.                     'type' => 'text',
  68.                     'label' => [
  69.                         'en-GB' => 'Product Benefit ' $i,
  70.                         'de-DE' => 'Produktvorteile ' $i
  71.                     ],
  72.                     'componentName' => 'sw-field',
  73.                     'customFieldType' => 'text',
  74.                     'customFieldPosition' => $i,
  75.                     'translated' => true
  76.                 ]
  77.             ];
  78.         }
  79.         if ($customFieldSetEntity === null) {
  80.             $customFieldSet array_merge($customFieldSet, [
  81.                 'customFields' => $customFields,
  82.                 'relations' => [
  83.                     ['entityName' => 'product']
  84.                 ],
  85.                 'config' => [
  86.                     'description' => [
  87.                         'en-GB' => 'Product Benefits',
  88.                         'de-DE' => 'Produktvorteile'
  89.                     ],
  90.                     'label' => [
  91.                         'en-GB' => 'Product Benefits',
  92.                         'de-DE' => 'Produktvorteile'
  93.                     ],
  94.                     'translated' => true
  95.                 ]
  96.             ]);
  97.         } else {
  98.             /** @var CustomFieldEntity $item */
  99.             foreach ($customFieldSetEntity->getCustomFields() as $item) {
  100.                 $key array_search($item->getName(), array_column($customFields'name'));
  101.                 if ($key !== FALSE) {
  102.                     $customFields[$key]['id'] = $item->getId();
  103.                 }
  104.             }
  105.             $customFieldSet array_merge($customFieldSet, [
  106.                 'id' => $customFieldSetEntity->getId(),
  107.                 'customFields' => $customFields
  108.             ]);
  109.         }
  110.         $customFieldSetRepository->upsert(
  111.             [$customFieldSet],
  112.             $context
  113.         );
  114.     }
  115.     public function uninstall(UninstallContext $uninstallContext): void
  116.     {
  117.         parent::uninstall($uninstallContext);
  118.         if ($uninstallContext->keepUserData()) {
  119.             return;
  120.         }
  121.         $this->removeSeminarCustomField($uninstallContext);
  122.     }
  123.     private function removeSeminarCustomField(UninstallContext $uninstallContext)
  124.     {
  125.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  126.         $context $uninstallContext->getContext();
  127.         //check if the custom field set is defined
  128.         $criteria = new Criteria();
  129.         $criteria->addFilter(new EqualsFilter('name', static::PRODUCT_CUSTOM_FIELD_GROUP));
  130.         $customFieldSetEntity $customFieldSetRepository->searchIds($criteria$context);
  131.         if ($customFieldSetEntity->getTotal()) {
  132.             $customFieldSetRepository->delete(array_values($customFieldSetEntity->getData()), $context);
  133.         }
  134.     }
  135. }