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

Improve error message in case of multiple admins for same entity #6486

Merged
merged 1 commit into from
Oct 20, 2020

Conversation

VincentLanglet
Copy link
Member

@VincentLanglet VincentLanglet commented Oct 15, 2020

Subject

This is BC

Closes #6181

Changelog

### Added
- Added `Pool::hasSingleAdminByClass()`

@@ -1487,6 +1489,18 @@ public function attachAdminClass(FieldDescriptionInterface $fieldDescription)

if (!$pool->hasAdminByClass($targetModel)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can't we use the hasAdminByClass method for this? IMHO the pool should now allow multiple admins for one entity

Copy link
Member Author

Choose a reason for hiding this comment

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

How would you do this @core23 ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Im my project we have about 10 admin classes for one entity.
Also im think that method getAdminByClass must be deprecated

Copy link
Member Author

Choose a reason for hiding this comment

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

Im my project we have about 10 admin classes for one entity.

That's not an issue. I have multiple classes for only one entity too.

Also im think that method getAdminByClass must be deprecated

The admin_code would be required in that case then. Don't know the implication.
And there is another usage of the adminByClass method.

I don't see a bad thing to automatically get an admin by Class, if possible.

Copy link
Contributor

@kirya-dev kirya-dev Oct 15, 2020

Choose a reason for hiding this comment

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

Im discover this method using for getting admin in twig function: sonata_urlsafeid (it seams too old method) and for attaching admins

Copy link
Contributor

Choose a reason for hiding this comment

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

So. getAdminByClass can be be removed. Use getAdminByCode

Copy link
Contributor

Choose a reason for hiding this comment

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

And attachAdminClass will throw own exception with suggest for define option admin_code.

Copy link
Member Author

@VincentLanglet VincentLanglet Oct 15, 2020

Choose a reason for hiding this comment

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

We could indeed deprecate getUrlSafeIdentifier in the TwigExtension. Do you mind creating the PR ?

But I'm against the deprecation of getAdminByClass, it's very useful when you only have one admin by class. In these case, there is no need to know, and set, the admin code for OtO or OtM relation between entities.

Copy link
Contributor

Choose a reason for hiding this comment

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

We should kept method getAdminClass as legacy?

Copy link
Contributor

Choose a reason for hiding this comment

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

For ex: (Instead of getAdminByClass)
image

@VincentLanglet VincentLanglet force-pushed the multipleAdmin branch 2 times, most recently from a6534bc to 49daf1a Compare October 15, 2020 20:59

if (!$pool->hasSingleAdminByClass($targetModel)) {
throw new \InvalidArgumentException(sprintf(
'To many admin found for the class "%s", please use the admin_code option instead',
Copy link
Contributor

Choose a reason for hiding this comment

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

You check on not single but says that too many( If someone delete method above here message logic will be broken.

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't understand, because the method above is not suppose to be deleted.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok

Copy link
Contributor

@kirya-dev kirya-dev left a comment

Choose a reason for hiding this comment

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

GetAdminByCode already has check on exists. You need add NEXT_MAGOR remove if block.

No nessary adds exception

@VincentLanglet
Copy link
Member Author

GetAdminByCode already has check on exists. You need add NEXT_MAGOR remove if block.

No necessary adds exception

It's not about adding exception. It's about having custom message more explicit in this method, to avoid issue like #6481

Unable to find a valid admin for the class: %s, there are too many registered: %s'
is less clear in this situation than the one I introduced.

@VincentLanglet VincentLanglet changed the title Improve error in case of multiple admins Improve error message in case of multiple admins for same entity Oct 15, 2020
@VincentLanglet VincentLanglet requested a review from a team October 15, 2020 21:40
@VincentLanglet VincentLanglet marked this pull request as ready for review October 15, 2020 21:41

if (!$pool->hasSingleAdminByClass($targetModel)) {
throw new \InvalidArgumentException(sprintf(
'Too many admins found for the class "%s", please use the admin_code option instead',
Copy link
Contributor

@greg0ire greg0ire Oct 17, 2020

Choose a reason for hiding this comment

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

We still don't require 7.3, but maybe we could start adopting multiline exception messages anyway

Suggested change
'Too many admins found for the class "%s", please use the admin_code option instead',
<<<'EXCEPTION'
Context: trying to find an admin for class "%s"
Problem: That class has more than one admin.
Solution: Use the unambiguous admin_code option instead.
EXCEPTION,

Copy link
Member Author

Choose a reason for hiding this comment

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

Won't it lead to weird display on 7.2 ?

I would prefer to do this change in a more general way on our code ? Maybe even with an helper to generate an exception message ?

public static function formatExceptionMessage($context, $problem, $solution)

@VincentLanglet VincentLanglet merged commit f62832c into sonata-project:3.x Oct 20, 2020
@VincentLanglet VincentLanglet deleted the multipleAdmin branch October 20, 2020 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add child object
5 participants