Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade Sonata to v4 #1963

Merged
4 changes: 2 additions & 2 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ jobs:
- php-version: '8.0'
dependencies: highest
allowed-to-fail: false
variant: 'sonata-project/admin-bundle:"3.*"'
variant: 'imagine/imagine:"1.*"'
- php-version: '8.0'
dependencies: highest
allowed-to-fail: false
variant: 'imagine/imagine:"1.*"'
variant: 'sonata-project/admin-bundle:"dev-master"'
- php-version: '8.0'
dependencies: highest
allowed-to-fail: false
Expand Down
25 changes: 12 additions & 13 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"psr/http-message": "^1.0",
"psr/log": "^1.0",
"sonata-project/doctrine-extensions": "^1.10.1",
"sonata-project/form-extensions": "^0.1.2 || ^1.4",
"sonata-project/twig-extensions": "^0.1.1 || ^1.3",
"sonata-project/form-extensions": "^1.4",
"sonata-project/twig-extensions": "^1.3",
"symfony/config": "^4.4 || ^5.2",
"symfony/console": "^4.4",
"symfony/dependency-injection": "^4.4 || ^5.2",
Expand All @@ -55,7 +55,6 @@
"symfony/routing": "^4.4 || ^5.2",
"symfony/security-bundle": "^4.4",
"symfony/security-core": "^4.4",
"symfony/templating": "^4.4 || ^5.2",
"symfony/translation": "^4.4",
"symfony/translation-contracts": "^1.1 || ^2.0",
"symfony/twig-bridge": "^4.4",
Expand All @@ -74,13 +73,13 @@
"liip/imagine-bundle": "<1.9",
"nelmio/api-doc-bundle": "<2.13.5 || >=4.0",
"rackspace/php-opencloud": "<1.6",
"sonata-project/block-bundle": "<3.17 || >=4.0",
"sonata-project/admin-bundle": "4.0.0-alpha-1",
"sonata-project/block-bundle": "<4.0",
"sonata-project/classification-bundle": "<3.0 || >= 5.0",
"sonata-project/core-bundle": "<3.20",
"sonata-project/doctrine-orm-admin-bundle": "<3.0",
"sonata-project/doctrine-orm-admin-bundle": "<4.0,4.0.0-alpha-1",
"sonata-project/formatter-bundle": "<3.4",
"sonata-project/notification-bundle": "<3.3",
"sonata-project/seo-bundle": "<2.5 || >=3.0",
"sonata-project/notification-bundle": "<4.0",
"sonata-project/seo-bundle": "<3.0",
"symfony/monolog-bundle": "<2.4",
"tilleuls/ckeditor-sonata-media-bundle": "<1.0"
},
Expand All @@ -92,12 +91,12 @@
"matthiasnoback/symfony-config-test": "^4.2",
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
"nelmio/api-doc-bundle": "^2.13.5",
"sonata-project/admin-bundle": "^3.98.2",
"sonata-project/block-bundle": "^3.17",
"sonata-project/admin-bundle": "^4.0@alpha",
"sonata-project/block-bundle": "^4.0",
"sonata-project/datagrid-bundle": "^3.0.1",
"sonata-project/doctrine-orm-admin-bundle": "^3.14",
"sonata-project/notification-bundle": "^3.12",
"sonata-project/seo-bundle": "^2.5",
"sonata-project/doctrine-orm-admin-bundle": "^4.0@alpha",
"sonata-project/notification-bundle": "^4.0@dev",
"sonata-project/seo-bundle": "^3.0@dev",
"symfony/browser-kit": "^4.4 || ^5.2",
"symfony/phpunit-bridge": "^5.2",
"symfony/security-csrf": "^4.4 || ^5.2"
Expand Down
50 changes: 21 additions & 29 deletions src/Admin/BaseMediaAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\AdminBundle\Object\Metadata;
use Sonata\AdminBundle\Object\MetadataInterface;
use Sonata\MediaBundle\Form\DataTransformer\ProviderDataTransformer;
use Sonata\MediaBundle\Model\CategoryManagerInterface;
use Sonata\MediaBundle\Provider\MediaProviderInterface;
Expand Down Expand Up @@ -49,19 +50,18 @@ public function __construct($code, $class, $baseControllerName, Pool $pool, ?Cat
parent::__construct($code, $class, $baseControllerName);

$this->pool = $pool;

$this->categoryManager = $categoryManager;
}

