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

Allow replace AbstractAdminBlockService by EditableBlockService #764

Closed
Closed
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
69 changes: 62 additions & 7 deletions src/Block/Service/AbstractAdminBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@

namespace Sonata\BlockBundle\Block\Service;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\AdminBundle\Form\FormMapper as AdminFormMapper;
use Sonata\BlockBundle\Form\Mapper\BlockFormMapper;
use Sonata\BlockBundle\Form\Mapper\FormMapper;
use Sonata\BlockBundle\Meta\Metadata;
use Sonata\BlockBundle\Meta\MetadataInterface;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\Form\Validator\ErrorElement;

Expand All @@ -31,45 +34,97 @@
*/
abstract class AbstractAdminBlockService extends AbstractBlockService implements AdminBlockServiceInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class is already deprecated, no need to change anything right here

{
public function buildCreateForm(FormMapper $formMapper, BlockInterface $block)
{
$this->buildEditForm($formMapper, $block);
}

/**
* @deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.
*/
public function prePersist(BlockInterface $block)
{
}

/**
* @deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.
*/
public function postPersist(BlockInterface $block)
{
}

/**
* @deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.
*/
public function preUpdate(BlockInterface $block)
{
}

/**
* @deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.
*/
public function postUpdate(BlockInterface $block)
{
}

/**
* @deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.
*/
public function preRemove(BlockInterface $block)
{
}

/**
* @deprecated since sonata-project/block-bundle 3.12.0 and will be removed in version 4.0.
*/
public function postRemove(BlockInterface $block)
{
}

public function buildEditForm(FormMapper $form, BlockInterface $block)
/**
* @deprecated since sonata-project/block-bundle 3.x. Use "configureCreateForm()" instead.
*/
public function buildCreateForm(AdminFormMapper $formMapper, BlockInterface $block)
{
$blockFormMapper = new BlockFormMapper($formMapper);
$this->configureCreateForm($blockFormMapper, $block);
}

/**
* @deprecated since sonata-project/block-bundle 3.x. Use "configureEditForm()" instead.
*/
public function buildEditForm(AdminFormMapper $formMapper, BlockInterface $block)
{
$blockFormMapper = new BlockFormMapper($formMapper);
$this->configureEditForm($blockFormMapper, $block);
}

/**
* @deprecated since sonata-project/block-bundle 3.x. Use "validate()" instead.
*/
public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
{
$this->validate($errorElement, $block);
}

/**
* @deprecated since sonata-project/block-bundle 3.x. Use "getMetadata()" instead.
*/
public function getBlockMetadata($code = null)
{
return new Metadata($this->getName(), (null !== $code ? $code : $this->getName()), false, 'SonataBlockBundle', ['class' => 'fa fa-file']);
}

public function configureEditForm(FormMapper $form, BlockInterface $block)
{
}

public function configureCreateForm(FormMapper $form, BlockInterface $block)
{
$this->configureEditForm($form, $block);
}

public function validate(ErrorElement $errorElement, BlockInterface $block)
{
}

public function getMetadata(): MetadataInterface
{
return new Metadata($this->getName(), $this->getName(), false, 'SonataBlockBundle', ['class' => 'fa fa-file']);
}
}
80 changes: 80 additions & 0 deletions src/Form/Mapper/BlockFormMapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Sonata\BlockBundle\Form\Mapper;

use Sonata\AdminBundle\Form\FormMapper as AdminFormMapper;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should not need any AdminBundle reference. We decoupled this bundle from the admin bundle on the 4.x branch

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is use only when user generate form for Block in Admin site.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would still need to require sonataAdmin on master https://github.com/sonata-project/SonataBlockBundle/blob/master/composer.json

The right way should be to add this code on sonataAdmin instead, if necessary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it could be directly added to the PageBundle then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it could be directly added to the PageBundle then

Already done ;)

use Symfony\Component\Form\FormBuilderInterface;

final class BlockFormMapper implements FormMapper
{
/**
* @var AdminFormMapper
*/
private $adminFormMapper;

public function __construct(AdminFormMapper $adminFormMapper)
{
$this->adminFormMapper = $adminFormMapper;
}

public function create(string $name, ?string $type = null, array $options = []): FormBuilderInterface
{
return $this->adminFormMapper->create($name, $type, $options);
}

public function reorder(array $keys): FormMapper
{
$this->adminFormMapper->reorder($keys);

return $this;
}

public function add($name, ?string $type = null, array $options = []): FormMapper
{
$this->adminFormMapper->add($name, $type, $options);

return $this;
}

public function remove(string $key): FormMapper
{
$this->adminFormMapper->remove($key);

return $this;
}

public function setHelps(array $helps = []): FormMapper
{
$this->adminFormMapper->setHelps($helps);

return $this;
}

public function addHelp(string $name, string $help): FormMapper
{
$this->adminFormMapper->addHelp($name, $help);

return $this;
}

public function has(string $key): bool
{
return $this->adminFormMapper->has($key);
}

public function get(string $name)
{
return $this->adminFormMapper->get($name);
}
}