Skip to content

Instantly share code, notes, and snippets.

@rande
Created February 24, 2012 01:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save rande/1896425 to your computer and use it in GitHub Desktop.
Save rande/1896425 to your computer and use it in GitHub Desktop.
SonataMediaBundle : sonata_media_type Form Type
<?php
namespace Sonata\Bundle\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sonata\Bundle\DemoBundle\Model\MediaPreview;
use Symfony\Component\HttpFoundation\Request;
class DemoController extends Controller
{
public function mediaAction(Request $request)
{
// preset a default value
$media = $this->get('sonata.media.manager.media')->create();
$media->setBinaryContent('http://www.youtube.com/watch?v=qTVfFmENgPU');
// create the target object
$mediaPreview = new MediaPreview();
$mediaPreview->setMedia($media);
// create the form
$builder = $this->createFormBuilder($mediaPreview);
$builder->add('media', 'sonata_media_type', array(
'provider' => 'sonata.media.provider.youtube',
'context' => 'default'
));
$form = $builder->getForm();
// bind and transform the media's binary content into real content
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
$this->getSeoPage()
->setTitle($media->getName())
->addMeta('property', 'og:description', $media->getDescription())
->addMeta('property', 'og:type', 'video')
;
}
return $this->render('SonataDemoBundle:Demo:media.html.twig', array(
'form' => $form->createView(),
'media' => $mediaPreview->getMedia()
));
}
/**
* @return \Sonata\SeoBundle\Seo\SeoPageInterface
*/
public function getSeoPage()
{
return $this->get('sonata.seo.page');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment