1
votes

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: ~
1
Sorry, I not quite understand the question. Your gallery need to accept only one type of media?jjgarcía
@jjgarcía Exactly. I want gallery only of youtube videos, or images. Not both or more.George Irimiciuc

1 Answers

2
votes

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