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

Prepare for BlockBundle 4.0 support and fix #1731 #1732

Closed
wants to merge 16 commits into from
Closed

Prepare for BlockBundle 4.0 support and fix #1731 #1732

wants to merge 16 commits into from

Conversation

haivala
Copy link
Contributor

@haivala haivala commented Apr 6, 2020

Subject

I am targeting this branch, because it is patch.

Closes #1731

Changelog

### Added
- prepare for BlockBundle 4.0 Support with `EditableBlockService`
### Fixed
- Added `GalleryBlockService::buildCreateForm` and `GalleryBlockService::validateBlock` to fix compatibility with PageBundle
- Added `GalleryListBlockService::buildCreateForm` and `GalleryListBlockService::validateBlock` to fix compatibility with PageBundle
- Added `MediaBlockService::buildCreateForm` and `MediaBlockService::validateBlock` to fix compatibility with PageBundle

src/Block/GalleryBlockService.php Outdated Show resolved Hide resolved
core23
core23 previously approved these changes Apr 6, 2020
@core23 core23 added the patch label Apr 6, 2020
@core23 core23 requested a review from franmomu April 6, 2020 11:09
phansys
phansys previously approved these changes Apr 6, 2020
Copy link
Member

@phansys phansys left a comment

Choose a reason for hiding this comment

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

I guess these extension points are intended to be kept in the next major.

@haivala
Copy link
Contributor Author

haivala commented Apr 6, 2020

This will fix only display Block in 3.x version. To full bugfix please do:

* copy methods from `Sonata/BlockBundle/Block/Service/AbstractAdminBlockService`

* add deprecation to them like AbstractAdmin.. have

* add `implements EditableBlockServie` and connect news methods with existing

I'll check this later

@haivala
Copy link
Contributor Author

haivala commented Apr 6, 2020

With these changes I can now add block in the Page composer but it still needs the validate method to be able to create the block.
Is it good if I add?

use Sonata\Form\Validator\ErrorElement;
...
    public function validateBlock(ErrorElement $errorElement, BlockInterface $block)
    {
    }

I tested and after that I can create the block.
Should I add that to this pull request?

@haivala haivala dismissed stale reviews from phansys and core23 via 4be6b0f April 6, 2020 14:54
@haivala haivala requested review from core23 and phansys April 6, 2020 14:55
core23
core23 previously approved these changes Apr 6, 2020
@phansys
Copy link
Member

phansys commented Apr 7, 2020

Is not clear for me if these methods are added back just to fix a BC break (and must be deprecated), or if they are intended to be kept in the next major. Could you please confirm that?

@wbloszyk
Copy link
Member

wbloszyk commented Apr 7, 2020

This commit make bug: 9533247 becouse AbstractAdminBlockService without add EditableBlockService
Look at: SonataPageBundle/blob/3.x/src/Admin/BlockAdmin.php#L217-L238

To fix it and keep BC-break all methods from AbstractAdminBlockService must be implemented and mark as depecated and NEXT_MAJOR: delete it to keep BlockBundle 3 support.

To add BlockBundle 4 support EditableBlockService must be implemented.

@phansys
Copy link
Member

phansys commented Apr 7, 2020

Thank you for the answer.

To fix it and keep BC-break all methods from AbstractAdminBlockService must be implemented and mark as depecated and NEXT_MAJOR: delete it to keep BlockBundle 3 support.

To add BlockBundle 4 support EditableBlockService must be implemented.

IIUC, I think we should add a NEXT_MAJOR comments, telling these methods must be removed and the interface EditableBlockService must be implemented here:

/**
* @final since sonata-project/media-bundle 3.21.0
*
* @author Thomas Rabaix <[email protected]>
*/
class GalleryBlockService extends AbstractBlockService

@haivala haivala changed the title fix #1731 WIP: fix #1731 Apr 8, 2020
@haivala
Copy link
Contributor Author

