Sascha
Sascha

Reputation: 31

sonata media bundle modify MediaType

I use symfony 3.4 with the sonata media bundle. Using the MediaType in a form works fine like this:

$this->builder->->add('document', MediaType::class,
                      ['label'    => 'Document',
                       'provider' => 'sonata.media.provider.file',
                       'context'  => 'default']) ;

Now I'd like to modify the fields that the MediaType generates.

I just copied the Twig Template \vendor\sonata-project\media-bundle\src\Resources\views\Form\media_widgets.html.twig to my project (\AppBundle\SonataMediaBundle\views\Form) an now I am able to modify the layout. So long, everything works very fine.

The Template finally uses {{ block('form_widget') }} to write out the Upload Button and a Checkbox to delete uploaded files including the labels. This is done in the \sonata-project\media-bundle\src\Forms\Type\MediaType.php class in the buildForm method.

Now I want to modify this buildForm method. Modifying classes in vendor packages isn't a good practise, so I wanted to create a new class the extends the MediaType class an overwrites the buildForm method, like this:

namespace AppBundle\Form\SonataMediaBundle ;

class MyMediaType extends \Sonata\MediaBundle\Form\Type\MediaType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        ... copy code from original MediaType class and modify it after my needs ...
        ... e.g. just remove the unlink checkbox ...
    }
}

I receive an error message Cannot autowire service "AppBundle\Form\SonataMediaBundle\EwMediaType": argument "$pool" of method "Sonata\MediaBundle\Form\Type\MediaType::__construct()" references class "Sonata\MediaBundle\Provider\Pool" but no such service exists. You should maybe alias this class to the existing "sonata.media.pool" service.

I assume I have to do some configuration stuff before I can extend the MediaType in my own project. I tried with following code in my services.yml

Sonata\MediaBundle\Provider\Pool: 
   tags: ['sonata.media.pool']

But now another error occurs: Cannot autowire service "AppBundle\Form\SonataMediaBundle\EwMediaType": argument "$class" of method "Sonata\MediaBundle\Form\Type\MediaType::__construct()" has no type-hint, you should configure its value explicitly.

I am quite new to symfony and not very familiar with its configuration. Can anyone please help me an list the steps I need to do, so I can extend and modify the MediaType class?

Thanks a lot.

Sascha

Upvotes: 2

Views: 1440

Answers (1)

Layke
Layke

Reputation: 53146

You can do the following. You need to create a custom service definition.

AppBundle\Form\Type\CustomMediaType:
    class: AppBundle\Form\Type\CustomMediaType
    autowire: true
    autoconfigure: true
    arguments:
    - "@sonata.media.pool"
    - "Application\\Sonata\\MediaBundle\\Entity\\Media"

Then in your CustomMediaType create your formtype as per normal..

namespace AppBundle\Form\Type;

use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\OptionsResolver\OptionsResolver;

class CustomMediaType extends MediaType
{
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
        ]);
    }

    public function getBlockPrefix()
    {
        return 'custom_media_type';
    }

    public function getParent()
    {
        return MediaType::class;
    }
}

Then somewhere in your twig inclusions, you can add your custom HTML...

{% block custom_media_type_widget %}
// Your HTML here
{% endblock %}

Upvotes: 2

Related Questions