public function prePersist($media): void
public function prePersist(object $media): void
{
$parameters = $this->getPersistentParameters();
$media->setContext($parameters['context']);
}

public function getPersistentParameters()
public function configurePersistentParameters(): array
{
$parameters = parent::getPersistentParameters();
$parameters = [];

if (!$this->hasRequest()) {
return $parameters;
Expand Down Expand Up @@ -103,30 +103,26 @@ public function getPersistentParameters()
]);
}

public function getNewInstance()
public function alterNewInstance(object $object): void
{
$media = parent::getNewInstance();

if ($this->hasRequest()) {
if ($this->getRequest()->isMethod('POST')) {
$uniqid = $this->getUniqid();
$media->setProviderName($this->getRequest()->get($uniqid)['providerName']);
$object->setProviderName($this->getRequest()->get($uniqid)['providerName']);
} else {
$media->setProviderName($this->getRequest()->get('provider'));
$object->setProviderName($this->getRequest()->get('provider'));
}

$media->setContext($context = $this->getRequest()->get('context'));
$object->setContext($context = $this->getRequest()->get('context'));

if (null !== $this->categoryManager && $categoryId = $this->getPersistentParameter('category')) {
$category = $this->categoryManager->find($categoryId);

if ($category && $category->getContext()->getId() === $context) {
$media->setCategory($category);
$object->setCategory($category);
}
}
}

return $media;
}

/**
Expand All @@ -137,7 +133,7 @@ public function getPool()
return $this->pool;
}

public function getObjectMetadata($object)
public function getObjectMetadata(object $object): MetadataInterface
{
$provider = $this->pool->getProvider($object->getProviderName());

Expand All @@ -149,41 +145,37 @@ public function getObjectMetadata($object)
return new Metadata($object->getName(), $object->getDescription(), $url);
}

protected function configureListFields(ListMapper $listMapper): void
protected function configureListFields(ListMapper $list): void
{
$listMapper
$list
->addIdentifier('name')
->add('description')
->add('enabled')
->add('size');
}

protected function configureFormFields(FormMapper $formMapper): void
protected function configureFormFields(FormMapper $form): void
{
$media = $this->getSubject();

if (!$media) {
$media = $this->getNewInstance();
}
$media = $this->hasSubject() ? $this->getSubject() : $this->getNewInstance();

if (!$media || !$media->getProviderName()) {
if (!$media->getProviderName()) {
return;
}

$formMapper->add('providerName', HiddenType::class);
$form->add('providerName', HiddenType::class);

$formMapper->getFormBuilder()->addModelTransformer(new ProviderDataTransformer($this->pool, $this->getClass()), true);
$form->getFormBuilder()->addModelTransformer(new ProviderDataTransformer($this->pool, $this->getClass()), true);

$provider = $this->pool->getProvider($media->getProviderName());

if ($media->getId()) {
$provider->buildEditForm($formMapper);
$provider->buildEditForm($form);
} else {
$provider->buildCreateForm($formMapper);
$provider->buildCreateForm($form);
}

if (null !== $this->categoryManager) {
$formMapper->add('category', ModelListType::class, [], [
$form->add('category', ModelListType::class, [], [
'link_parameters' => [
'context' => $media->getContext(),
'hide_context' => true,
Expand Down
43 changes: 17 additions & 26 deletions src/Admin/GalleryAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
use Sonata\MediaBundle\Provider\Pool;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;

/**
* @final since sonata-project/media-bundle 3.21.0
*/
class GalleryAdmin extends AbstractAdmin
final class GalleryAdmin extends AbstractAdmin
{
/**
* @var Pool
Expand Down Expand Up @@ -52,39 +49,33 @@ public function prePersist($gallery): void
$gallery->setContext($parameters['context']);
}

public function postUpdate($gallery)
public function postUpdate(object $object): void
{
$gallery->reorderGalleryItems();
$object->reorderGalleryItems();
}

public function getPersistentParameters()
public function configurePersistentParameters(): array
{
$parameters = parent::getPersistentParameters();

if (!$this->hasRequest()) {
return $parameters;
return [];
}

return array_merge($parameters, [
return [
'context' => $this->getRequest()->get('context', $this->pool->getDefaultContext()),
]);
];
}

public function getNewInstance()
public function alterNewInstance(object $object): void
{
$gallery = parent::getNewInstance();

if ($this->hasRequest()) {
$gallery->setContext($this->getRequest()->get('context'));
$object->setContext($this->getRequest()->get('context'));
}

return $gallery;
}

protected function configureFormFields(FormMapper $formMapper): void
protected function configureFormFields(FormMapper $form): void
{
// define group zoning
$formMapper
$form
// NEXT_MAJOR: Change Gallery key to `form_group.gallery` and update translations files.
->with('Gallery', ['class' => 'col-md-9'])->end()
// NEXT_MAJOR: Change Options key to `form_group.options` and update translations files.
Expand All @@ -106,15 +97,15 @@ protected function configureFormFields(FormMapper $formMapper): void
$contexts[$contextItem] = $contextItem;
}

$formMapper
$form
->with('Options')
->add('context', ChoiceType::class, [
'choices' => $contexts,
'choice_translation_domain' => 'SonataMediaBundle',
])
->add('enabled', null, ['required' => false])
->add('name')
->ifTrue($formats)
->ifTrue(!empty($formats))
->add('defaultFormat', ChoiceType::class, ['choices' => $formats])
->ifEnd()
->end()
Expand All @@ -129,18 +120,18 @@ protected function configureFormFields(FormMapper $formMapper): void
->end();
}

protected function configureListFields(ListMapper $listMapper): void
protected function configureListFields(ListMapper $list): void
{
$listMapper
$list
->addIdentifier('name')
->add('enabled', 'boolean', ['editable' => true])
->add('context', 'trans', ['catalogue' => 'SonataMediaBundle'])
->add('defaultFormat', 'trans', ['catalogue' => 'SonataMediaBundle']);
}

protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
protected function configureDatagridFilters(DatagridMapper $filter): void
{
$datagridMapper
$filter
->add('name')
->add('enabled')
->add('context', null, [
Expand Down
13 changes: 5 additions & 8 deletions src/Admin/GalleryItemAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,9 @@
use Sonata\AdminBundle\Form\Type\ModelListType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;

/**
* @final since sonata-project/media-bundle 3.21.0
*/
class GalleryItemAdmin extends AbstractAdmin
final class GalleryItemAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper): void
protected function configureFormFields(FormMapper $form): void
{
$link_parameters = [];

Expand All @@ -40,17 +37,17 @@ protected function configureFormFields(FormMapper $formMapper): void
}
}

$formMapper
$form
->add('media', ModelListType::class, ['required' => false], [
'link_parameters' => $link_parameters,
])
->add('enabled', null, ['required' => false])
->add('position', HiddenType::class);
}

protected function configureListFields(ListMapper $listMapper): void
protected function configureListFields(ListMapper $list): void
{
$listMapper
$list
->add('media')
->add('gallery')
->add('position')
Expand Down
11 changes: 4 additions & 7 deletions src/Admin/ODM/MediaAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,13 @@
namespace Sonata\MediaBundle\Admin\ODM;

use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\MediaBundle\Admin\BaseMediaAdmin as Admin;
use Sonata\MediaBundle\Admin\BaseMediaAdmin;

/**
* @final since sonata-project/media-bundle 3.21.0
*/
class MediaAdmin extends Admin
final class MediaAdmin extends BaseMediaAdmin
{
protected function configureDatagridFilters(DatagridMapper $datagridMapper): void
protected function configureDatagridFilters(DatagridMapper $filter): void
{
$datagridMapper
$filter
->add('name')
->add('providerReference')
->add('enabled')
Expand Down
Loading