public function getLocalePath(Locale $locale, Request $request) { $arrPath = explode('/', ltrim($request->getPathInfo(), '/')); if (in_array($arrPath[0], $this->manager->getAllAliases())) { unset($arrPath[0]); } $resultUrl = '/' . implode('/', $arrPath); if (!$this->manager->getIgnoreDefaultLocaleFlag() || $locale->getAlias() != $this->manager->getDefaultLocaleAlias()) { $resultUrl = '/' . $locale->getAlias() . $resultUrl; } return $resultUrl; }
public function postSubmit(FormEvent $event) { $object = $event->getData(); /** @var TranslationRepository $repository */ $repository = $this->em->getRepository('Gedmo\\Translatable\\Entity\\Translation'); $translations = $repository->findTranslations($object); $fileMappingsFields = array_keys($this->getFileMappings($object)); foreach ($this->newData as $locale => $localeData) { foreach ($localeData as $field => $value) { if (in_array($field, $fileMappingsFields)) { $value = $this->processFileUpload($value, $field, $locale, $object); if ($this->localeManager->getDefaultLocaleAlias() == $locale && is_array($value)) { $object->{'set' . ucfirst($field)}($value); } if (empty($value['fileName'])) { $value = false; } } if ($value !== false) { $repository->translate($object, $field, $locale, $value); } else { if (array_key_exists($locale, $translations) && array_key_exists($field, $translations[$locale])) { $repository->createQueryBuilder('t')->where('t.field = :field')->andWhere('t.locale = :locale')->andWhere('t.objectClass = :class')->andWhere('t.foreignKey = :foreignKey')->setParameters(['class' => get_class($object), 'field' => $field, 'locale' => $locale, 'foreignKey' => $object->getId()])->delete()->getQuery()->execute(); } } } } }
/** * Закончить задание содержания мультиязычной * вкладки и передать результат в мапер формы * @desc Метод повторяет поведение класса FormMapper * @return FormMapper */ public function end() { $eventSubscriber = new AddTranslationFieldSubscriber($this->em, $this->localeManager, $this->mappingFactory, $this->fileStorage); $this->formMapper->getFormBuilder()->addEventSubscriber($eventSubscriber); $firstTab = true; $locales = $this->localeManager->getActiveList(); $administrator = $this->getTokenStorage()->getToken()->getUser(); if ($administrator instanceof Administrator) { usort($locales, [$administrator, 'sortLocales']); } foreach ($locales as $localeData) { $tabName = $localeData->getName(); if (strlen($this->tabName) > 0) { $tabName = str_replace('%locale_name%', $localeData->getName(), $this->tabName); } $tabOptions = []; if ($this->isDisabledByAdministratorLocales($localeData->getAlias())) { $tabOptions = ['class' => 'disabled']; } $this->formMapper->with($tabName, $tabOptions); foreach ($this->fields as $fieldData) { if (!$firstTab && (!isset($fieldData['options']['translatable']) || !$fieldData['options']['translatable'])) { continue; } $fieldName = $fieldData['name']; if (isset($fieldData['options']['translatable'])) { if ($fieldData['options']['translatable']) { $fieldName = 'translation___' . $fieldData['name'] . '___' . $localeData->getAlias(); $fieldData['options']['mapped'] = false; if (isset($fieldData['options']['locale'])) { $fieldData['options']['locale'] = $localeData->getAlias(); } $fieldData['options']['disabled'] = $this->isDisabled($localeData->getAlias(), $fieldData['options']); if ($localeData->getAlias() != $this->localeManager->getDefaultLocaleAlias()) { //убираем обязательность полей для языков $fieldData['options']['required'] = false; if (!empty($fieldData['options']['inner_constraints'])) { foreach ($fieldData['options']['inner_constraints'] as $key => $value) { if ($value instanceof FileNotBlank) { unset($fieldData['options']['inner_constraints'][$key]); } } } } } unset($fieldData['options']['translatable']); } $this->formMapper->add($fieldName, $fieldData['type'], $fieldData['options'], $fieldData['fieldDescriptionOptions']); } $this->formMapper->end(); $firstTab = false; } return $this->formMapper; }