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 to Sonata 4 #1485

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@
"cocur/slugify": "^4.0",
"doctrine/doctrine-bundle": "^2.5",
"doctrine/persistence": "^2.1",
"sonata-project/admin-bundle": "^3.99",
"sonata-project/admin-bundle": "^4.15",
"sonata-project/block-bundle": "^4.11",
"sonata-project/doctrine-extensions": "^1.8",
"sonata-project/doctrine-orm-admin-bundle": "^3.19",
"sonata-project/doctrine-orm-admin-bundle": "^4.0",
"sonata-project/form-extensions": "^1.4",
"sonata-project/seo-bundle": "^3.0",
"sonata-project/twig-extensions": "^1.3",
Expand Down
122 changes: 22 additions & 100 deletions src/Admin/BaseBlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\BlockBundle\Block\BlockServiceManagerInterface;
use Sonata\PageBundle\Model\PageBlockInterface;
use Sonata\PageBundle\Model\PageInterface;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Abstract admin class for the Block model.
*
* @extends AbstractAdmin<PageBlockInterface>
*
* @author Thomas Rabaix <[email protected]>
Expand All @@ -48,129 +46,53 @@ abstract class BaseBlockAdmin extends AbstractAdmin
*/
protected $containerBlockTypes = [];

public function preUpdate($object): void
public function setBlockManager(BlockServiceManagerInterface $blockManager): void
{
$block = $this->blockManager->get($object);

if (\is_callable([$block, 'preUpdate'])) {
$block->preUpdate($object);

@trigger_error(
'The '.__METHOD__.'() method is deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.',
\E_USER_DEPRECATED
);
}

if ($object->getPage() instanceof PageInterface) {
$object->getPage()->setEdited(true);
}
$this->blockManager = $blockManager;
}

public function postUpdate($object): void
public function setContainerBlockTypes(array $containerBlockTypes): void
{
$block = $this->blockManager->get($object);

if (\is_callable([$block, 'postUpdate'])) {
$block->postUpdate($object);

@trigger_error(
'The '.__METHOD__.'() method is deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.',
\E_USER_DEPRECATED
);
}
$this->containerBlockTypes = $containerBlockTypes;
}

public function prePersist($object): void
public function preBatchAction(string $actionName, ProxyQueryInterface $query, array &$idx, bool $allElements = false): void
{
$block = $this->blockManager->get($object);

if (\is_callable([$block, 'prePersist'])) {
$block->prePersist($object);
if ($this->isChild() && 'delete' === $actionName) {
$parent = $this->getParent();
$subject = $parent->getSubject();

@trigger_error(
'The '.__METHOD__.'() method is deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.',
\E_USER_DEPRECATED
);
if ($subject instanceof PageInterface) {
$subject->setEdited(true);
}
}

parent::preBatchAction($actionName, $query, $idx, $allElements);
}

protected function preUpdate(object $object): void
{
if ($object->getPage() instanceof PageInterface) {
$object->getPage()->setEdited(true);
}
}

public function postPersist($object): void
protected function prePersist(object $object): void
{
$block = $this->blockManager->get($object);

if (\is_callable([$block, 'postPersist'])) {
$block->postPersist($object);

@trigger_error(
'The '.__METHOD__.'() method is deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.',
\E_USER_DEPRECATED
);
if ($object->getPage() instanceof PageInterface) {
$object->getPage()->setEdited(true);
}
}

public function preRemove($object): void
protected function preRemove(object $object): void
{
$block = $this->blockManager->get($object);

if (\is_callable([$block, 'preRemove'])) {
$block->preRemove($object);

@trigger_error(
'The '.__METHOD__.'() method is deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.',
\E_USER_DEPRECATED
);
}

$page = $object->getPage();

if ($page instanceof PageInterface) {
$page->setEdited(true);
}
}

public function postRemove($object): void
{
$block = $this->blockManager->get($object);

if (\is_callable([$block, 'postRemove'])) {
$block->postRemove($object);

@trigger_error(
'The '.__METHOD__.'() method is deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.',
\E_USER_DEPRECATED
);
}
}

public function setBlockManager(BlockServiceManagerInterface $blockManager): void
{
$this->blockManager = $blockManager;
}

public function setContainerBlockTypes(array $containerBlockTypes): void
{
$this->containerBlockTypes = $containerBlockTypes;
}

public function preBatchAction($actionName, ProxyQueryInterface $query, array &$idx, $allElements): void
{
$parent = $this->getParent();

if ($parent && 'delete' === $actionName) {
$subject = $parent->getSubject();

if ($subject instanceof PageInterface) {
$subject->setEdited(true);
}
}

parent::preBatchAction($actionName, $query, $idx, $allElements);
}

protected function alterObject(object $object): void
{
$this->loadBlockDefaults($object);
Expand All @@ -194,7 +116,7 @@ protected function configurePersistentParameters(): array
];
}

protected function configureRoutes(RouteCollection $collection): void
protected function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->add('view', $this->getRouterIdParameter().'/view');
}
Expand Down
21 changes: 6 additions & 15 deletions src/Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

use Doctrine\ORM\EntityRepository;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\BlockBundle\Block\Service\BlockServiceInterface;
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Form\Type\ServiceListType;
Expand All @@ -29,8 +29,6 @@
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* Admin class for the Block model.
*
* @author Thomas Rabaix <[email protected]>
*/
final class BlockAdmin extends BaseBlockAdmin
Expand All @@ -39,21 +37,14 @@ final class BlockAdmin extends BaseBlockAdmin

protected $classnameLabel = 'Block';

