-->

How to remove category from sonata_type_model_list

2019-06-10 05:43发布

问题:

I'm using Media in User Entity(avatar).

At first I used sonata_media_type. It worked good.

The problem is I'm using ManyToOne - so the admin can select one from the list. To achieve this I have to use sonata_type_model_list - this has list,new,delete actions. I removed delete by '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).

Here I need to remove Category field entirely(list,new,delete). Why do we need this? Because it is useless!.

  • LIST - only display the 'context' => 'profile' from 'link_parameters'. So here the LIST action is useless.
  • NEW - New action can create new context, but it will not display in the LIST(right now). So I don't need this. If I need I'll create from 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!

To validation File/Reference - I created my own ImageProvider(as per doc) - It works(validate) good.

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

-

回答1:

To remove category field from media popup

  • You need to override media admin's class by overriding class parameter sonata.media.admin.media.class
  • Create you own admin class and extend it with sonata media's base admin class.
  • Override configureFormFields() method by defining in your admin class
  • Remove category field from $formMapper

Override Sonata media class

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

    }

}


回答2:

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

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