Skip to content

Commit

Permalink
Move appendParentObject into createNewInstance (#7549)
Browse files Browse the repository at this point in the history
* 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 <VincentLanglet@users.noreply.github.com>

Co-authored-by: Christian Gripp <core23@users.noreply.github.com>
Co-authored-by: Vincent Langlet <VincentLanglet@users.noreply.github.com>
  • Loading branch information
3 people committed Oct 27, 2021
1 parent 3d0cbd1 commit 2f6213e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 2 deletions.
38 changes: 38 additions & 0 deletions UPGRADE-4.x.md
Original file line number Diff line number Diff line change
@@ -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;
}
}
```
6 changes: 4 additions & 2 deletions src/Admin/AbstractAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,6 @@ final public function getNewInstance(): object
{
$object = $this->createNewInstance();

$this->appendParentObject($object);
$this->alterNewInstance($object);

foreach ($this->getExtensions() as $extension) {
Expand Down Expand Up @@ -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;
}

/**
Expand Down
18 changes: 18 additions & 0 deletions tests/Admin/AdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
28 changes: 28 additions & 0 deletions tests/Fixtures/Admin/TagWithoutPostAdmin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* 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<Tag>
*/
final class TagWithoutPostAdmin extends AbstractAdmin
{
protected function createNewInstance(): object
{
return new Tag();
}
}

0 comments on commit 2f6213e

Please sign in to comment.