admin管理员组

文章数量:1278820

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

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

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

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

Share Improve this question asked Aug 19, 2014 at 14:45 KubadevKubadev 86510 silver badges26 bronze badges
Add a ment  | 

7 Answers 7

Reset to default 9

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

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');
    } 

To hide unlink, change this near line 63 in MediaType.php in Type:

$builder->add('unlink', 'hidden', array(
        'mapped'   => false,
        'data'     => false,
        'required' => false
    ));

to remove binary content, i'm searching for now... Anybody have a solution?

Best regards.

In the accepted answer You must add the extended_type option for Symfony >= 3.1 So the correct service registration is :

# Bundle config

mynamespace.form.type_extension.media:
    class: MyNamespace\AppBundle\Form\Extension\MediaTypeExtension
    tags:
        -
            name: form.type_extension
            extended_type: 'Sonata\MediaBundle\Form\Type\MediaType'
            alias: sonata_media_type

In the example provided by malberts to hide / override the label remember you can access the current widget label of your linked form thanks to the formBuilder getOption method.

public function buildMediaType(FormBuilder $formBuilder)
{
    $formBuilder->add('binaryContent', 'file', array(
        'label' => $formBuilder->getOption('label'),
    ));
}

To have a plete remove element 'unlink' from element of type 'sonata_media_type'

Exemple set into a form :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add(
        'mediaProfil',
        'sonata_media_type',
        array(
            'provider' => 'sonata.media.provider.image',
            'context'  => 'teams',
            'required' => false
        )
    );


    $builder->get('mediaProfil')->remove('unlink');
}

Or inside a controller:

public function editProfileAction()
{

    /** @var \Symfony\Component\Form\Form $form */
    $form        = $this->container->get('sonata.user.profile.form');
    $form->get('mediaProfil')->remove('unlink');
}

But an explicit call of element 'unlink' is done into sonataMedia

PR: https://github./sonata-project/SonataMediaBundle/pull/666

Or simply add into you're form :

        // fix https://github./sonata-project/SonataMediaBundle/pull/666
    $builder->addEventListener(
        FormEvents::PRE_SUBMIT,
        function (FormEvent $event) use ($formModifier) {

            $event->getForm()->get('mediaProfil')->add('unlink', null, array('mapped' => false,));
        }
    );

if you like hidden unlink and binary content from media/admin this for me work

protected function configureFormFields(FormMapper $formMapper)
{

    $formMapper
        ->add('numerocamera')
        ->add('tipocamera')
        ->add('media', 'sonata_media_type', array(
             'label'=> 'carica immagine 1',
             'provider' => 'sonata.media.provider.image',
             'context'  => 'camere'
        ))
        ->add('media1', 'sonata_media_type', array(
             'label'=> 'carica immagine 2',
             'provider' => 'sonata.media.provider.image',
             'context'  => 'camere'
        ))

        ->add('media2', 'sonata_media_type', array(
             'label'=> 'carica immagine 3',
             'provider' => 'sonata.media.provider.image',
             'context'  => 'camere'
        ))
        ->add('media3', 'sonata_media_type', array(
             'label'=> 'inserisci url video youtube 1',
             'provider' => 'sonata.media.provider.youtube',
             'context'  => 'camere'
        ))

    ;
        **$formMapper->get('media')->add('unlink', 'hidden', ['mapped' => false, 'data' => false]) 
                   ->add('binaryContent', 'file', ['label' => false]);
        $formMapper->get('media1')->add('unlink', 'hidden', ['mapped' => false, 'data' => false])
                   ->add('binaryContent', 'file', ['label' => false]); 
        $formMapper->get('media2')->add('unlink', 'hidden', ['mapped' => false, 'data' => false])
                   ->add('binaryContent', 'file', ['label' => false]);
        $formMapper->get('media3')->add('unlink', 'hidden', ['mapped' => false, 'data' => false])
                   ->add('binaryContent', 'url', ['label' => false]); 
}**

本文标签: javascriptSonataMediaBundlea way to remove unlink thickboxStack Overflow