I have installed Sonata Media Bundle in Symfony 4 and all is correct, but something is different respect t Symfony 3.
I can't see the service in Sonata Admin and when I add the sonata media bundle field to an Admin Class this shows a different template.
Here images:
Sonata Media Bundle template - Symfony 4, in User Entity
Sonata Media Bundle template - Symfony 3, in User Entity
Sonata Media Bundle template - Symfony 3, Adding new image
As you can see the template is not working in Symfony 4 or I'm missing something in my code.
My Sonata Media config
sonata_media.yaml
sonata_media:
class:
media: App\Application\Sonata\MediaBundle\Entity\Media
gallery: App\Application\Sonata\MediaBundle\Entity\Gallery
gallery_has_media: App\Application\Sonata\MediaBundle\Entity\GalleryHasMedia
default_context: default
contexts:
default:
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}
cdn:
server:
path: /upload/media
filesystem:
local:
# Directory for uploads should be writable
directory: "%kernel.project_dir%/public/upload/media"
create: false
providers:
# ...
file:
# the resizer must be set to false, otherwhise this can delete icon files from the fs
resizer: false
image:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
vimeo:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
youtube:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
dailymotion:
thumbnail: sonata.media.thumbnail.format # default value
# thumbnail: sonata.media.thumbnail.consumer.format # can be used to dispatch the resize action to async task
# thumbnail: sonata.media.thumbnail.liip_imagine # use the LiipImagineBundle to resize the image
My User's Admin Class
// src/Admin/OgaUsersAdmin.php
namespace App\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\MediaBundle\Form\Type\MediaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class OgaUsersAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('userFirstName', TextType::class)
->add('userCollection', MediaType::class, array(
'provider' => 'sonata.media.provider.image',
'context' => 'default'
));
}
protected function configureDatagridFilters(DatagridMapper $datagridMapper)
{
$datagridMapper->add('userFirstName');
}
protected function configureListFields(ListMapper $listMapper)
{
$listMapper->addIdentifier('userFirstName');
}
}
My Users Entity and Media Bundle field
namespace App\Entity;
use Application\Sonata\MediaBundle\Entity\Media;
use Doctrine\ORM\Mapping as ORM;
/**
* OgaUsers
*
* @ORM\Table(name="oga_users", indexes={@ORM\Index(name="memb_id_idx", columns={"memb_id"}), @ORM\Index(name="comp_id_idx", columns={"comp_id"}), @ORM\Index(name="u_ui_id_idx", columns={"user_collection"})})
* @ORM\Entity
*/
class OgaUsers
{
/**
* @var int
*
* @ORM\Column(name="user_id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $userId;
/**
* @var Media
*
* @ORM\ManyToOne(targetEntity="App\Application\Sonata\MediaBundle\Entity\Media")
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="userCollection", referencedColumnName="id")
* })
*/
private $userCollection;
Getter and Settter
public function getUserCollection(): ?\App\Application\Sonata\MediaBundle\Entity\Media
{
return $this->userCollection;
}
public function setUserCollection(?\App\Application\Sonata\MediaBundle\Entity\Media $userCollection): self
{
$this->userCollection = $userCollection;
return $this;
}
Thank's
After search for some weeks.
I have the answer.
First I need to add this line to my sonata_media.yaml, to see the Media Library in Admin Dashboard.
db_driver: doctrine_orm # or doctrine_mongodb, doctrine_phpcr it is mandatory to choose one here
After in Admin in configureFormFields, just need to add ModellistType::class, to the media field.
class OgaUsersAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper->add('userFirstname', TextType::class)
->add('userImage', ModelListType::class);
}
Hope it helps!!