Mk6ix Mk6ix - 4 months ago 21
PHP Question

SonataMediaBundle : a way to remove unlink thickbox

So i want to remove the "Unlink" thickbox and "Binary content" from the form of SonataMediaBundle :

enter image description here

I found nothing on google. Should i use Javascript to hide them ?

Answer

Unlink

I extended the Media Type form to make the unlink checkbox optional (based on abadius' answer):

# src/MyNamespace/AppBundle/Form/Extension/MediaTypeExtension.php

namespace MyNamespace\AppBundle\Form\Extension;

use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\AbstractTypeExtension;
use Symfony\Component\Form\FormBuilderInterface;

class MediaTypeExtension extends AbstractTypeExtension
{
    /**
     * {@inheritdoc}
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'show_unlink' => true,
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        if (!$options['show_unlink']) {
            $builder->add('unlink', 'hidden', array(
                'mapped'   => false,
                'data'     => false,
                'required' => false,
            ));
        }
    }

    /**
     * {@inheritdoc}
     */
    public function getExtendedType()
    {
        return 'sonata_media_type';
    }
}

Add it as a form extension service:

# Bundle config

mynamespace.form.type_extension.media:
    class: MyNamespace\AppBundle\Form\Extension\MediaTypeExtension
    tags:
        -
            name: form.type_extension
            alias: sonata_media_type

Binary Content

Here you need to override the File provider to change or remove the label (source):

# src/Application/Sonata/MediaBundle/Provider/FileProvider.php

namespace Application\Sonata\MediaBundle\Provider;

use Sonata\MediaBundle\Provider\FileProvider as BaseFileProvider;
use Symfony\Component\Form\FormBuilder;

class FileProvider extends BaseFileProvider
{
    /**
     * {@inheritdoc}
     */
    public function buildMediaType(FormBuilder $formBuilder)
    {
        $formBuilder->add('binaryContent', 'file', array(
            'label' => false,
        ));
    }
}

And override the parameter in your app config:

# app/config/config.yml

parameters:
    sonata.media.provider.file.class: Application\Sonata\MediaBundle\Provider\FileProvider
Comments