6. Usage

6.1. Saving a media file

Saving a media file required at least 3 pieces of information:

  • the context: which is used as a main category user picture, news or any

  • the provider name: the provider code which handles the media processing while saving

  • the binary content: the provider information source: the unique id for a video, a File instance, etc …

For instance, a Youtube video can be created and saved like this:

$media = new Media();
$media->setContext('user'); // video related to the user

/** @var Sonata\MediaBundle\Model\MediaManagerInterface $mediaManager */

or this:

$media = new Media();

$mediaManager->save($media, 'user', 'sonata.media.provider.youtube');

6.2. Retrieving metadata information

The providers has a dedicated field where extra information can be saved: the providerMetadata field.

You can retrieve a value very easily with the getMetadataValue method.

For instance you can retrieve the original Youtube video title with:

$media = $mediaManager->find(132);

echo $media->getMetadataValue('title', 'if none use this string');