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

Open in your IDE?
  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\Uuid\Uuid;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class CustomFieldSubscriber implements EventSubscriberInterface
  11. {
  12.     private const CUSTOM_FIELD_ID_FIELD 'custom_field_id';
  13.     /**
  14.      * @var Connection
  15.      */
  16.     private $connection;
  17.     /**
  18.      * @internal
  19.      */
  20.     public function __construct(Connection $connection)
  21.     {
  22.         $this->connection $connection;
  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->fetchAll('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->executeUpdate(
  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->executeUpdate(
  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.     private function setInsertSnippets(EntityWriteResult $writeResult, array $snippetSets, array &$snippets): void
  71.     {
  72.         $name $writeResult->getPayload()['name'];
  73.         $labels $writeResult->getPayload()['config']['label'];
  74.         foreach ($snippetSets as $snippetSet) {
  75.             $label $name;
  76.             $iso $snippetSet['iso'];
  77.             if (isset($labels[$iso])) {
  78.                 $label $labels[$iso];
  79.             }
  80.             $snippets[] = [
  81.                 'id' => Uuid::randomBytes(),
  82.                 'setId' => $snippetSet['id'],
  83.                 'translationKey' => 'customFields.' $name,
  84.                 'value' => $label,
  85.                 'author' => 'System',
  86.                 'customFields' => json_encode([
  87.                     self::CUSTOM_FIELD_ID_FIELD => $writeResult->getPrimaryKey(),
  88.                 ]),
  89.                 'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  90.             ];
  91.         }
  92.     }
  93. }