vendor/shopware/core/System/Snippet/Subscriber/CustomFieldSubscriber.php line 74

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\System\Snippet\Subscriber;
  3. use Doctrine\DBAL\ArrayParameterType;
  4. use Doctrine\DBAL\Connection;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  9. use Shopware\Core\Framework\Log\Package;
  10. use Shopware\Core\Framework\Uuid\Uuid;
  11. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  12. /**
  13.  * @internal
  14.  */
  15. #[Package('system-settings')]
  16. class CustomFieldSubscriber implements EventSubscriberInterface
  17. {
  18.     private const CUSTOM_FIELD_ID_FIELD 'custom_field_id';
  19.     /**
  20.      * @internal
  21.      */
  22.     public function __construct(private readonly Connection $connection)
  23.     {
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             'custom_field.written' => 'customFieldIsWritten',
  29.             'custom_field.deleted' => 'customFieldIsDeleted',
  30.         ];
  31.     }
  32.     public function customFieldIsWritten(EntityWrittenEvent $event): void
  33.     {
  34.         $snippets = [];
  35.         $snippetSets null;
  36.         foreach ($event->getWriteResults() as $writeResult) {
  37.             if (!isset($writeResult->getPayload()['config']['label']) || empty($writeResult->getPayload()['config']['label'])) {
  38.                 continue;
  39.             }
  40.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT) {
  41.                 if ($snippetSets === null) {
  42.                     $snippetSets $this->connection->fetchAllAssociative('SELECT id, iso FROM snippet_set');
  43.                 }
  44.                 if (empty($snippetSets)) {
  45.                     return;
  46.                 }
  47.                 $this->setInsertSnippets($writeResult$snippetSets$snippets);
  48.             }
  49.         }
  50.         if (empty($snippets)) {
  51.             return;
  52.         }
  53.         foreach ($snippets as $snippet) {
  54.             $this->connection->executeStatement(
  55.                 'INSERT INTO snippet (`id`, `snippet_set_id`, `translation_key`, `value`, `author`, `custom_fields`, `created_at`)
  56.                       VALUES (:id, :setId, :translationKey, :value, :author, :customFields, :createdAt)
  57.                       ON DUPLICATE KEY UPDATE `value` = :value',
  58.                 $snippet
  59.             );
  60.         }
  61.     }
  62.     public function customFieldIsDeleted(EntityDeletedEvent $event): void
  63.     {
  64.         $this->connection->executeStatement(
  65.             'DELETE FROM `snippet`
  66.             WHERE JSON_EXTRACT(`custom_fields`, "$.custom_field_id") IN (:customFieldIds)',
  67.             ['customFieldIds' => $event->getIds()],
  68.             ['customFieldIds' => ArrayParameterType::STRING]
  69.         );
  70.     }
  71.     /**
  72.      * @param list<array<string, string>> $snippetSets
  73.      * @param array<string, mixed> $snippets
  74.      */
  75.     private function setInsertSnippets(EntityWriteResult $writeResult, array $snippetSets, array &$snippets): void
  76.     {
  77.         $name $writeResult->getPayload()['name'];
  78.         $labels $writeResult->getPayload()['config']['label'];
  79.         foreach ($snippetSets as $snippetSet) {
  80.             $label $name;
  81.             $iso $snippetSet['iso'];
  82.             if (isset($labels[$iso])) {
  83.                 $label $labels[$iso];
  84.             }
  85.             $snippets[] = [
  86.                 'id' => Uuid::randomBytes(),
  87.                 'setId' => $snippetSet['id'],
  88.                 'translationKey' => 'customFields.' $name,
  89.                 'value' => $label,
  90.                 'author' => 'System',
  91.                 'customFields' => json_encode([
  92.                     self::CUSTOM_FIELD_ID_FIELD => $writeResult->getPrimaryKey(),
  93.                 ], \JSON_THROW_ON_ERROR),
  94.                 'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  95.             ];
  96.         }
  97.     }
  98. }