custom/plugins/ScopProductlistBanner/src/ScopProductlistBanner.php line 21

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Scop\ScopProductlistBanner;
  3. use Doctrine\DBAL\Connection;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  8. use Shopware\Core\Framework\Plugin;
  9. use Shopware\Core\Framework\Plugin\Context\InstallContext;
  10. use Shopware\Core\Framework\Plugin\Context\UninstallContext;
  11. use Shopware\Core\Framework\Plugin\Context\UpdateContext;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSet\CustomFieldSetEntity;
  14. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSetRelation\CustomFieldSetRelationCollection;
  15. use Shopware\Core\System\CustomField\Aggregate\CustomFieldSetRelation\CustomFieldSetRelationEntity;
  16. use Shopware\Core\System\CustomField\CustomFieldEntity;
  17. use Shopware\Core\System\CustomField\CustomFieldTypes;
  18. class ScopProductlistBanner extends Plugin
  19. {
  20.     /**
  21.      * @param InstallContext $installContext
  22.      */
  23.     public function install(InstallContext $installContext): void
  24.     {
  25.         parent::install($installContext);
  26.         $this->createAttributes($installContext->getContext());
  27.     }
  28.     /**
  29.      * @param UpdateContext $updateContext
  30.      */
  31.     public function update(UpdateContext $updateContext): void
  32.     {
  33.         parent::update($updateContext);
  34.         $this->createAttributes($updateContext->getContext());
  35.     }
  36.     public function uninstall(UninstallContext $uninstallContext): void
  37.     {
  38.         parent::uninstall($uninstallContext);
  39.         if (!$uninstallContext->keepUserData()) {
  40.             $this->deleteAttributes($uninstallContext->getContext());
  41.         }
  42.     }
  43.     /**
  44.      * @param Context $installUpdateContext
  45.      */
  46.     protected function createAttributes(Context $installUpdateContext): void
  47.     {
  48.         $context Context::createDefaultContext();
  49.         $cfSet $this->createFieldsSet('scop_productlist_banner', ['Produktlisten Banner''productlist banner'], 'category'$context);
  50.         /** @var EntityRepositoryInterface $customFieldRepository */
  51.         $customFieldRepository $this->container->get('custom_field.repository');
  52.         $this->createField([
  53.             'name' => 'scop_productlist_banner_active',
  54.             'type' => CustomFieldTypes::SWITCH,
  55.             'customFieldSetId' => $cfSet,
  56.             'config' => [
  57.                 'customFieldPosition' => 1,
  58.                 'label' => [
  59.                     'de-DE' => 'Produktlisten Banner aktiv',
  60.                     'en-GB' => 'productlist banner active'
  61.                 ],
  62.             ],
  63.         ], $customFieldRepository$context);
  64.         $this->createField([
  65.             'name' => 'scop_productlist_banner_image',
  66.             'type' => CustomFieldTypes::MEDIA,
  67.             'customFieldSetId' => $cfSet,
  68.             'config' => [
  69.                 'customFieldPosition' => 2,
  70.                 "componentName" => "sw-media-field",
  71.                 "customFieldType" => "media",
  72.                 'label' => [
  73.                     'de-DE' => 'Banner Bild',
  74.                     'en-GB' => 'banner image'
  75.                 ],
  76.             ],
  77.         ], $customFieldRepository$context);
  78.         $this->createField([
  79.             'name' => 'scop_productlist_banner_headline',
  80.             'type' => CustomFieldTypes::HTML,
  81.             'customFieldSetId' => $cfSet,
  82.             'config' => [
  83.                 'customFieldPosition' => 3,
  84.                 'label' => [
  85.                     'de-DE' => 'Banner Überschrift',
  86.                     'en-GB' => 'banner headline'
  87.                 ],
  88.                 'helpText' => [
  89.                     'de-DE' => htmlspecialchars('Die Überschrift des Banners. Kann <br> enthalten, um eine neue Zeile anzufangen.'),
  90.                     'en-GB' => htmlspecialchars('The headline of the banner. Could contain <br> for a new line.')
  91.                 ]
  92.             ],
  93.         ], $customFieldRepository$context);
  94.         $this->createField([
  95.             'name' => 'scop_productlist_banner_headline_color',
  96.             'type' => CustomFieldTypes::TEXT,
  97.             'customFieldSetId' => $cfSet,
  98.             'config' => [
  99.                 'customFieldPosition' => 4,
  100.                 'type' => 'colorpicker',
  101.                 'customFieldType' => 'colorpicker',
  102.                 'label' => [
  103.                     'de-DE' => 'Banner Überschrift Farbe',
  104.                     'en-GB' => 'banner headline color'
  105.                 ],
  106.             ],
  107.         ], $customFieldRepository$context);
  108.         $this->createField([
  109.             'name' => 'scop_productlist_banner_text',
  110.             'type' => CustomFieldTypes::HTML,
  111.             'customFieldSetId' => $cfSet,
  112.             'config' => [
  113.                 'customFieldPosition' => 5,
  114.                 'label' => [
  115.                     'de-DE' => 'Banner Text',
  116.                     'en-GB' => 'banner text'
  117.                 ],
  118.                 'helpText' => [
  119.                     'de-DE' => htmlspecialchars('Der Text des Banners. Kann <br> enthalten, um eine neue Zeile anzufangen.'),
  120.                     'en-GB' => htmlspecialchars('The text of the banner. Could contain <br> for a new line.')
  121.                 ]
  122.             ],
  123.         ], $customFieldRepository$context);
  124.         $this->createField([
  125.             'name' => 'scop_productlist_banner_text_color',
  126.             'type' => CustomFieldTypes::TEXT,
  127.             'customFieldSetId' => $cfSet,
  128.             'config' => [
  129.                 'customFieldPosition' => 6,
  130.                 'type' => 'colorpicker',
  131.                 'customFieldType' => 'colorpicker',
  132.                 'label' => [
  133.                     'de-DE' => 'Banner Text Farbe',
  134.                     'en-GB' => 'banner text color'
  135.                 ],
  136.             ],
  137.         ], $customFieldRepository$context);
  138.         $this->createField([
  139.             'name' => 'scop_productlist_banner_button_link',
  140.             'type' => CustomFieldTypes::TEXT,
  141.             'customFieldSetId' => $cfSet,
  142.             'config' => [
  143.                 'customFieldPosition' => 7,
  144.                 'label' => [
  145.                     'de-DE' => 'Button Link',
  146.                     'en-GB' => 'button link'
  147.                 ],
  148.             ],
  149.         ], $customFieldRepository$context);
  150.         $this->createField([
  151.             'name' => 'scop_productlist_banner_button_text',
  152.             'type' => CustomFieldTypes::TEXT,
  153.             'customFieldSetId' => $cfSet,
  154.             'config' => [
  155.                 'customFieldPosition' => 8,
  156.                 'label' => [
  157.                     'de-DE' => 'Button Text',
  158.                     'en-GB' => 'button text'
  159.                 ],
  160.             ],
  161.         ], $customFieldRepository$context);
  162.         $this->createField([
  163.             'name' => 'scop_productlist_banner_button_open_in_new_tab',
  164.             'type' => CustomFieldTypes::SWITCH,
  165.             'customFieldSetId' => $cfSet,
  166.             'config' => [
  167.                 'customFieldPosition' => 9,
  168.                 'label' => [
  169.                     'de-DE' => 'Link in neuem Tab öffnen',
  170.                     'en-GB' => 'open link in new tab'
  171.                 ],
  172.             ],
  173.         ], $customFieldRepository$context);
  174.         $this->createField([
  175.             'name' => 'scop_productlist_banner_position',
  176.             'type' => CustomFieldTypes::INT,
  177.             'customFieldSetId' => $cfSet,
  178.             'config' => [
  179.                 'customFieldPosition' => 10,
  180.                 'type' => 'number',
  181.                 'numberType' => 'int',
  182.                 'min' => 1,
  183.                 'label' => [
  184.                     'de-DE' => 'Banner Position',
  185.                     'en-GB' => 'banner position'
  186.                 ],
  187.             ],
  188.         ], $customFieldRepository$context);
  189.     }
  190.     /**
  191.      * @param string $setName
  192.      * @param array $setLabel
  193.      * @param string $entityName
  194.      * @param Context $context
  195.      * @return string
  196.      */
  197.     protected function createFieldsSet(string $setName, array $setLabelstring $entityNameContext $context): string
  198.     {
  199.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  200.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  201.         $criteria = new Criteria();
  202.         $criteria->addFilter(new EqualsFilter('name'$setName))
  203.             ->addAssociation('relations');
  204.         /** @var CustomFieldSetEntity|null $cfSet */
  205.         $cfSet $customFieldSetRepository->search($criteria$context)->first();
  206.         $assignedToOrder false;
  207.         if (null !== $cfSet) {
  208.             $cfSetId $cfSet->getId();
  209.             /** @var CustomFieldSetRelationCollection<CustomFieldSetRelationEntity> $relations */
  210.             $relations $cfSet->getRelations();
  211.             if (null !== $relations) {
  212.                 /** @var CustomFieldSetRelationEntity $relation */
  213.                 foreach ($relations as $relation) {
  214.                     if ($relation->getEntityName() === $entityName) {
  215.                         $assignedToOrder true;
  216.                         break;
  217.                     }
  218.                 }
  219.             }
  220.         } else {
  221.             $cfSetId Uuid::randomHex();
  222.         }
  223.         $setData = [
  224.             'name' => $setName,
  225.             'id' => $cfSetId,
  226.             'config' => [
  227.                 'label' => [
  228.                     'de-DE' => $setLabel[0],
  229.                     'en-GB' => $setLabel[1]
  230.                 ]
  231.             ],
  232.         ];
  233.         if (false === $assignedToOrder) {
  234.             $setData['relations'] = [['entityName' => $entityName]];
  235.         }
  236.         $customFieldSetRepository->upsert([$setData], $context);
  237.         return $cfSetId;
  238.     }
  239.     /**
  240.      * @param array<string, mixed> $data
  241.      * @param EntityRepositoryInterface $customFieldRepository
  242.      * @param Context $context
  243.      */
  244.     protected function createField(array $dataEntityRepositoryInterface $customFieldRepositoryContext $context): void
  245.     {
  246.         $criteria = new Criteria();
  247.         $criteria->addFilter(
  248.             new EqualsFilter('name'$data['name'])
  249.         );
  250.         /** @var CustomFieldEntity|null $fieldEntity */
  251.         $fieldEntity $customFieldRepository->search($criteria$context)->first();
  252.         $data['id'] = $fieldEntity $fieldEntity->getId() : Uuid::randomHex();
  253.         $customFieldRepository->upsert([$data], $context);
  254.     }
  255.     /**
  256.      * @param Context $uninstallUpdateContext
  257.      */
  258.     protected function deleteAttributes(Context $uninstallUpdateContext): void
  259.     {
  260.         /** @var EntityRepositoryInterface $customFieldSetRepository */
  261.         $customFieldSetRepository $this->container->get('custom_field_set.repository');
  262.         $context Context::createDefaultContext();
  263.         $criteria = new Criteria();
  264.         $criteria->addFilter(new EqualsFilter('name''scop_productlist_banner'));
  265.         /** @var array<string> $ids */
  266.         $ids $customFieldSetRepository->searchIds($criteria$context)->getIds();
  267.         if (!empty($ids)) {
  268.             $ids array_map(static function (string $id): array {
  269.                 return ['id' => $id];
  270.             }, $ids);
  271.             $customFieldSetRepository->delete($ids$context);
  272.         }
  273.         /**
  274.          *
  275.          * @var Connection $connection
  276.          */
  277.         $connection $this->container->get(Connection::class);
  278.         $sql "DELETE FROM snippet WHERE translation_key LIKE 'customFields.scop_productlist_banner_%'";
  279.         $connection->executeUpdate($sql);
  280.     }
  281. }