protected $accessMapping = [
protected array $accessMapping = [
'savePosition' => 'EDIT',
'switchParent' => 'EDIT',
'composePreview' => 'EDIT',
];

/**
* @param string $code
* @param string $class
* @param string $baseControllerName
*/
public function __construct($code, $class, $baseControllerName, array $blocks = [])
public function __construct(array $blocks = [])
{
parent::__construct($code, $class, $baseControllerName);

$this->blocks = $blocks;
}

Expand All @@ -72,7 +63,7 @@ protected function configurePersistentParameters(): array
return $parameters;
}

protected function configureRoutes(RouteCollection $collection): void
protected function configureRoutes(RouteCollectionInterface $collection): void
{
parent::configureRoutes($collection);

Expand All @@ -93,15 +84,15 @@ protected function configureFormFields(FormMapper $form): void

$page = false;

if ($this->getParent()) {
if ($this->isChild()) {
$page = $this->getParent()->getSubject();

if (!$page instanceof PageInterface) {
throw new \RuntimeException('The BlockAdmin must be attached to a parent PageAdmin');
}

if ($this->hasRequest() && null === $block->getId()) { // new block
$block->setType($this->request->get('type'));
$block->setType($this->getRequest()->get('type'));
$block->setPage($page);
}

Expand Down
12 changes: 6 additions & 6 deletions src/Admin/Extension/CreateSnapshotAdminExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

use Sonata\AdminBundle\Admin\AbstractAdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\PageBundle\Model\PageBlockInterface;
use Sonata\PageBundle\Model\PageInterface;
use Sonata\PageBundle\Service\Contract\CreateSnapshotByPageInterface;

/**
* @extends AbstractAdminExtension<BlockInterface|PageInterface>
* @extends AbstractAdminExtension<PageBlockInterface|PageInterface>
*/
final class CreateSnapshotAdminExtension extends AbstractAdminExtension
{
Expand All @@ -31,24 +31,24 @@ public function __construct(CreateSnapshotByPageInterface $createSnapshotByPage)
$this->createSnapshotByPage = $createSnapshotByPage;
}

public function postUpdate(AdminInterface $admin, $object): void
public function postUpdate(AdminInterface $admin, object $object): void
{
$this->createByPage($object);
}

public function postPersist(AdminInterface $admin, $object): void
public function postPersist(AdminInterface $admin, object $object): void
{
$this->createByPage($object);
}

public function postRemove(AdminInterface $admin, $object): void
public function postRemove(AdminInterface $admin, object $object): void
{
$this->createByPage($object);
}

private function createByPage(object $object): void
{
if ($object instanceof BlockInterface && method_exists($object, 'getPage')) {
if ($object instanceof PageBlockInterface) {
$page = $object->getPage();
} elseif ($object instanceof PageInterface) {
$page = $object;
Expand Down
28 changes: 12 additions & 16 deletions src/Admin/PageAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@

namespace Sonata\PageBundle\Admin;

use Knp\Menu\ItemInterface as MenuItemInterface;
use Knp\Menu\ItemInterface;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Route\RouteCollection;
use Sonata\AdminBundle\Route\RouteCollectionInterface;
use Sonata\AdminBundle\Show\ShowMapper;
use Sonata\DoctrineORMAdminBundle\Filter\CallbackFilter;
use Sonata\PageBundle\Exception\InternalErrorException;
Expand All @@ -36,8 +36,6 @@
use Symfony\Component\Form\Extension\Core\Type\TextType;

/**
* Admin definition for the Page class.
*
* @extends AbstractAdmin<PageInterface>
*
* @author Thomas Rabaix <[email protected]>
Expand All @@ -50,12 +48,12 @@ final class PageAdmin extends AbstractAdmin

protected ?SiteManagerInterface $siteManager = null;

protected $accessMapping = [
protected array $accessMapping = [
'tree' => 'LIST',
'compose' => 'EDIT',
];

public function configureRoutes(RouteCollection $collection): void
public function configureRoutes(RouteCollectionInterface $collection): void
{
$collection->add('compose', '{id}/compose', [
'id' => null,
Expand Down Expand Up @@ -128,7 +126,7 @@ public function getSites()
return $this->siteManager->findBy([]);
}

protected function configureBatchActions($actions): array
protected function configureBatchActions(array $actions): array
{
$actions = parent::configureBatchActions($actions);

Expand Down Expand Up @@ -179,11 +177,13 @@ protected function configurePersistentParameters(): array
return $parameters;
}

if ($site = $this->request->get('site', null)) {
$this->request->getSession()->set($key, $site);
$request = $this->getRequest();

if ($site = $request->get('site', null)) {
$request->getSession()->set($key, $site);
}

if ($site = $this->request->getSession()->get($key, null)) {
if ($site = $request->getSession()->get($key, null)) {
$parameters['site'] = $site;
}

Expand Down Expand Up @@ -274,7 +274,7 @@ protected function configureFormFields(FormMapper $form): void

$form
->with('form_page.group_main_label')
->add('name')
->add('name', null, ['help' => 'help_page_name'])
->add('enabled', null, ['required' => false])
->add('position')
->end();
Expand Down Expand Up @@ -358,13 +358,9 @@ protected function configureFormFields(FormMapper $form): void
->add('stylesheet', null, ['required' => false])
->add('rawHeaders', null, ['required' => false])
->end();

$form->setHelps([
'name' => 'help_page_name',
jordisala1991 marked this conversation as resolved.
Show resolved Hide resolved
]);
}

protected function configureTabMenu(MenuItemInterface $menu, $action, ?AdminInterface $childAdmin = null): void
protected function configureTabMenu(ItemInterface $menu, string $action, ?AdminInterface $childAdmin = null): void
{
if (!$childAdmin && !\in_array($action, ['edit'], true)) {
return;
Expand Down
Loading