简体   繁体   English

如何从URL @admin_sonata_media_media_create的sonata_type_model_list中删除类别

[英]How to remove category from sonata_type_model_list at url @admin_sonata_media_media_create

I'm using Media in User Entity(avatar). 我在用户实体(头像)中使用媒体。

At first I used sonata_media_type. 最初,我使用sonata_media_type。 It worked good. 效果很好。

The problem is I'm using ManyToOne - so the admin can select one from the list. 问题是我正在使用ManyToOne-管理员可以从列表中选择一个。 To achieve this I have to use sonata_type_model_list - this has list,new,delete actions. 为此,我必须使用sonata_type_model_list-这具有list,new,delete操作。 I removed delete by 'btn_delete' => ''. 我通过'btn_delete'=>''删除了删除。 Here the list action works good(up to now). 在这里,列表动作效果很好(到目前为止)。

The real PROBLEM is at new action. 真正的问题正在采取新的行动。 The new action window load from ajax - and it has File/Reference, Category (Two fields). 从ajax加载新的操作窗口-它具有“文件/参考”,“类别”(两个字段)。

Here I need to remove Category field entirely(list,new,delete) . 在这里,我需要完全删除Category字段(list,new,delete) Why do we need this? 我们为什么需要这个? Because it is useless!. 因为没用!

  • LIST - only display the 'context' => 'profile' from 'link_parameters'. LIST-仅显示'link_parameters'中的'context'=>'profile'。 So here the LIST action is useless. 因此,这里的LIST操作是没有用的。
  • NEW - New action can create new context, but it will not display in the LIST(right now). NEW-新动作可以创建新的上下文,但是不会显示在LIST中(现在)。 So I don't need this. 所以我不需要这个。 If I need I'll create from ClassificationBundle. 如果需要的话,我将通过ClassificationBundle创建。
  • DELETE - Delete action has no effect(right now - here). 删除-删除操作无效(现在-在此处)。

在此处输入图片说明

MY-RESEARCH: 我的研究:

I tried to modify the TEMPLATE - but I can't find the correct twig file. 我试图修改模板-但找不到正确的树枝文件。 It points to parent() - which is pointing to admin bundle! 它指向parent()-指向管理包!

To validation File/Reference - I created my own ImageProvider(as per doc) - It works(validate) good. 验证文件/引用 -我创建了自己的ImageProvider(根据文档)-效果很好(验证)。

I tried to remove Category field(check image) - but failed. 我试图删除类别字段(检查图像)-但失败了。

My code is: 我的代码是:

    class ImageProvider extends BaseProvider{...}
        public function buildCreateForm(FormMapper $formMapper) {
// This works - changed File/Reference to ok
            $formMapper->add('binaryContent', 'file', array('label' => 'ok',
                'constraints' => array(
                    new NotBlank(),
                ),
            ));
// This works - added a new text field
            $formMapper->add('context', 'text', ['attr' => ['class' => 'fz_rocks']]);
// This not working - also ->add('category') - has no effect even when attr=hide and so on..
            $formMapper->remove('category');
        }

- --

To remove category field from media popup 从媒体弹出窗口中删除类别字段

  • You need to override media admin's class by overriding class parameter sonata.media.admin.media.class 您需要通过覆盖类参数sonata.media.admin.media.class来覆盖媒体管理员的类
  • Create you own admin class and extend it with sonata media's base admin class. 创建您自己的管理类,并使用Sonata Media的基本管理类进行扩展。
  • Override configureFormFields() method by defining in your admin class 通过在您的管理类中定义来覆盖configureFormFields()方法
  • Remove category field from $formMapper $formMapper删除类别字段

Override Sonata media class 覆盖Sonata媒体类

parameters:
    sonata.media.admin.media.class: Your\MediaBundle\Admin\ORM\MediaAdmin

Media Admin Class 媒体管理员班

namespace Your\MediaBundle\Admin\ORM;

use Sonata\MediaBundle\Admin\ORM\MediaAdmin as Admin;
// .. Other use statements 

class MediaAdmin extends Admin {

    /**
     * {@inheritdoc}
     */
    protected function configureFormFields( FormMapper $formMapper ) {
        $media = $this->getSubject();

        if ( ! $media ) {
            $media = $this->getNewInstance();
        }

        if ( ! $media || ! $media->getProviderName() ) {
            return;
        }

        $formMapper->add( 'providerName', 'hidden' );

        $formMapper->getFormBuilder()->addModelTransformer( new ProviderDataTransformer( $this->pool, $this->getClass() ), true );

        $provider = $this->pool->getProvider( $media->getProviderName() );

        if ( $media->getId() ) {
            $provider->buildEditForm( $formMapper );
        } else {
            $provider->buildCreateForm( $formMapper );
        }

    }

}

I solved by hiding the categoy field. 我通过隐藏类别字段解决了。 If i removed completely it cause problem sometimes. 如果我将其完全移除,有时会引起问题。 Safe is to hide. 安全是躲藏。

To achieve this i use custom providers, as per sonata-media doc creating_a_provider_class.rst 为此,我根据奏鸣曲媒体文件creating_a_provider_class.rst使用自定义提供程序

namespace Application\Sonata\MediaBundle\Provider;
class ImageProvider extends BaseProvider {
    public function buildCreateForm(FormMapper $formMapper) {
        $formMapper->add('binaryContent', 'file', ['label' => 'Upload a new file', 'constraints' => [new NotBlank(), new NotNull()]])->end();
        $formMapper->with('General', ['class' => 'hidden'])->add('category');
    }
    public function buildEditForm(FormMapper $formMapper) {
        parent::buildEditForm($formMapper);
        $formMapper->add('binaryContent', 'file', ['label' => 'Upload a new file', 'required' => FALSE])->end();
        $formMapper->with('General', ['class' => 'hidden'])->add('category');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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