MediaBundle ~ MediaType improved

The SonataMediaBundle manages any kind of media: from images to youtube videos. It handles the lifecycle of processing a media: validation, storage and publication. On top of these features, the MediaBundle generates thumbnails and provides security strategies to safely distribute a media.

As handling media is not always easy, the media bundle provides a set of tools to help your development process (batch insert, async thumbnail queue ...). Today, I want to focus on the MediaType form type. This type has been re-work to have a better integration with the SonataAdminBundle and so with your admin classes and your entities.

Let's see how to use the MediaType ...

Add a media property to your entity

/**
 * @ORM\Entity
 * @ORM\Table(name="test_car_engine")
 */
class Engine
{

    /**
     * @var \Application\Sonata\MediaBundle\Entity\Media
     * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
     */
    protected $media;

    /**
     * @param MediaInterface $media
     */
    public function setMedia(MediaInterface $media)
    {
        $this->media = $media;
    }

    /**
     * @return MediaInterface
     */
    public function getMedia()
    {
        return $this->media;
    }
}

Don't forget to run Doctrine's migration on your database.

Add the new field into the EngineAdmin

class EngineAdmin extends Admin
{
    /**
     * {@inheritdoc}
     */
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            // ... others fields here
            ->add('media', 'sonata_media_type', array(
                 'provider' => 'sonata.media.provider.image',
                 'context'  => 'engine'
            ));
        ;
    }
}

As we can see the sonata_media_type (the instance code of the MediaType) accept 2 mandatory options: a provider and a context. These values must exist in the sonata_media configuration.

Configure the MediaBundle

Let's add the engine context. For information a context is just a way of grouping a same set of media, inside a context you can have different formats for your media.

sonata_media:
    db_driver:       doctrine_orm # | doctrine_mongodb
    default_context: default
    contexts:
        # other contexts here
        engine:
            providers:
                - sonata.media.provider.image

            formats:
                preview:   { width: 100, quality: 100}
                small:     { width: 200, quality: 100}
                large:     { width: 600, quality: 100}

Now you can go to your admin backend and enter a media to your entity.

Result

MediaType

The unlink option allows to remove the association between the media instance and your entity.

Usage

You can display the media by using the built-in twig helpers:

{% if enfine.media %}
   {% thumbnail engine.media, 'small' %} {# render the related thumbnail #}
   {% media engine.media, 'small' %} {# render the media #}
{% endif %}

Reference

Comments

  • No comments available
The comment form is closed for this current news.