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

Add more CRUD form tests #1552

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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"dama/doctrine-test-bundle": "^6.7",
"doctrine/annotations": "^1.13.3",
"friendsofphp/php-cs-fixer": "^3.4",
"masterminds/html5": "^2.7",
"matthiasnoback/symfony-dependency-injection-test": "^4.1.1",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "^1.0",
Expand Down
7 changes: 2 additions & 5 deletions src/Admin/BlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Form\Type\ServiceListType;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\PageBundle\Mapper\PageFormMapper;
use Sonata\PageBundle\Model\PageBlockInterface;
use Sonata\PageBundle\Model\PageInterface;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
Expand Down Expand Up @@ -232,12 +231,10 @@ private function configureBlockFields(FormMapper $form, BlockInterface $block):
));
}

$blockMapper = new PageFormMapper($form);

if ($block->getId() > 0) {
$service->configureEditForm($blockMapper, $block);
$service->configureEditForm($form, $block);
} else {
$service->configureCreateForm($blockMapper, $block);
$service->configureCreateForm($form, $block);
}

if ($form->has('settings') && isset($this->blocks[$blockType]['templates'])) {
Expand Down
7 changes: 2 additions & 5 deletions src/Admin/SharedBlockAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
use Sonata\BlockBundle\Block\Service\EditableBlockService;
use Sonata\BlockBundle\Model\BlockInterface;
use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
use Sonata\PageBundle\Mapper\PageFormMapper;
use Sonata\PageBundle\Model\PageBlockInterface;

/**
Expand Down Expand Up @@ -103,12 +102,10 @@ private function configureBlockFields(FormMapper $form, BlockInterface $block):
));
}

$blockMapper = new PageFormMapper($form);

if ($block->getId() > 0) {
$service->configureEditForm($blockMapper, $block);
$service->configureEditForm($form, $block);
} else {
$service->configureCreateForm($blockMapper, $block);
$service->configureCreateForm($form, $block);
}
}
}
14 changes: 0 additions & 14 deletions src/Block/ChildrenPagesBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,6 @@ public function configureSettings(OptionsResolver $resolver): void
]);
}

public function prePersist(BlockInterface $block): void
{
$page = $block->getSetting('pageId');

$block->setSetting('pageId', $page instanceof PageInterface ? $page->getId() : null);
}

public function preUpdate(BlockInterface $block): void
{
$page = $block->getSetting('pageId');

$block->setSetting('pageId', $page instanceof PageInterface ? $page->getId() : null);
}

public function load(BlockInterface $block): void
{
if (!$block instanceof PageBlockInterface) {
Expand Down
54 changes: 21 additions & 33 deletions src/Block/SharedBlockBlockService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace Sonata\PageBundle\Block;

use Sonata\AdminBundle\Admin\AdminInterface;
use Sonata\AdminBundle\Form\FormMapper as AdminFormMapper;
use Sonata\AdminBundle\Form\Type\ModelListType;
use Sonata\BlockBundle\Block\BlockContextInterface;
use Sonata\BlockBundle\Block\Service\AbstractBlockService;
Expand Down Expand Up @@ -42,24 +42,16 @@ final class SharedBlockBlockService extends AbstractBlockService implements Edit
*/
private ManagerInterface $blockManager;

/**
* @var AdminInterface<PageBlockInterface>
*/
private AdminInterface $sharedBlockAdmin;

/**
* @param ManagerInterface<PageBlockInterface> $blockManager
* @param AdminInterface<PageBlockInterface> $sharedBlockAdmin
*/
public function __construct(
Environment $twig,
ManagerInterface $blockManager,
AdminInterface $sharedBlockAdmin
ManagerInterface $blockManager
) {
parent::__construct($twig);

$this->blockManager = $blockManager;
$this->sharedBlockAdmin = $sharedBlockAdmin;
}

public function execute(BlockContextInterface $blockContext, ?Response $response = null): Response
Expand All @@ -76,10 +68,10 @@ public function execute(BlockContextInterface $blockContext, ?Response $response
\assert(null !== $template);

return $this->renderResponse($template, [
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
'sharedBlock' => $sharedBlock,
], $response);
'block' => $blockContext->getBlock(),
'settings' => $blockContext->getSettings(),
'sharedBlock' => $sharedBlock,
], $response);
}

public function validate(ErrorElement $errorElement, BlockInterface $block): void
Expand All @@ -92,13 +84,17 @@ public function validate(ErrorElement $errorElement, BlockInterface $block): voi

public function configureEditForm(FormMapper $form, BlockInterface $block): void
{
if (!$form instanceof AdminFormMapper) {
throw new \InvalidArgumentException('Shared Block requires to be used in the Admin context');
}

if (!$block->getSetting('blockId') instanceof BlockInterface) {
$this->load($block);
}

$form->add('settings', ImmutableArrayType::class, [
'keys' => [
[$this->getBlockBuilder(), null, []],
[$this->getBlockBuilder($form), null, []],
],
]);
}
Expand Down Expand Up @@ -134,31 +130,23 @@ public function load(BlockInterface $block): void
$block->setSetting('blockId', $sharedBlock);
}

public function prePersist(BlockInterface $block): void
{
$block = $block->getSetting('blockId');

$block->setSetting('blockId', $block instanceof BlockInterface ? $block->getId() : null);
}

public function preUpdate(BlockInterface $block): void
/**
* @param AdminFormMapper<object> $form
*/
private function getBlockBuilder(AdminFormMapper $form): FormBuilderInterface
{
$block = $block->getSetting('blockId');
$admin = $form->getAdmin();

$block->setSetting('blockId', $block instanceof BlockInterface ? $block->getId() : null);
}

private function getBlockBuilder(): FormBuilderInterface
{
$fieldDescription = $this->sharedBlockAdmin->createFieldDescription('block', [
$fieldDescription = $admin->createFieldDescription('block', [
'translation_domain' => 'SonataPageBundle',
'edit' => 'list',
]);
$fieldDescription->setAssociationAdmin($admin);

return $this->sharedBlockAdmin->getFormBuilder()->create('blockId', ModelListType::class, [
return $form->getFormBuilder()->create('blockId', ModelListType::class, [
'sonata_field_description' => $fieldDescription,
'class' => $this->sharedBlockAdmin->getClass(),
'model_manager' => $this->sharedBlockAdmin->getModelManager(),
'class' => $admin->getClass(),
'model_manager' => $admin->getModelManager(),
'label' => 'form.label_block',
'required' => false,
]);
Expand Down
73 changes: 0 additions & 73 deletions src/Mapper/PageFormMapper.php

This file was deleted.

1 change: 0 additions & 1 deletion src/Resources/config/block.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
->args([
new ReferenceConfigurator('twig'),
new ReferenceConfigurator('sonata.page.manager.block'),
new ReferenceConfigurator('sonata.page.admin.shared_block'),
])

->set('sonata.page.block.pagelist', PageListBlockService::class)
Expand Down
Loading