/** * Buildform function. * * @param FormBuilderInterface $builder the formBuilder * @param array $options the options for this form */ public function buildForm(FormBuilderInterface $builder, array $options) { $entityAlias = $this->entityConfiguration['alias']; $entityIdGetter = $this->entityConfiguration['idGetter']; $fieldOptions = $this->formConfig->getOptions(); $fieldType = $this->formConfig->getType()->getName(); foreach ($this->locales as $locale) { $translatedFieldName = $locale . '_' . $this->fieldName; $entityId = $this->entity->{$entityIdGetter}(); $translationData = $entityId ? $this->entityTranslationProvider->getTranslation($entityAlias, $entityId, $this->fieldName, $locale) : ''; $builder->add($translatedFieldName, $fieldType, ['required' => isset($fieldOptions['required']) ? $this->evaluateRequired($fieldOptions['required'], $locale) : false, 'mapped' => false, 'label' => $fieldOptions['label'], 'data' => $translationData, 'constraints' => $fieldOptions['constraints']]); } }
/** * Method executed at the end of the response. Save all entity translations * previously generated and waiting for being flushed into database and * cache layer */ public function saveEntityTranslations() { if (empty($this->translationsBackup)) { return null; } foreach ($this->translationsBackup as $formHash => $entities) { foreach ($entities as $entityData) { $entity = $entityData['object']; $entityIdGetter = $entityData['idGetter']; $entityAlias = $entityData['alias']; $fields = $entityData['fields']; foreach ($fields as $fieldName => $locales) { foreach ($locales as $locale => $translation) { $this->entityTranslationProvider->setTranslation($entityAlias, $entity->{$entityIdGetter}(), $fieldName, $translation, $locale); } } } } $this->entityTranslationProvider->flushTranslations(); }
/** * Saves object translations * * $translations = array( * 'es' => array( * 'name' => 'Nombre del producto', * 'description' => 'Descripción del producto', * ), * 'fr' => array( * 'name' => 'Nom du produit', * 'description' => 'Description du produit', * ), * ); * * @param Object $object Object * @param array $translations Translations * * @return $this Self object */ public function save($object, array $translations) { $classStack = $this->getNamespacesFromClass(get_class($object)); foreach ($classStack as $classNamespace) { if (!array_key_exists($classNamespace, $this->configuration)) { continue; } $configuration = $this->configuration[$classNamespace]; $idGetter = $configuration['idGetter']; $entityId = $object->{$idGetter}(); foreach ($translations as $locale => $translation) { foreach ($configuration['fields'] as $fieldName => $fieldConfiguration) { if (isset($translation[$fieldName])) { $this->entityTranslationProvider->setTranslation($configuration['alias'], $entityId, $fieldName, $translation[$fieldName], $locale); } } } } $this->entityTranslationProvider->flushTranslations(); return $this; }
/** * Flush all previously set translations. * * @return $this Self object */ public function flushTranslations() { return $this->entityTranslatorProvider->flushTranslations(); }