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 badges7 Answers
Reset to default 9Unlink
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
版权声明:本文标题:javascript - SonataMediaBundle : a way to remove unlink thickbox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741225393a2361761.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论