From 2f6213ef08faf221141c5905df5f9be3db7958b5 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Wed, 27 Oct 2021 21:58:32 +0200 Subject: [PATCH] Move appendParentObject into createNewInstance (#7549) * move appendParentObject into createNewInstance to allow creating new instances for which the parent entity will not be managed by sonata * add upgrade notes * Update UPGRADE-4.x.md Co-authored-by: Vincent Langlet Co-authored-by: Christian Gripp Co-authored-by: Vincent Langlet --- UPGRADE-4.x.md | 38 ++++++++++++++++++++ src/Admin/AbstractAdmin.php | 6 ++-- tests/Admin/AdminTest.php | 18 ++++++++++ tests/Fixtures/Admin/TagWithoutPostAdmin.php | 28 +++++++++++++++ 4 files changed, 88 insertions(+), 2 deletions(-) create mode 100644 UPGRADE-4.x.md create mode 100644 tests/Fixtures/Admin/TagWithoutPostAdmin.php diff --git a/UPGRADE-4.x.md b/UPGRADE-4.x.md new file mode 100644 index 0000000000..7e7cc537bd --- /dev/null +++ b/UPGRADE-4.x.md @@ -0,0 +1,38 @@ +UPGRADE 4.x +=========== + +UPGRADE FROM 4.x to 4.x +=========================== + +### appendParentObject is called inside createNewInstance with child admins + +In a child admin, if you were overriding `createNewInstance` and relying on sonata to provide the needed "parent" entity +to the instance, now you have to call `appendParentObject` manually. + +Before: +```php +final class PostAdmin extends AbstractAdmin +{ + public function createNewInstance(): object + { + return new Post(); + } +} +``` + +After: +```php + +final class PostAdmin extends AbstractAdmin +{ + public function createNewInstance(): object + { + $object = new Post(); + + // set the post author if the parent admin is "AuthorAdmin" + $this->appendParentObject($object); + + return $object; + } +} +``` diff --git a/src/Admin/AbstractAdmin.php b/src/Admin/AbstractAdmin.php index 9785894e73..5ac2230d0a 100644 --- a/src/Admin/AbstractAdmin.php +++ b/src/Admin/AbstractAdmin.php @@ -855,7 +855,6 @@ final public function getNewInstance(): object { $object = $this->createNewInstance(); - $this->appendParentObject($object); $this->alterNewInstance($object); foreach ($this->getExtensions() as $extension) { @@ -1845,7 +1844,10 @@ protected function configure(): void */ protected function createNewInstance(): object { - return Instantiator::instantiate($this->getClass()); + $object = Instantiator::instantiate($this->getClass()); + $this->appendParentObject($object); + + return $object; } /** diff --git a/tests/Admin/AdminTest.php b/tests/Admin/AdminTest.php index 7d6476cb00..2389ced91d 100644 --- a/tests/Admin/AdminTest.php +++ b/tests/Admin/AdminTest.php @@ -61,6 +61,7 @@ use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithCustomRouteAdmin; use Sonata\AdminBundle\Tests\Fixtures\Admin\PostWithoutBatchRouteAdmin; use Sonata\AdminBundle\Tests\Fixtures\Admin\TagAdmin; +use Sonata\AdminBundle\Tests\Fixtures\Admin\TagWithoutPostAdmin; use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\BlogPost; use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Comment; use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\CommentVote; @@ -1419,6 +1420,23 @@ public function testGetNewInstanceForChildAdminWithParentValue(): void static::assertSame($post, $tag->getPost()); } + public function testGetNewInstanceForChildAdminWithParentValueCanBeDisabled(): void + { + $postAdmin = $this->getMockBuilder(PostAdmin::class)->setConstructorArgs([ + 'post', + Post::class, + CRUDController::class, + ])->getMock(); + $postAdmin->expects(static::never())->method('getIdParameter'); + + $tagAdmin = new TagWithoutPostAdmin('admin.tag', Tag::class, 'MyBundle\MyController'); + $tagAdmin->setParent($postAdmin, 'post'); + + $tag = $tagAdmin->getNewInstance(); + + static::assertNull($tag->getPost()); + } + public function testGetNewInstanceForChildAdminWithCollectionParentValue(): void { $post = new Post(); diff --git a/tests/Fixtures/Admin/TagWithoutPostAdmin.php b/tests/Fixtures/Admin/TagWithoutPostAdmin.php new file mode 100644 index 0000000000..8a8859f340 --- /dev/null +++ b/tests/Fixtures/Admin/TagWithoutPostAdmin.php @@ -0,0 +1,28 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Sonata\AdminBundle\Tests\Fixtures\Admin; + +use Sonata\AdminBundle\Admin\AbstractAdmin; +use Sonata\AdminBundle\Tests\Fixtures\Bundle\Entity\Tag; + +/** + * @phpstan-extends AbstractAdmin + */ +final class TagWithoutPostAdmin extends AbstractAdmin +{ + protected function createNewInstance(): object + { + return new Tag(); + } +}