custom/plugins/NxsDifferentProductListingImage/src/NxsDifferentProductListingImage.php line 14

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nxs\NxsDifferentProductListingImage;
  3. use Nxs\NxsDifferentProductListingImage\Resources\config\ConfigInterface;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Indexing\EntityIndexerRegistry;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\Plugin;
  8. use Shopware\Core\Framework\Plugin\Context\{ActivateContextUninstallContextInstallContext};
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. class NxsDifferentProductListingImage extends Plugin implements ConfigInterface
  12. {
  13.     /**
  14.      * @param ActivateContext $activateContext
  15.      * @throws \Exception
  16.      */
  17.     public function activate(ActivateContext $activateContext): void
  18.     {
  19.         $indexerMessageSender $this->container->get(EntityIndexerRegistry::class);
  20.         $indexerMessageSender->sendIndexingMessage(['product.indexer']);
  21.     }
  22.     /**
  23.      * @param Plugin\Context\InstallContext $installContext
  24.      * @throws InconsistentCriteriaIdsException
  25.      */
  26.     public function install(InstallContext $installContext): void
  27.     {
  28.         parent::install($installContext);
  29.         $criteria = new Criteria();
  30.         $criteria->setTerm(self::CUSTOM_FIELD_SET_NAME);
  31.         $customSet $this->container->get('custom_field_set.repository')->search($criteria$installContext->getContext());
  32.         if ($customSet->getTotal() === 0) {
  33.             $this->container->get('custom_field_set_relation.repository')->create($this->getFields(), $installContext->getContext());
  34.         } else {
  35.             $this->container->get('custom_field_set_relation.repository')->create($this->getFields(), $installContext->getContext());
  36.         }
  37.     }
  38.     /**
  39.      * @return array
  40.      */
  41.     private function getFields(): array
  42.     {
  43.         return [[
  44.             'id' => Uuid::randomHex(),
  45.             'entityName' => 'product',
  46.             'customFieldSet' => [
  47.                 'name' => self::CUSTOM_FIELD_SET_NAME,
  48.                 'config' => [
  49.                     'label' => [
  50.                         'de-DE' => 'NXS Listenseiten Attribute',
  51.                         'en-GB' => 'NXS Listingpage Attributes'
  52.                     ],
  53.                     'translated' => true
  54.                 ],
  55.                 'customFields' => [
  56.                     [
  57.                         'name' => self::CUSTOM_FIELD_NAME,
  58.                         'type' => CustomFieldTypes::TEXT,
  59.                         'config' => [
  60.                             'type' => 'media',
  61.                             'label' => [
  62.                                 'de-DE' => 'Listenseiten Bild',
  63.                                 'en-GB' => 'Listing Image'
  64.                             ],
  65.                             'placeholder' => [
  66.                                 'de-DE' => '0',
  67.                                 'en-GB' => '0'
  68.                             ],
  69.                             'componentName' => 'sw-media-field',
  70.                             'customFieldType' => 'media',
  71.                             'customFieldPosition' => 1
  72.                         ]
  73.                     ]
  74.                 ]
  75.             ]]
  76.         ];
  77.     }
  78.     public function uninstall(UninstallContext $uninstallContext): void
  79.     {
  80.         parent::uninstall($uninstallContext);
  81.         $customFieldSetCriteria = new Criteria();
  82.         $customFieldSetCriteria->setTerm(self::CUSTOM_FIELD_SET_NAME);
  83.         $customFieldCriteria = new Criteria();
  84.         $customFieldCriteria->setTerm(self::CUSTOM_FIELD_NAME);
  85.         $customField $this->container->get('custom_field.repository')->search($customFieldCriteria$uninstallContext->getContext());
  86.         $customFieldSet $this->container->get('custom_field_set.repository')->search($customFieldSetCriteria$uninstallContext->getContext());
  87.         if ($customField->getTotal() !== 0) {
  88.             foreach ($customField->getEntities() as $entity){
  89.                 $this->container->get('custom_field.repository')->delete([['id' => $entity->getId()]], $uninstallContext->getContext());
  90.             }
  91.         }
  92.         if ($customFieldSet->getTotal() !== 0) {
  93.             foreach ($customFieldSet->getEntities() as $entity){
  94.                 $this->container->get('custom_field_set.repository')->delete([['id' => $entity->getId()]], $uninstallContext->getContext());
  95.             }
  96.         }
  97.     }
  98. }