-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Introduce field description factory #6854
Introduce field description factory #6854
Conversation
* @method Pool getConfigurationPool() | ||
* @method void setRouteGenerator(RouteGeneratorInterface $routeGenerator) | ||
* @method RouteGeneratorInterface getRouteGenerator() | ||
* @method void initialize() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All of these method annotations look weird 🧐
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an idea for all these methods...
Can we add a new NextMajorTaggedAdminInterface
with all these methods and mark it as @internal
.
We can then use @mixin NextMajorTaggedAdminInterface
to include all these methods.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can rebase 3.x to fix the phpstan failures
It could be great to have this in 4.0 since it will solved the following issue too if I understand correctly: |
f52897b
to
8f3dfb1
Compare
@@ -3077,7 +3074,6 @@ protected function buildList() | |||
] | |||
); | |||
|
|||
$fieldDescription->setAdmin($this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to keep this, no ?
@@ -3103,7 +3098,6 @@ protected function buildList() | |||
] | |||
); | |||
|
|||
$fieldDescription->setAdmin($this); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
/** | ||
* @phpstan-param class-string $class | ||
*/ | ||
public function create(string $class, string $name, array $options = []): FieldDescriptionInterface; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use a more explicite name than $class
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could use modelClass
maybe, but I guess this is "sync" with AdminInterface::getClass
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some type hints for the $options
?
|
||
interface TypeGuesserInterface | ||
{ | ||
public function guess(FieldDescriptionInterface $fieldDescription): TypeGuess; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we create a TypeGuesserChain too ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure, I guess so, I'll take a look.
Yes, that is the idea, we could also remove all the |
I'll try to advance/finish it at the weekend, I have some doubts about how to enforce calling I thought about creating a final class FieldDescriptionCreator implements FieldDescriptionCreatorInterface
{
public function create(AdminInterface $admin, string $propertyName, array $options = []): FieldDescriptionInterface
{
$fieldDescriptionFactory = $admin->getFieldDescriptionFactory();
if (null === $fieldDescriptionFactory) {
$fieldDescription = $admin->getModelManager()->getNewFieldDescriptionInstance(
$this->getClass(),
$propertyName,
$options
);
} else {
$fieldDescription = $fieldDescriptionFactory->create($admin->getClass(), $propertyName, $options);
}
$fieldDescription->setAdmin($admin);
return $fieldDescription;
}
} This would work fine for Mappers, but maybe it's strange to inject this in Admins too. I'll give it some thought and check this current |
8f3dfb1
to
11fa21f
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is needed to finish this @franmomu ?
11fa21f
to
75e03fa
Compare
I think now some reviews to see if it's fine and then I'll create both PR for the persistence bundles. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
after this PR we should move
- FieldDescriptionCollection.php
- FieldDescriptionInterface.php
- FieldDescriptionRegistryInterface.php
To the new directory FieldDescription and deprecate the old classes
src/Admin/AbstractAdmin.php
Outdated
@@ -2892,6 +2890,26 @@ final public function hasTemplateRegistry(): bool | |||
return null !== $this->templateRegistry; | |||
} | |||
|
|||
public function createFieldDescription(string $propertyName, array $options = []): FieldDescriptionInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final?
/** | ||
* @phpstan-param class-string $class | ||
*/ | ||
public function create(string $class, string $name, array $options = []): FieldDescriptionInterface; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add some type hints for the $options
?
*/ | ||
private $guessers = []; | ||
|
||
public function __construct(array $guessers) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type hint missing
Can we use the factory to solve this issue too? sonata-project/SonataBlockBundle#244 |
Friendly ping @franmomu ; do you have some time to finish this ? :) |
hmmmm I guess that this code: // simulate an association ...
$fieldDescription = $this->getGalleryAdmin()->getModelManager()->getNewFieldDescriptionInstance($this->getGalleryAdmin()->getClass(), 'media', [
'translation_domain' => 'SonataMediaBundle',
]);
$fieldDescription->setAssociationAdmin($this->getGalleryAdmin());
$fieldDescription->setAdmin($formMapper->getAdmin());
$fieldDescription->setOption('edit', 'list');
$fieldDescription->setAssociationMapping(['fieldName' => 'gallery', 'type' => ClassMetadataInfo::MANY_TO_ONE]); Will become: $fieldDescription = $this->getGalleryAdmin()->createFieldDescription('media', [
'translation_domain' => 'SonataMediaBundle',
'edit' => 'list',
]);
// Not sure about this one, I think is not necessary
$fieldDescription->setAssociationAdmin($this->getGalleryAdmin()); But to do this: sonata-project/SonataBlockBundle#244 (comment) I guess we could create something like: SonataAdminBundle/src/Form/Extension/Field/Type/FormTypeFieldExtension.php Lines 75 to 87 in 4c97daf
And I guess we could call I'll address the suggestions later today. |
This interface will be responsible of creating FieldDescriptionInterface instances.
This will guess the field description type from its properties.
75e03fa
to
c1db7b6
Compare
This method will use the field description factory and also sets the current admin to the field description.
Deprecated in favor of Sonata\AdminBundle\FieldDescription\TypeGuesserInterface.
c1db7b6
to
e477c50
Compare
@@ -2892,6 +2890,26 @@ final public function hasTemplateRegistry(): bool | |||
return null !== $this->templateRegistry; | |||
} | |||
|
|||
final public function createFieldDescription(string $propertyName, array $options = []): FieldDescriptionInterface |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide type hints for the $options
?
* @method Pool getConfigurationPool() | ||
* @method void setRouteGenerator(RouteGeneratorInterface $routeGenerator) | ||
* @method RouteGeneratorInterface getRouteGenerator() | ||
* @method void initialize() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just an idea for all these methods...
Can we add a new NextMajorTaggedAdminInterface
with all these methods and mark it as @internal
.
We can then use @mixin NextMajorTaggedAdminInterface
to include all these methods.
Subject
For background: #6761 and #6701.
First of all, I had this half done, so I've "finished", I'm fine postponing this for
5.0
if we think this shouldn't be done now (and if this is fine ofc).The idea of this is basically to move responsibility of creating a
FieldDescription
fromModelManagerInterface
toFielDescriptionFactoryInterface
.And also there is a new
Sonata\AdminBundle\FieldDescription\TypeGuesserInterface
that will guess the FieldDescription type base on aFieldDescriptionInterface
properties instead of rely onModelManagerInterface
.I've created sonata-project/SonataDoctrineMongoDBAdminBundle#531 (unfinished) as example of implementation.
I am targeting this branch, because these changes are BC.
Closes #6761 and #6701.
Changelog