3

There is an example in the official documentation about how to write custom provider, but it doesn't work.

My question is: what is the best way to write custom provider, especially how to write and register provider as a new service?

When I try to use this code from documentation, I get errors about type of arguments. What does mean empty argument?

Thank you.

borN_free
  • 1,385
  • 11
  • 19

2 Answers2

7

After some investigation, the following code works:

Register provider as a service:

// src/Application/Sonata/MediaBundle/Resources/config/services.yml
parameters:
    application_sonata_media.custom_class: Application\Sonata\MediaBundle\Provider\CustomProvider

services:
    sonata.media.provider.custom:
          class: %application_sonata_media.custom_class%
          tags:
              - { name: sonata.media.provider }
          arguments:
              - sonata.media.provider.custom
              - @sonata.media.filesystem.local
              - @sonata.media.cdn.server
              - @sonata.media.generator.default
              - @sonata.media.thumbnail.format

Custom Provider code:

// src/Application/Sonata/MediaBundle/Provider/CustomProvider.php
<?php

namespace Application\Sonata\MediaBundle\Provider;

use Sonata\MediaBundle\Model\MediaInterface;
use Sonata\MediaBundle\Provider\FileProvider;
use Symfony\Component\Form\FormBuilder;
use Symfony\Component\HttpFoundation\File\File;

/**
 * Class CustomProvider
 * @package Application\Sonata\MediaBundle\Provider
 */
class CustomProvider extends FileProvider
{
    /**
     * @param MediaInterface $media
     */
    protected function doTransform(MediaInterface $media)
    {
         // ...
    }

    /**
     * {@inheritdoc}
     */
    public function generatePublicUrl(MediaInterface $media, $format)
    {
        // new logic
    }

    /**
     * {@inheritdoc}
     */
    public function postPersist(MediaInterface $media)
    {
    }

    /**
     * {@inheritdoc}
     */
    public function postUpdate(MediaInterface $media)
    {
    }
}

Updated sonata configuration:

// app/config/sonata/sonata_media.yml
sonata_media:
        ...
        product:
            providers:
                - sonata.media.provider.image
                - sonata.media.provider.custom
            formats:
                small: { width: 40 , quality: 100}
        ...

And I've also setup DI extension to autoload services.yml

I made a PR to update outdated documentation.

borN_free
  • 1,385
  • 11
  • 19
  • Hi @borN_free, I'm trying this snippet, but when I try to upload a file, a get, **"This value should not be null. This value should not be blank."** 3 times., all your code, adding `- ['pdf'] - ['application/pdf', 'application/x-pdf']` at end of arguments. any idea to solve it? maybe the example code is incomplete? thanks – jjgarcía Feb 22 '16 at 02:01
0

I couldn’t get this to work until i named the service exactly as the one i was overriding (sonata.media.provider.image)

See https://stackoverflow.com/a/20118256/4239642

Community
  • 1
  • 1
Adrian C.
  • 742
  • 6
  • 14