Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 777
Kubadev Avatar asked Jan 19 '26 14:01

Kubadev


2 Answers

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
like image 94
malberts Avatar answered Jan 21 '26 05:01

malberts


you can remove the unlink check box with :

  protected function configureFormFields(FormMapper $formMapper)
    {
        $fileFieldOptions = array(
            'provider' => 'sonata.media.provider.file',
            'context' => 'default',
            'label' => 'File',
            'required' => true
        );
        $formMapper
            ->add(
                'file',
                'sonata_media_type',
                $fileFieldOptions
            );
        $formMapper->get('file')->remove('unlink');
    } 

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!