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

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.     public function __construct(Connection $connection)
  18.     {
  19.         $this->connection $connection;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             'custom_field.written' => 'customFieldIsWritten',
  25.             'custom_field.deleted' => 'customFieldIsDeleted',
  26.         ];
  27.     }
  28.     public function customFieldIsWritten(EntityWrittenEvent $event): void
  29.     {
  30.         $snippets = [];
  31.         $snippetSets null;
  32.         foreach ($event->getWriteResults() as $writeResult) {
  33.             if (!isset($writeResult->getPayload()['config']['label']) || empty($writeResult->getPayload()['config']['label'])) {
  34.                 continue;
  35.             }
  36.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT) {
  37.                 if ($snippetSets === null) {
  38.                     $snippetSets $this->connection->fetchAll('SELECT id, iso FROM snippet_set');
  39.                 }
  40.                 if (empty($snippetSets)) {
  41.                     return;
  42.                 }
  43.                 $this->setInsertSnippets($writeResult$snippetSets$snippets);
  44.             }
  45.         }
  46.         if (empty($snippets)) {
  47.             return;
  48.         }
  49.         foreach ($snippets as $snippet) {
  50.             $this->connection->executeUpdate(
  51.                 'INSERT INTO snippet (`id`, `snippet_set_id`, `translation_key`, `value`, `author`, `custom_fields`, `created_at`)
  52.                       VALUES (:id, :setId, :translationKey, :value, :author, :customFields, :createdAt)
  53.                       ON DUPLICATE KEY UPDATE `value` = :value',
  54.                 $snippet
  55.             );
  56.         }
  57.     }
  58.     public function customFieldIsDeleted(EntityDeletedEvent $event): void
  59.     {
  60.         $this->connection->executeUpdate(
  61.             'DELETE FROM `snippet`
  62.             WHERE JSON_EXTRACT(`custom_fields`, "$.custom_field_id") IN (:customFieldIds)',
  63.             ['customFieldIds' => $event->getIds()],
  64.             ['customFieldIds' => Connection::PARAM_STR_ARRAY]
  65.         );
  66.     }
  67.     private function setInsertSnippets(EntityWriteResult $writeResult, array $snippetSets, array &$snippets): void
  68.     {
  69.         $name $writeResult->getPayload()['name'];
  70.         $labels $writeResult->getPayload()['config']['label'];
  71.         foreach ($snippetSets as $snippetSet) {
  72.             $label $name;
  73.             $iso $snippetSet['iso'];
  74.             if (isset($labels[$iso])) {
  75.                 $label $labels[$iso];
  76.             }
  77.             $snippets[] = [
  78.                 'id' => Uuid::randomBytes(),
  79.                 'setId' => $snippetSet['id'],
  80.                 'translationKey' => 'customFields.' $name,
  81.                 'value' => $label,
  82.                 'author' => 'System',
  83.                 'customFields' => json_encode([
  84.                     self::CUSTOM_FIELD_ID_FIELD => $writeResult->getPrimaryKey(),
  85.                 ]),
  86.                 'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  87.             ];
  88.         }
  89.     }
  90. }