src/Core/System/Snippet/Subscriber/CustomFieldSubscriber.php line 37

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