Documentation » Media

5. Usages »

« 3. Creating a Media Provider: A Vimeo Provider

4. Media Context

When a site has to handle pictures, you can have different type of pictures: news pictures, users pictures etc. But in the end pictures require the same features: resize, cdn and database relationship with entities.

The MediaBundle tries to solve this situation by introducing context: a context has its own set of media providers and its own set of formats. That means you can have a small user picture format and a small news picture format with different sizes and providers. For example:

    default:  # the default context is mandatory

            small: { width: 100 , quality: 70}
            big:   { width: 500 , quality: 70}


            small: { width: 150 , quality: 95}
            big:   { width: 500 , quality: 90}

4.1. AdminBundle Integration

When you create a new blog post, you might want to link an image to that post.

Doctrine ORM:


Doctrine PHPCR:


In the PostAdmin, you can add a new field image with a link_parameters option. This option will add an extra parameter into the add link. This parameter will be used by the related controller.

public function configureFormFields(FormMapper $form)
    // ...
    $form->add('image', 'sonata_type_model_list', array(), array('link_parameters' => array('context' => 'news')));
    // ...

If you look in the MediaAdmin class, the class defined a getPersistentParameters method. This method allows you to define persistent parameters across the MediaAdminController. Depending on the action, the parameter can change the Admin behaviors:

  • list: filters the list to display only one context
  • create: creates a new media asset with the provided context
public function getPersistentParameters()
    if (!$this->getRequest()) {
        return array();

    return array(
        'provider' => $this->getRequest()->get('provider'),
        'context'  => $this->getRequest()->get('context'),

Found a typo? Something is wrong in this documentation? Just fork and edit it!