haivala commented Apr 8, 2020

How do I solve this: "Attempted to call an undefined method named "getAdmin" of class "Sonata\PageBundle\Mapper\PageFormMapper"."
If comes from:

$fieldDescription->setAdmin($formMapper->getAdmin());

@haivala haivala requested a review from core23 April 8, 2020 07:06
@haivala
Copy link
Contributor Author

haivala commented Apr 8, 2020

And is it ok to remove the $code variable in validate method like I have done?

@haivala haivala changed the title WIP: fix #1731 WIP: Add BlockBundle 4.0 support and fix #1731 Apr 8, 2020
@haivala
Copy link
Contributor Author

haivala commented Apr 8, 2020

If this goes smoothly: now that I have experience on this I can consider adding BlockBundle 4 support other SonataBundle blocks too.

@haivala
Copy link
Contributor Author

haivala commented Apr 8, 2020

Can I have comment on this? @wbloszyk or @phansys ?

src/Block/GalleryBlockService.php Outdated Show resolved Hide resolved
src/Block/GalleryBlockService.php Show resolved Hide resolved
@haivala
Copy link
Contributor Author

haivala commented Apr 8, 2020

How do I solve this: "Attempted to call an undefined method named "getAdmin" of class "Sonata\PageBundle\Mapper\PageFormMapper"."
If comes from:

$fieldDescription->setAdmin($formMapper->getAdmin());

@phansys this is the real problem

@phansys
Copy link
Member

phansys commented Apr 8, 2020

How do I solve this: "Attempted to call an undefined method named "getAdmin" of class "Sonata\PageBundle\Mapper\PageFormMapper"."

I didn't dive in this issue, but maybe you could use a class property in order to store the required admin instance. Otherwise, you should implement a solution based on func_get_args() in order to keep the method signature while allowing it to receive an extra argument. What I really don't know is who is responsible to provide that admin instance.

@haivala
Copy link
Contributor Author

haivala commented Apr 8, 2020

How do I solve this: "Attempted to call an undefined method named "getAdmin" of class "Sonata\PageBundle\Mapper\PageFormMapper"."

I didn't dive in this issue, but maybe you could use a class property in order to store the required admin instance. Otherwise, you should implement a solution based on func_get_args() in order to keep the method signature while allowing it to receive an extra argument. What I really don't know is who is responsible to provide that admin instance.

How I understand this is that we just need to provide the current admin (sonata.page.admin.block in this case) to it so that it can create the virtual ManyToOne relation in ImmutableArrayType form. The block admin saves the id of an Media as int in the block settings and loads the object when needed from media admin but we need to create that that form to be able to set the int (and use media admin to do so). This has been the implementation for this more than 5 years and I have feeling that it has always been considered as hacky.

My question is can I just get the sonata.page.admin.block from the container in that line? Does it beak something else? I tested this and it works as it should. like this:
$fieldDescription->setAdmin($this->container->get('sonata.page.admin.block'));

@haivala haivala requested a review from phansys April 9, 2020 05:42
public function getBlockMetadata($code = null)
{
if ('sonata_deprecation_mute' !== (\func_get_args()[2] ?? null)) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Does this 2 need to be changed to 1?

Copy link
Member

Choose a reason for hiding this comment

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

It depends on how are you providing the arguments for this method, but since there is no magic usage for argument 2 (offset 1 for func_get_args()), I think you must use it.

src/Block/MediaBlockService.php Show resolved Hide resolved
@haivala haivala changed the title WIP: Add BlockBundle 4.0 support and fix #1731 Add BlockBundle 4.0 support and fix #1731 Apr 9, 2020
@haivala
Copy link
Contributor Author

haivala commented Apr 9, 2020

@phansys waiting for review

'class' => 'fa fa-picture-o',
]);
}

