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

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\EntityWrittenEvent;
  7. use Shopware\Core\Framework\Uuid\Uuid;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. class CustomFieldSubscriber implements EventSubscriberInterface
  10. {
  11.     /**
  12.      * @var Connection
  13.      */
  14.     private $connection;
  15.     public function __construct(Connection $connection)
  16.     {
  17.         $this->connection $connection;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             'custom_field.written' => 'customFieldIsWritten',
  23.         ];
  24.     }
  25.     public function customFieldIsWritten(EntityWrittenEvent $event): void
  26.     {
  27.         $snippets = [];
  28.         $snippetSets null;
  29.         foreach ($event->getWriteResults() as $writeResult) {
  30.             if (!isset($writeResult->getPayload()['config']['label']) || empty($writeResult->getPayload()['config']['label'])) {
  31.                 continue;
  32.             }
  33.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_INSERT) {
  34.                 if ($snippetSets === null) {
  35.                     $snippetSets $this->connection->fetchAll('SELECT id, iso FROM snippet_set');
  36.                 }
  37.                 if (empty($snippetSets)) {
  38.                     return;
  39.                 }
  40.                 $this->setInsertSnippets($writeResult$snippetSets$snippets);
  41.             }
  42.         }
  43.         if (empty($snippets)) {
  44.             return;
  45.         }
  46.         foreach ($snippets as $snippet) {
  47.             $this->connection->executeUpdate(
  48.                 'INSERT INTO snippet (`id`, `snippet_set_id`, `translation_key`, `value`, `author`, `created_at`) VALUES(:id, :setId, :translationKey, :value, :author, :createdAt)',
  49.                 $snippet
  50.             );
  51.         }
  52.     }
  53.     private function setInsertSnippets(EntityWriteResult $writeResult, array $snippetSets, array &$snippets): void
  54.     {
  55.         $name $writeResult->getPayload()['name'];
  56.         $labels $writeResult->getPayload()['config']['label'];
  57.         foreach ($snippetSets as $snippetSet) {
  58.             $label $name;
  59.             $iso $snippetSet['iso'];
  60.             if (isset($labels[$iso])) {
  61.                 $label $labels[$iso];
  62.             }
  63.             $snippets[] = [
  64.                 'id' => Uuid::randomBytes(),
  65.                 'setId' => $snippetSet['id'],
  66.                 'translationKey' => 'customFields.' $name,
  67.                 'value' => $label,
  68.                 'author' => 'System',
  69.                 'createdAt' => (new \DateTime())->format(Defaults::STORAGE_DATE_TIME_FORMAT),
  70.             ];
  71.         }
  72.     }
  73. }