1. Go to this page and download the library: Download ranky/media-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
# config/routes/ranky_media.php
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
return static function (RoutingConfigurator $routes) {
$routes
->import('@RankyMediaBundle/config/routes.php') // annotation or attributes
->prefix('/admin')
;
};
# config/packages/ranky_media.php
return static function (RankyMediaConfig $rankyMediaConfig) {
$rankyMediaConfig
->mediaEntity(Media::class)
->userEntity(User::class)
->apiPrefix('/admin') // Optional: The same prefix you use when importing the routes must be the same here
;
};
# src/Entity/Media.php
use Doctrine\ORM\Mapping as ORM;
use Ranky\MediaBundle\Domain\Model\Media as BaseMedia;
#[ORM\Table(name: 'ranky_media')]
#[ORM\Entity(repositoryClass: MediaRepository::class)]
class Media extends BaseMedia
{
}
# src/Repository/MediaRepository.php
class MediaRepository extends DoctrineOrmMediaRepository
{
public function __construct(
ManagerRegistry $registry,
DoctrineCriteriaBuilderFactory $doctrineCriteriaBuilderFactory,
UidMapperPlatform $uidMapperPlatform,
) {
parent::__construct(
$registry,
$doctrineCriteriaBuilderFactory,
$uidMapperPlatform,
Media::class
);
}
}
// config/packages/doctrine.php
// Sqlite
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Sqlite\MimeSubType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Sqlite\MimeType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Sqlite\Month;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Sqlite\Year;
// MySQL
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\MimeSubType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\MimeType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\Month;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Mysql\Year;
// Postgres
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\MimeSubTypee;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\MimeType;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\Month;
use Ranky\MediaBundle\Infrastructure\Persistence\Dql\Postgresql\Year;
use Symfony\Config\DoctrineConfig;
return static function (DoctrineConfig $doctrineConfig): void {
// ...
$orm = $doctrineConfig->orm();
$em = $orm->entityManager('default');
$dql = $em->dql();
$dql->stringFunction('MIME_TYPE', MimeType::class);
$dql->stringFunction('MIME_SUBTYPE', MimeSubType::class);
$dql->datetimeFunction('YEAR', Year::class);
$dql->datetimeFunction('MONTH', Month::class);
}
use Ranky\MediaBundle\Presentation\Form\RankyMediaFileManagerType;
class MyFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('medias', RankyMediaFileManagerType::class, [
'label' => 'Media Collection',
'association' => true,
'modal_title' => 'Gallery',
'multiple_selection' => true,
])
;
}
}
use Ranky\MediaBundle\Presentation\Form\EasyAdmin\EARankyMediaFileManagerField;
// ...
public function configureFields(string $pageName): iterable
{
// ...
yield EARankyMediaFileManagerField::new('path')->savePath();
yield EARankyMediaFileManagerField::new('paths')->multipleSelection()->savePath();
yield EARankyMediaFileManagerField::new('mediaId');
yield EARankyMediaFileManagerField::new('gallery')->multipleSelection()->modalTitle('Gallery');
yield EARankyMediaFileManagerField::new('media')->association();
yield EARankyMediaFileManagerField::new('medias')->association()->multipleSelection();
}
use Ranky\MediaBundle\Infrastructure\Event\PreCreateEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class MyEventSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
return [
PreCreateEvent::NAME => 'onPreCreate',
];
}
public function onPreCreate(PreCreateEvent $event)
{
// do something
}
}