简体   繁体   中英

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 to my project ( ) an now I am able to modify the layout. 到我的项目( ),现在我可以修改了布局。 So long, everything works very fine.

The Template finally uses to write out the Upload Button and a Checkbox to delete uploaded files including the labels. 来写出上传按钮和复选框以删除包含标签的上传文件。 This is done in the 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 使用以下代码

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

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 %}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM