Skip to content

Instantly share code, notes, and snippets.

@iampersistent
Created August 28, 2011 02:00
Show Gist options
  • Save iampersistent/1176142 to your computer and use it in GitHub Desktop.
Save iampersistent/1176142 to your computer and use it in GitHub Desktop.
using SonataMediaBundle w/o SonataAdminBundle
// in controller ...
$mgr = $this->get('sonata.media.manager.media');
$pool = $this->get('sonata.media.pool');
$class = $mgr->getClass();
$medium = new $class();
$medium->setProviderName('sonata.media.provider.image');
$form = $this->createForm(new MediaFormType(), $medium);
if ($this->getRequest()->getMethod() === 'POST') {
$form->bindRequest($this->getRequest());
if ($form->isValid()) {
$pool->prePersist($medium);
$mgr->update($medium);
$pool->postPersist($medium); // includes generate thumbnails
$this->redirect($this->generateUrl('picture'));
}
}
// The bundle needs to be updated to use the current forms in SF2, for now, build your own.
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class MediaFormType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('binaryContent', 'file');
}
public function getName()
{
return 'sonata_media';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment