1

I'm using SonataMediaBundle and want to add a Gallery of Images to an entity, let's say Ad. Each Ad has a Gallery that can have multiple Media. I've been following this other answer, too and it has been pretty useful.

I did it already, I can add Galleries to my Entity and add all sort of Media to that Gallery, but what I want is in backend to skip the Provider selection for when a Media is added to the Gallery, and to automatically select the ImageProvider, so that I can create an image-only Gallery.

class Ad
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="title", type="string", length=255,nullable=true)
     */
    private $adTitle;

    /**
     * @GalleryHasMoreThanNineMedia
     * @ORM\OneToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Gallery",mappedBy="ad",cascade={"persist"})
     * @ORM\JoinColumn(name="gallery", referencedColumnName="id",nullable=true)
     */
    private $gallery;
}

And in Ad sonata admin class:

 protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('adTitle')   
            ->add('gallery', 'sonata_type_model_list', array(
                'cascade_validation' => true,
                'required' => false,
            ), array(
                    'edit' => 'inline',
                    'inline' => 'table',
                    'sortable' => 'position',
                    'link_parameters' => array('provider' => 'sonata.media.provider.image'),
                    'admin_code' => 'sonata.media.admin.gallery',
                )
            );
    }

Somehow I need to pass the link_parameters not to the Gallery form, but to each GalleryHasMedia being created, so that each of the new Media is an image of ImageProvider and gets the validation of that provider(the extensions,MIME Type, thumbnail etc.)

So this is how the form looks like

enter image description here

Which opens this modal window where I can add GalleryHasMedia to a Gallery.

enter image description here

What I want is upon adding the Media, this modal to choose the provider to not appear, but instead select ImageProvider automatically. Is it possible to pass link_parameters' => array('provider' => 'sonata.media.provider.youtube') inside a nested form value in the admin class?

enter image description here

Is it possible? Do I have to modify it in my Admin class or overwrite some other form/class?

Added my SonataMedia configuration:

sonata_media:
    # if you don't use default namespace configuration
    #class:
    #    media: MyVendor\MediaBundle\Entity\Media
    #    gallery: MyVendor\MediaBundle\Entity\Gallery
    #    gallery_has_media: MyVendor\MediaBundle\Entity\GalleryHasMedia
    db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
    default_context: default # you need to set a context
    contexts:
        default:  # the default context is mandatory
            providers:
                - sonata.media.provider.dailymotion
                - sonata.media.provider.youtube
                - sonata.media.provider.image
                - sonata.media.provider.file
                - sonata.media.provider.vimeo
            formats:
                small: { width: 100 , quality: 70}
                big:   { width: 500 , quality: 70}
        profile_pics:  # the default context is mandatory
            providers:
                - sonata.media.provider.image
            formats:
                small: { width: 100 , quality: 70}
                big:   { width: 500 , quality: 70}
        personal_albums:
            download:
                strategy: sonata.media.security.private_strategy
                mode: http
            providers:
                - sonata.media.provider.image
                - sonata.media.provider.file
                - sonata.media.provider.private
            formats:
                default:  { width: 100 , quality: 70}
    cdn:
        server:
            path: %cdn_server_path% # http://media.sonata-project.org/

    filesystem:
        local:
            directory:  %kernel.root_dir%/../web/uploads/media
            create:     true
    providers:
        #image:
        #    resizer: false
        file:
            service:    sonata.media.provider.file
            resizer:    false
            filesystem: sonata.media.filesystem.local
            cdn:        sonata.media.cdn.server
            generator:  sonata.media.generator.default
            thumbnail:  sonata.media.thumbnail.format
            allowed_extensions: ['mp4','pdf', 'txt', 'rtf', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pttx', 'odt', 'odg', 'odp', 'ods', 'odc', 'odf', 'odb', 'csv', 'xml']
            allowed_mime_types: ['video/mp4','application/pdf', 'application/x-pdf', 'application/rtf', 'text/html', 'text/rtf', 'text/plain']
    buzz:
        connector:  sonata.media.buzz.connector.file_get_contents

doctrine:
    orm:
        entity_managers:
            default:
                mappings:
                    ApplicationSonataMediaBundle: ~
                    SonataMediaBundle: ~
Community
  • 1
  • 1
George Irimiciuc
  • 4,573
  • 8
  • 44
  • 88

1 Answers1

2

Try this.

config.yml here you define your allowed providers:

sonata_media:

    #...

    contexts:

        #...

        your_context:
            providers:
                - sonata.media.provider.image

            formats:
                admin: { width: 55 , height: 55 , quality: 80}
                small: { width: 75 , quality: 80}
                medium: { width: 125 , quality: 80}
                big: { width: 265 , quality: 80}

Entity.php

//...


/**
 * @ORM\OneToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Gallery",cascade={"all"}, orphanRemoval=true)
 * @ORM\JoinColumn(name="gallery", referencedColumnName="id")
 */
  private $gallery;

//...

EntityAdmin.php here you define your custom context with allowed providers:

//...

class EntityAdmin extends Admin {

    //...

    // Fields to be shown on create/edit forms
    protected function configureFormFields(FormMapper $formMapper) {

        //...

        $formMapper
                    ->add('gallery', 'sonata_type_model_list', array(
                        'btn_list' => false,
                        'help' => 'Your help text',
                            ), array(
                        'link_parameters' => array(
                            'context' => 'your_context'
                        ))
                    );

        //...

    }

    //...
}

I hope this helps.

These are the steps:

Step 1.

Create new

Step 2.

Add new

Step 3.

Add new

Step 4.

Select file

jjgarcía
  • 589
  • 1
  • 4
  • 26
  • This works but I still get the unnecessary step of having to select the image provider for each image that I want to upload. I was thinking maybe there is a way to skip that step and automatically assign the imageprovider to these new medias of the gallery. Somehow I need to send the provider in the request, when creating a new media in the gallery. – George Irimiciuc Mar 10 '16 at 07:42
  • Setting it as I say, there is no step to select a type of provider. I updated my answer with catches of all steps. – jjgarcía Mar 10 '16 at 16:53
  • Very strange. What version of SonataMedia are you using? 2.3 or master? I might have wrong version or a missing setting. I have master with Syfmony 2.8. Could that be the problem? – George Irimiciuc Mar 10 '16 at 17:23
  • These catches are on Symfony 2.7.9, I didn't try on Symfony 2.8. I do not think it's anything related to the version of Symfony. sonata-project/admin-bundle 2.3.7 and sonata-project/media-bundle 2.3.3 Maybe some other configuration "forgotten"? – jjgarcía Mar 10 '16 at 17:48
  • Well I'm using the master versions of the bundles, not 2.3. I've added my configuration to the OP. Judging by them, I'm using the default image provider settings. – George Irimiciuc Mar 10 '16 at 18:33
  • In the production environment, I use only stable versions of the bundles. Sorry, what do you mean by OP? – jjgarcía Mar 10 '16 at 18:38
  • Opening post, above. The question. They are stable but quite outdated, unfortunately. – George Irimiciuc Mar 10 '16 at 18:47
  • What new features the master versions need to use? – jjgarcía Mar 10 '16 at 18:52
  • I'm not sure, but they are a bit different. They use Category from ClassificationBundle and have slight differences. The forms are a bit different, too. Actually you can see at http://demo.sonata-project.org/ – George Irimiciuc Mar 10 '16 at 18:55
  • For instance if you go there to new gallery while being at the product_catalog context, it will ask you to select providers, and only image provider shows up. Which means that's how the master version works, so it's either not implemented or there's another condition. – George Irimiciuc Mar 10 '16 at 19:11
  • Sonata Admin 3 use AdminLTE 2, this look different. Is optimized to Symfony3, I have not needed any of the new features of the new version, and will look to migrate my projects to Symfony3 to use the new version of Sonata Admin. I have seen on your demo the behavior that you indicate. Sorry, I can not help you with this version of Sonata Admin – jjgarcía Mar 10 '16 at 19:45
  • No problem, I think this will do just fine and I'll just override some controllers. – George Irimiciuc Mar 10 '16 at 19:52