/**
* NEXT_MAJOR: delete it.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
* NEXT_MAJOR: delete it.
* NEXT_MAJOR: delete it.
*
* @deprecated Will be removed in version 4.0. Use getMetadata instead.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It triggers deprecation notice already..

@@ -13,7 +13,7 @@

namespace Sonata\MediaBundle\Block;

use Sonata\AdminBundle\Form\FormMapper;
use Sonata\BlockBundle\Form\Mapper\FormMapper;
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
use Sonata\BlockBundle\Form\Mapper\FormMapper;
use Sonata\AdminBundle\Form\Mapper\FormMapper as AdminFormMapper;
use Sonata\BlockBundle\Form\Mapper\FormMapper;

Use AdminFormMapper in buildCreateForm and buildEditForm
Use FormMapper in configureCreateForm and configureEditForm

This can make some problem, I will check it.

@@ -140,7 +145,7 @@ public function buildEditForm(FormMapper $formMapper, BlockInterface $block)
'translation_domain' => 'SonataMediaBundle',
]);
$fieldDescription->setAssociationAdmin($this->getGalleryAdmin());
$fieldDescription->setAdmin($formMapper->getAdmin());
$fieldDescription->setAdmin($this->container->get('sonata.page.admin.block'));
Copy link
Member

Choose a reason for hiding this comment

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

It is OK for old Symfony practice.

For new pratice we should avoid using container. Please move it this service to block property and set it in constructor. This will allow inject required servaices insted container.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think the container should be removed in next major not in this patch. The container is used else where in the blocks too for example when we are getting the media admin. Can we have another pull request for that?

Copy link
Member

Choose a reason for hiding this comment

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

Container should be remove in Block 4 support becouse you must change constructor parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well this can be changed to be "prepare for Block 4 support".
I would happily do this later.
This was supposed to be patch for these blocks to work at all and now they work.
OK?

@haivala haivala changed the title Add BlockBundle 4.0 support and fix #1731 Prepare for BlockBundle 4.0 support and fix #1731 Apr 9, 2020
static::class
), E_USER_DEPRECATED);
}
$this->configureEditForm($formMapper, $block);
Copy link
Member

Choose a reason for hiding this comment

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

FormMapper are not the same:

        public function buildEditForm(Sonata\AdminBundle\Form\FormMapper $form, BlockInterface $block)
    public function configureEditForm(Sonata\BlockBundle\Form\Mapper\FormMapper $form, BlockInterface $block)

You should use something like PageFormMapper:

$blockMapper = new PageFormMapper($formMapper);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sonata\BlockBundle\Form\Mapper\FormMapper has been introduced in block bundle 3.13
And this requires 3.17: https://github.com/sonata-project/SonataMediaBundle/blob/3.x/composer.json#L66
block bundles formmapper uses the same adminbundle formmapper without some functions.
Is this really needed?

Copy link
Member

@franmomu franmomu Apr 10, 2020

Choose a reason for hiding this comment

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

The thing is that when I introduced the BC break in #1699, it was extending AbstractAdminBlockService, so to be compatible again, we should just copy those methods that don't exist in these classes, we cannot change the signature of those methods. IMHO the compatibility with BlockBundle 4 should be done in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yet, somehow these blocks work with my changes. they do not need all of those methods.

@haivala
Copy link
Contributor Author

haivala commented Apr 10, 2020

This is really frustrating.

@wbloszyk
Copy link
Member

@haivala Change this PR to fix only. Focus on:

  • remove BlockBundle 4 from composer
  • restore methods from AbstractAdminBlockService
  • add deprecation notes

In another we can focus on BlockBundle 4 support:

  • add EditableBlockService

@core23
Copy link
Member

core23 commented May 3, 2020

Any progress here? Should we revert the change?

@wbloszyk
Copy link
Member

wbloszyk commented May 3, 2020

I think revert 9533247 PR will be the best option now.

This PR will be resolve in #700

@wbloszyk wbloszyk mentioned this pull request May 5, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
6 participants