StockBreak
StockBreak

Reputation: 2895

Sonata Admin Media Picker

I am using Sonata Admin for my back-end and following this guide I was able to setup a relation between my entities and a media object.

This is the relation:

/**
 * @var \Application\Sonata\MediaBundle\Entity\Media
 *
 * @ORM\ManyToOne(targetEntity="\Application\Sonata\MediaBundle\Entity\Media", cascade={"persist", "remove"})
 * @ORM\JoinColumn(name="image_id", referencedColumnName="id", nullable=false)
 */
protected $image;

This is the admin class:

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
        ->add('name')
        ->add('family')
        ->add('image', MediaType::class, array(
            'provider'  => 'sonata.media.provider.image',
            'context'   => 'default',
            'required'  => false,
        ))
    ;
}

These are my settings:

# Twig Configuration
twig:
    debug: '%kernel.debug%'
    strict_variables: '%kernel.debug%'

    # Sonata form themes
    form_themes:
        - 'SonataCoreBundle:Form:colorpicker.html.twig'
        - 'SonataMediaBundle:Form:media_widgets.html.twig'

The result looks like this:

enter image description here

I can successfully insert a new image and save it along my entities but I can't select an entity which already exists, which settings do i need to get a media picker in a modal window?

Upvotes: 1

Views: 548

Answers (1)

M Khalid Junaid
M Khalid Junaid

Reputation: 64476

To choose any existing media you can use sonata_type_model_list

Which will show you a field with Add/List options for your model, In your case it will show medias and you can select from the existing medias

Upvotes: 1

Related Questions