-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #137 from eiannone/file-form-type
Adding new 'cmf_media_file' form type
- Loading branch information
Showing
12 changed files
with
346 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony CMF package. | ||
* | ||
* (c) 2011-2014 Symfony CMF | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Cmf\Bundle\MediaBundle\Form\Type; | ||
|
||
use Symfony\Cmf\Bundle\MediaBundle\File\UploadFileHelperInterface; | ||
use Symfony\Cmf\Bundle\MediaBundle\Form\DataTransformer\ModelToFileTransformer; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | ||
|
||
/** | ||
* Form type which transforms an uploaded file to an object implementing the | ||
* Symfony\Cmf\Bundle\MediaBundle\FileInterface | ||
* | ||
* It renders as a file upload button with a link for downloading the existing | ||
* file, if any. | ||
* | ||
* Usage: you need to supply the object class to which the file will be | ||
* transformed (which should implement FileInterface) and an UploadFileHelper, | ||
* which will handle the UploadedFile and create the transformed object. | ||
*/ | ||
class FileType extends AbstractType | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $dataClass; | ||
|
||
/** | ||
* @var UploadFileHelperInterface | ||
*/ | ||
protected $uploadFileHelper; | ||
|
||
/** | ||
* @param string $class | ||
* @param UploadFileHelperInterface $uploadFileHelper | ||
*/ | ||
public function __construct($class, UploadFileHelperInterface $uploadFileHelper) | ||
{ | ||
$this->dataClass = $class; | ||
$this->uploadFileHelper = $uploadFileHelper; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getParent() | ||
{ | ||
return 'file'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getName() | ||
{ | ||
return 'cmf_media_file'; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function buildForm(FormBuilderInterface $builder, array $options) | ||
{ | ||
$transformer = new ModelToFileTransformer($this->uploadFileHelper, $options['data_class']); | ||
$builder->addModelTransformer($transformer); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function setDefaultOptions(OptionsResolverInterface $options) | ||
{ | ||
$this->configureOptions($options); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function configureOptions(OptionsResolver $options) | ||
{ | ||
$options->setDefaults(array('data_class' => $this->dataClass)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.