public function preBind(FormEvent $event) { $form = $event->getForm(); $propertyName = $form->getName(); $obj = $form->getParent()->getData(); //For oneToMany at SonataAdmin if (!$obj) { return; } $mapping = $this->mappingFactory->getMappingFromField($obj, $this->dataStorage->getReflectionClass($obj), $propertyName); if ($mapping) { /* $form->add('file', 'file', array('required' => false)) ->add('delete', 'checkbox', array('required' => false));*/ $this->transformer->setMapping($mapping, $mapping->getFileUploadPropertyName() == $propertyName ? FileDataTransformer::MODE_UPLOAD_FIELD : FileDataTransformer::MODE_FILEDATA_FIELD); } }
/** */ public function testGetMappingFromField() { $obj = new DummyEntity(); $class = new \ReflectionClass($obj); $uploadable = Mocks::getUploadableMock($this); $fileField = Mocks::getUploadableFieldMock($this); $mappingsConfig = array('dummy_file' => array('upload_dir' => '/www/web/images', 'upload_path' => '/images')); $fileField->expects($this->any())->method('getMapping')->will($this->returnValue('dummy_file')); $fileField->expects($this->once())->method('getFileUploadPropertyName')->will($this->returnValue('file')); $fileField->expects($this->once())->method('getFileDataPropertyName')->will($this->returnValue('file')); $this->driver->expects($this->once())->method('readUploadable')->with($class)->will($this->returnValue($uploadable)); $this->driver->expects($this->once())->method('readUploadableField')->with($class)->will($this->returnValue($fileField)); $factory = new PropertyMappingFactory($this->namerServiceInvoker, $this->driver, $mappingsConfig); $mapping = $factory->getMappingFromField($obj, $class, 'file'); $this->assertSame($mapping->getObj(), $obj); $this->assertEquals('dummy_file', $mapping->getMappingName()); $this->assertEquals('/www/web/images', $mapping->getUploadDir()); $this->assertEquals('/images', $mapping->getUploadPath()); $this->assertTrue($mapping->getDeleteOnRemove()); $this->assertFalse($mapping->hasNamer()); $this->assertFalse($mapping->hasDirectoryNamer()); $this->assertFalse($mapping->isStoreFullDir()); $this->assertFalse($mapping->isOverwriteDuplicates()); }