custom/plugins/fourtwosixAttributeBadges/src/fourtwosixAttributeBadges.php line 18

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace fourtwosix\AttributeBadges;
  4. use Shopware\Core\Framework\Plugin;
  5. use Shopware\Core\Content\Product\ProductDefinition;
  6. use Shopware\Core\Content\Property\PropertyGroupDefinition;
  7. use Shopware\Core\Content\Rule\RuleDefinition;
  8. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  9. use Shopware\Core\Framework\Uuid\Uuid;
  10. use Shopware\Core\System\CustomField\CustomFieldTypes;
  11. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. class fourtwosixAttributeBadges extends Plugin
  15. {
  16.     public function install(InstallContext $installContext): void
  17.     {
  18.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  19.         $customFieldSetRelationRepository $this->container->get('custom_field_set_relation.repository');
  20.         $id Uuid::randomHex();
  21.         $customFieldSetRepository->create([
  22.             [
  23.                 'id' => $id,
  24.                 'name' => 'attribute_badges_set',
  25.                 'config' => [
  26.                     'label' => [
  27.                         'en-GB' => 'Attribute Badges Set',
  28.                         'de-DE' => 'Attribut Badges Set',
  29.                     ]
  30.                 ],
  31.                 'customFields' => [
  32.                     [
  33.                         'name' => 'attribute_badges',
  34.                         'type' => CustomFieldTypes::BOOL,
  35.                         'config' => [
  36.                             'label' => [
  37.                                 'en-GB' => 'Attribute Badges',
  38.                                 'de-DE' => 'Attribut Badges',
  39.                             ],
  40.                             'customFieldPosition' => 1,
  41.                             'type' => 'checkbox',
  42.                             'componentName' => 'sw-field',
  43.                             'customFieldType' => 'checkbox',
  44.                         ]
  45.                     ]
  46.                 ]
  47.             ]
  48.         ], $installContext->getContext());
  49.         $customFieldSetRelationRepository->create([
  50.             [
  51.                 'customFieldSetId' => $id,
  52.                 'entityName' => PropertyGroupDefinition::ENTITY_NAME
  53.             ]
  54.         ], $installContext->getContext());
  55.     }
  56.     public function uninstall(UninstallContext $uninstallContext): void
  57.     {
  58.         if (!$uninstallContext->keepUserData()) {
  59.             $customFieldSetRepository $this->container->get('custom_field_set.repository');
  60.             $criteria = new Criteria();
  61.             $criteria->addFilter(new EqualsFilter('name''attribute_badges_set'));
  62.             $custumFiledSetId $customFieldSetRepository->search($criteria$uninstallContext->getContext())->first()->getId();
  63.             $customFieldSetRepository->delete([
  64.                 [
  65.                     'id' => $custumFiledSetId
  66.                 ]
  67.             ], $uninstallContext->getContext());
  68.         }
  69.     }
  70. }