Beispiel #1
0
 /**
  * @param FormEvent $event
  */
 public function postSubmit(FormEvent $event)
 {
     $form = $event->getForm();
     $configModel = $form->getConfig()->getOption('config_model');
     $data = $event->getData();
     $labelsToBeUpdated = [];
     foreach ($this->configManager->getProviders() as $provider) {
         $scope = $provider->getScope();
         if (isset($data[$scope])) {
             $configId = $this->configManager->getConfigIdByModel($configModel, $scope);
             $config = $provider->getConfigById($configId);
             $translatable = $provider->getPropertyConfig()->getTranslatableValues($configId);
             foreach ($data[$scope] as $code => $value) {
                 if (in_array($code, $translatable, true)) {
                     // check if a label text was changed
                     $labelKey = $config->get($code);
                     if (!$configModel->getId()) {
                         $labelsToBeUpdated[$labelKey] = $value;
                     } elseif ($value != $this->translator->trans($labelKey)) {
                         $labelsToBeUpdated[$labelKey] = $value;
                     }
                     // replace label text with label name in $value variable
                     $value = $config->get($code);
                 }
                 $config->set($code, $value);
             }
             $this->configManager->persist($config);
         }
     }
     if ($form->isValid()) {
         // update changed labels if any
         if (!empty($labelsToBeUpdated)) {
             /** @var EntityManager $translationEm */
             $translationEm = $this->doctrine->getManagerForClass(Translation::ENTITY_NAME);
             /** @var TranslationRepository $translationRepo */
             $translationRepo = $translationEm->getRepository(Translation::ENTITY_NAME);
             $values = [];
             $locale = $this->translator->getLocale();
             foreach ($labelsToBeUpdated as $labelKey => $labelText) {
                 // save into translation table
                 $values[] = $translationRepo->saveValue($labelKey, $labelText, $locale, TranslationRepository::DEFAULT_DOMAIN, Translation::SCOPE_UI);
             }
             // mark translation cache dirty
             $this->dbTranslationMetadataCache->updateTimestamp($locale);
             $translationEm->flush($values);
         }
         $this->configManager->flush();
     }
 }