function it_does_nothing_if_form_has_no_translatable_parent(FormView $view, FormInterface $form, FormConfigInterface $formConfig, FormInterface $parentForm, FormConfigInterface $parentFormConfig) { $form->getPropertyPath()->willReturn('translatable_property'); $form->getConfig()->willReturn($formConfig); $form->getParent()->willReturn($parentForm); $parentForm->getConfig()->willReturn($parentFormConfig); $parentFormConfig->getInheritData()->willReturn(true); $parentForm->getParent()->willReturn(null); $this->finishView($view, $form, array()); }
/** * @param FormInterface $form * @return bool */ public function isFormPropertyPathTranslatable(FormInterface $form) { $propertyPath = $form->getPropertyPath(); if (!$propertyPath) { return false; } $parent = $this->getFirstTranslatableParent($form); if (!$parent) { return false; } if (!$this->hasFormTranslatableProperty($parent, $propertyPath)) { return false; } return true; }
function it_does_nothing_if_forms_property_is_not_translatable_in_first_translatable_parent(ManagerRegistry $managerRegistry, ObjectManager $manager, TranslatableListener $translatableListener, ClassMetadata $translatableMetadata, PropertyPath $propertyPath, FormView $view, FormInterface $form, FormConfigInterface $formConfig, FormInterface $parentForm, FormConfigInterface $parentFormConfig, FormInterface $grandParentForm, FormConfigInterface $grandParentFormConfig) { $propertyPath->__toString()->willReturn('translatable_property'); $form->getPropertyPath()->willReturn($propertyPath); $form->getConfig()->willReturn($formConfig); $form->getParent()->willReturn($parentForm); $parentForm->getConfig()->willReturn($parentFormConfig); $parentFormConfig->getInheritData()->willReturn(true); $parentForm->getParent()->willReturn($grandParentForm); $grandParentForm->getConfig()->willReturn($grandParentFormConfig); $grandParentFormConfig->getInheritData()->willReturn(false); $grandParentFormConfig->getDataClass()->willReturn('translatable_class'); $managerRegistry->getManagerForClass('translatable_class')->willReturn($manager); $translatableListener->getExtendedMetadata($manager, 'translatable_class')->willReturn($translatableMetadata); $translatableMetadata->hasTranslatableProperties()->willReturn(true); $translatableMetadata->getTranslatableProperties()->willReturn(array()); $this->finishView($view, $form, array()); expect($view->vars['translatable'])->toBe(false); expect($view->vars['not_translated'])->toBe(false); }
/** * Pass the image URL to the view * * @param FormView $view * @param FormInterface $form * @param array $options */ public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['translation'] = isset($options['translation']) && $options['translation'] === true; $view->vars['currentLocale'] = $this->defaultLocale; if ($view->vars['translation']) { $parent = $form->getParent(); if ($parent instanceof Form) { $property = new Property($form->getPropertyPath()); $entity = $parent->getConfig()->getDataClass(); if (is_object($parent->getData())) { $entity = $parent->getData(); } $translations = $this->translator->getTranslations($entity, $property); if ($translations === null) { $view->vars['translation'] = false; return; } $view->vars['translations'] = $translations; } } }
function let(ManagerRegistry $managerRegistry, TranslatableListener $translatableListener, PropertyAccessor $propertyAccessor, ObjectManager $manager, ClassMetadata $translatableMetadata, PropertyPath $propertyPath, FormView $view, FormInterface $form, FormConfigInterface $formConfig, FormInterface $parentForm, FormConfigInterface $parentFormConfig, FormInterface $grandParentForm, FormConfigInterface $grandParentFormConfig) { $this->beConstructedWith($managerRegistry, $translatableListener, $propertyAccessor); $propertyPath->__toString()->willReturn('translatable_property'); $form->getPropertyPath()->willReturn($propertyPath); $form->getConfig()->willReturn($formConfig); $form->getParent()->willReturn($parentForm); $parentForm->getConfig()->willReturn($parentFormConfig); $parentFormConfig->getInheritData()->willReturn(true); $parentForm->getParent()->willReturn($grandParentForm); $data = new \stdClass(); $grandParentForm->getNormData()->willReturn($data); $propertyAccessor->getValue($data, 'locale')->willReturn('en'); $grandParentForm->getConfig()->willReturn($grandParentFormConfig); $grandParentFormConfig->getInheritData()->willReturn(false); $grandParentFormConfig->getDataClass()->willReturn('translatable_class'); $managerRegistry->getManagerForClass('translatable_class')->willReturn($manager); $translatableListener->getExtendedMetadata($manager, 'translatable_class')->willReturn($translatableMetadata); $translatableListener->getLocale()->willReturn('de'); $translatableMetadata->localeProperty = 'locale'; $translatableMetadata->hasTranslatableProperties()->willReturn(true); }
/** * @param mixed $targetEntity * @param FormInterface $form * @return bool */ protected function isDataEmptyValue($targetEntity, FormInterface $form) { $formConfig = $form->getConfig(); // Check to see if there's a value provided by the form. $accessor = PropertyAccess::createPropertyAccessor(); try { $value = $accessor->getValue($targetEntity, $form->getPropertyPath()); if ($formConfig->getOption('multiple')) { $result = $value instanceof Collection && $value->isEmpty(); } else { $result = null === $value; } } catch (NoSuchPropertyException $exception) { // If value cannot be get then treat it as value as empty and we need to suppress this exception. $result = false; } return $result; }
/** * Gets the form root data class used by the given form. * * @param FormInterface $form * @return string|null */ private function resolveDataClass(FormInterface $form) { // Nothing to do if root if ($form->isRoot()) { return $form->getConfig()->getDataClass(); } $propertyPath = $form->getPropertyPath(); /** @var FormInterface $dataForm */ $dataForm = $form; // If we have a index then we need to use it's parent if ($propertyPath->getLength() === 1 && $propertyPath->isIndex(0) && $form->getConfig()->getCompound()) { return $this->resolveDataClass($form->getParent()); } // Now locate the closest data class // TODO what is the length really for? for ($i = $propertyPath->getLength(); $i != 0; $i--) { $dataForm = $dataForm->getParent(); # When a data class is found then use that form # This happend when property_path contains multiple parts aka `entity.prop` if ($dataForm->getConfig()->getDataClass() !== null) { break; } } // If the root inherits data, then grab the parent if ($dataForm->getConfig()->getInheritData()) { $dataForm = $dataForm->getParent(); } return $dataForm->getConfig()->getDataClass(); }