A Symfony Media Manager. Inspired by SonataMediaBundle's use of Media Providers to add different Media types.
Note: This bundle is still very much a work in progress, so BC-breaks will happen until the first stable release.
Add the bundle to your composer.json
composer require opifer/media-bundle dev-master
Register the bundle in app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new JMS\SerializerBundle\JMSSerializerBundle(),
new Knp\Bundle\GaufretteBundle\KnpGaufretteBundle(),
new Liip\ImagineBundle\LiipImagineBundle(),
new Opifer\MediaBundle\OpiferMediaBundle()
);
}
You should create your own Media entity that extends Opifer\MediaBundle\Model\Media
.
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Opifer\MediaBundle\Model\Media as BaseMedia;
/**
* @ORM\Table(name="media")
* @ORM\Entity(repositoryClass="Opifer\MediaBundle\Repository\MediaRepository")
*/
class Media extends BaseMedia
{
// Add custom functionality...
}
And reference to it in your app/config/config.yml
opifer_media:
media_class: AppBundle\Entity\Media
This bundle comes with an AngularJS media manager included. To use it, you'll need to include some necessary javascript files and CSS files into your templates.
First, make sure you installed all asset dependencies. You can download them manually, copy the bower.json
file to your own bundle and run bower install
or copy the bower.json
content to your own bower dependencies.
Then, add the dependencies to your templates.
{% stylesheets
'bundles/opifermedia/css/dropzone.less'
'bundles/opifermedia/css/main.less'
filter='less,cssrewrite' %}
<link rel="stylesheet" href="{{ asset_url }}" />
{% endstylesheets %}
...
{% javascripts
'@AppBundle/Resources/public/components/ng-file-upload/angular-file-upload-shim.min.js'
'@AppBundle/Resources/public/components/angular/angular.js'
'@AppBundle/Resources/public/components/angular-route/angular-route.js'
'@AppBundle/Resources/public/components/angular-resource/angular-resource.js'
'@AppBundle/Resources/public/components/ngInfiniteScroll/build/ng-infinite-scroll.js'
'@AppBundle/Resources/public/components/ng-file-upload/angular-file-upload.min.js'
'@OpiferMediaBundle/Resources/public/js/dropzone.js'
'@OpiferMediaBundle/Resources/public/js/main.js'
'@OpiferMediaBundle/Resources/public/app/modal/modal.js'
'@OpiferMediaBundle/Resources/public/app/medialibrary/medialibrary.js'
'bundles/fosjsrouting/js/router.js' %}
<script type="text/javascript" src="{{ asset_url }}"></script>
{% endjavascripts %}
Then, create an Angular module that requires the following modules:
'use strict';
angular.module('App', [
'ngRoute',
'ngResource',
'mediaLibrary',
'angularFileUpload',
]);
Make sure you add the angular module in your template by adding the the file to your {% javascripts %}
list.
To make the mediamanager accessible in the browser, add the routes to your routing.yml
:
opifer_media:
resource: "@OpiferMediaBundle/Resources/config/routing.yml"
prefix: /admin
Create a relationship between the media entity and any other entity. For example, Users must be able to add media to a Content item.
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Opifer\MediaBundle\Model\MediaInterface;
class Content
{
/**
* @var Media
*
* @ORM\ManyToOne(targetEntity="Media")
* @ORM\JoinColumn(name="media_id", referencedColumnName="id", onDelete="SET NULL")
*/
protected $image;
/**
* Set image
*
* @param string $image
*
* @return Content
*/
public function setImage(MediaInterface $image = null)
{
$this->image = $image;
return $this;
}
/**
* Get image
*
* @return MediaInterface
*/
public function getImage()
{
return $this->image;
}
}
In your content FormType, add the mediapicker
form type:
namespace AppBundle\Form\Type;
use Doctrine\ORM\EntityRepository;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
class ContentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
// ...
->add('image', 'mediapicker', [
'multiple' => false,
'property' => 'name',
'class' => 'AppBundle:Media',
'query_builder' => function (EntityRepository $mediaRepository) {
return $mediaRepository->createQueryBuilder('m')->add('orderBy', 'm.name ASC');
}
])
;
}
}