Reputation: 1482
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.
Upvotes: 3
Views: 5342
Reputation: 1
Yes, the example is incomplete, although it can work just like that once you need to configure the provider from the yaml file, as mentioned in the official documentation, eg.( allowed_extensions: xyz, ijk,) or any of the advanced configuration as I recall they call it there, you get to a point which documentation does not mention, That is the part where you have to connect your provider with the bundle configuration defaults.
This can bee seen in the difference between the full service names and the short ones used to configure the default providers, I have not looked into that so much, but am sure that can be configured from the class itself, although I would really like to keep the configurations in one place.
Do the devs of sonata accept any newcomers to the team? I know it has been quite a long time, and the sonata project has grown, but there are many things I think could be improved
Upvotes: 0
Reputation: 762
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
Upvotes: 0
Reputation: 1482
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.
Upvotes: 7