-
-
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
Changes from all commits
e60ebfe
218e433
5274f8f
b0af0a7
cb38c97
e477c50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -746,10 +746,8 @@ public function buildDatagrid() | |
if ($this->hasListFieldDescription($filterParameters['_sort_by'])) { | ||
$filterParameters['_sort_by'] = $this->getListFieldDescription($filterParameters['_sort_by']); | ||
} else { | ||
$filterParameters['_sort_by'] = $this->getModelManager()->getNewFieldDescriptionInstance( | ||
$this->getClass(), | ||
$filterParameters['_sort_by'], | ||
[] | ||
$filterParameters['_sort_by'] = $this->createFieldDescription( | ||
$filterParameters['_sort_by'] | ||
); | ||
|
||
$this->getListBuilder()->buildField(null, $filterParameters['_sort_by'], $this); | ||
|
@@ -2892,6 +2890,26 @@ final public function hasTemplateRegistry(): bool | |
return null !== $this->templateRegistry; | ||
} | ||
|
||
final public function createFieldDescription(string $propertyName, array $options = []): FieldDescriptionInterface | ||
{ | ||
$fieldDescriptionFactory = $this->getFieldDescriptionFactory(); | ||
|
||
// NEXT_MAJOR: Remove the "if" block and leave the "else" one. | ||
if (null === $fieldDescriptionFactory) { | ||
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance( | ||
$this->getClass(), | ||
$propertyName, | ||
$options | ||
); | ||
} else { | ||
$fieldDescription = $fieldDescriptionFactory->create($this->getClass(), $propertyName, $options); | ||
} | ||
|
||
$fieldDescription->setAdmin($this); | ||
|
||
return $fieldDescription; | ||
} | ||
|
||
/** | ||
* @phpstan-return T | ||
*/ | ||
|
@@ -3096,8 +3114,7 @@ protected function buildList() | |
$mapper = new ListMapper($this->getListBuilder(), $this->list, $this); | ||
|
||
if (\count($this->getBatchActions()) > 0 && $this->hasRequest() && !$this->getRequest()->isXmlHttpRequest()) { | ||
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance( | ||
$this->getClass(), | ||
$fieldDescription = $this->createFieldDescription( | ||
'batch', | ||
[ | ||
'label' => 'batch', | ||
|
@@ -3107,7 +3124,6 @@ protected function buildList() | |
] | ||
); | ||
|
||
$fieldDescription->setAdmin($this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to keep this, no ? |
||
// NEXT_MAJOR: Remove this line and use commented line below it instead | ||
$fieldDescription->setTemplate($this->getTemplate('batch')); | ||
// $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('batch')); | ||
|
@@ -3122,8 +3138,7 @@ protected function buildList() | |
} | ||
|
||
if ($this->hasRequest() && $this->getRequest()->isXmlHttpRequest()) { | ||
$fieldDescription = $this->getModelManager()->getNewFieldDescriptionInstance( | ||
$this->getClass(), | ||
$fieldDescription = $this->createFieldDescription( | ||
'select', | ||
[ | ||
'label' => false, | ||
|
@@ -3133,7 +3148,6 @@ protected function buildList() | |
] | ||
); | ||
|
||
$fieldDescription->setAdmin($this); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same |
||
// NEXT_MAJOR: Remove this line and use commented line below it instead | ||
$fieldDescription->setTemplate($this->getTemplate('select')); | ||
// $fieldDescription->setTemplate($this->getTemplateRegistry()->getTemplate('select')); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,8 @@ | |
* | ||
* @author Thomas Rabaix <[email protected]> | ||
* | ||
* @psalm-import-type FieldDescriptionOptions from FieldDescriptionInterface | ||
* | ||
* @method void setFieldMapping(array $fieldMapping) | ||
* @method void setAssociationMapping(array $associationMapping) | ||
* @method void setParentAssociationMappings(array $parentAssociationMappings) | ||
|
@@ -141,6 +143,9 @@ abstract class BaseFieldDescription implements FieldDescriptionInterface | |
|
||
/** | ||
* NEXT_MAJOR: Remove the null default value for $name and restrict param type to `string`. | ||
* | ||
* @psalm-param FieldDescriptionOptions $options | ||
* @phpstan-param array<string, mixed> $options | ||
*/ | ||
public function __construct( | ||
?string $name = null, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,46 @@ | |
namespace Sonata\AdminBundle\Admin; | ||
|
||
use Sonata\AdminBundle\Exception\NoValueException; | ||
use Symfony\Component\Form\DataTransformerInterface; | ||
use Symfony\Component\PropertyAccess\PropertyPathInterface; | ||
|
||
/** | ||
* @author Thomas Rabaix <[email protected]> | ||
* | ||
* @psalm-type FieldDescriptionOptions = array{ | ||
* accessor?: string|callable|PropertyPathInterface, | ||
* actions?: array, | ||
* admin_code?: string, | ||
* associated_property?: string, | ||
* block_name?: string, | ||
* catalogue?: string, | ||
* data_transformer?: DataTransformerInterface, | ||
* edit?: string, | ||
* editable?: bool, | ||
* field_name?: string, | ||
* field_options?: array, | ||
* field_type?: string, | ||
* header_class?: string, | ||
* identifier?: bool, | ||
* inline?: string, | ||
* label?: bool|string|null, | ||
* link_parameters?: array, | ||
* multiple?: bool, | ||
* placeholder?: string, | ||
* required?: bool, | ||
* role?: string|string[], | ||
* route?: array, | ||
* safe?: bool, | ||
* sort_field_mapping?: array, | ||
* sort_parent_association_mappings?: array, | ||
* sortable?: bool, | ||
* template?: string, | ||
* timezone?: string|\DateTimeZone, | ||
* translation_domain?: string, | ||
* type?: string, | ||
* virtual_field?: bool | ||
* } | ||
* | ||
* @method string|null getTargetModel() | ||
* @method bool hasAdmin() | ||
* @method bool hasParent() | ||
|
@@ -105,13 +141,19 @@ public function setOption($name, $value); | |
* - template. | ||
* | ||
* Then the value are copied across to the related property value | ||
* | ||
* @psalm-param FieldDescriptionOptions $options | ||
* @phpstan-param array<string, mixed> $options | ||
*/ | ||
public function setOptions(array $options); | ||
|
||
/** | ||
* Returns options. | ||
* | ||
* @return array<string, mixed> options | ||
* | ||
* @psalm-return FieldDescriptionOptions | ||
* @phpstan-return array<string, mixed> | ||
*/ | ||
public function getOptions(); | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
use Sonata\AdminBundle\Builder\RouteBuilderInterface; | ||
use Sonata\AdminBundle\Builder\ShowBuilderInterface; | ||
use Sonata\AdminBundle\Exporter\DataSourceInterface; | ||
use Sonata\AdminBundle\FieldDescription\FieldDescriptionFactoryInterface; | ||
use Sonata\AdminBundle\Filter\Persister\FilterPersisterInterface; | ||
use Sonata\AdminBundle\Model\ModelManagerInterface; | ||
use Sonata\AdminBundle\Route\RouteGeneratorInterface; | ||
|
@@ -41,25 +42,27 @@ | |
* - The first and third argument are automatically injected by the AddDependencyCallsCompilerPass. | ||
* - The second one is used as a reference of the Admin in the Pool, with the `setAdminClasses` call. | ||
* | ||
* @method void initialize() | ||
* @method void setLabel(?string $label) | ||
* @method void showMosaicButton(bool $isShown) | ||
* @method void setPagerType(string $pagerType) | ||
* @method string getPagerType() | ||
* @method void setManagerType(string $managerType) | ||
* @method void setSecurityInformation(array $information) | ||
* @method void setFilterPersister(?FilterPersisterInterface $filterPersister = null) | ||
* @method FilterPersisterInterface|null getFilterPersister() | ||
* @method bool hasFilterPersister() | ||
* @method void setModelManager(ModelManagerInterface $modelManager) | ||
* @method void setDataSource(DataSourceInterface $dataSource) | ||
* @method DataSourceInterface getDataSource() | ||
* @method FormContractorInterface getFormContractor() | ||
* @method void setShowBuilder(ShowBuilderInterface $showBuilder) | ||
* @method ShowBuilderInterface getShowBuilder() | ||
* @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 commentThe 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Just an idea for all these methods... Can we add a new We can then use |
||
* @method void setLabel(?string $label) | ||
* @method void showMosaicButton(bool $isShown) | ||
* @method void setPagerType(string $pagerType) | ||
* @method string getPagerType() | ||
* @method void setManagerType(string $managerType) | ||
* @method void setSecurityInformation(array $information) | ||
* @method void setFilterPersister(?FilterPersisterInterface $filterPersister = null) | ||
* @method FilterPersisterInterface|null getFilterPersister() | ||
* @method bool hasFilterPersister() | ||
* @method void setModelManager(ModelManagerInterface $modelManager) | ||
* @method void setDataSource(DataSourceInterface $dataSource) | ||
* @method DataSourceInterface getDataSource() | ||
* @method void setFieldDescriptionFactory(FieldDescriptionFactoryInterface $fieldDescriptionFactory) | ||
* @method FieldDescriptionFactoryInterface getFieldDescriptionFactory() | ||
* @method FormContractorInterface getFormContractor() | ||
* @method void setShowBuilder(ShowBuilderInterface $showBuilder) | ||
* @method ShowBuilderInterface getShowBuilder() | ||
* @method Pool getConfigurationPool() | ||
* @method void setRouteGenerator(RouteGeneratorInterface $routeGenerator) | ||
* @method RouteGeneratorInterface getRouteGenerator() | ||
*/ | ||
interface TaggedAdminInterface | ||
{ | ||
|
@@ -168,6 +171,16 @@ public function getModelManager(); | |
*/ | ||
// public function getDataSource(): DataSourceInterface; | ||
|
||
/** | ||
* NEXT_MAJOR: Uncomment this method. | ||
*/ | ||
// public function setFieldDescriptionFactory(FieldDescriptionFactoryInterface $fieldDescriptionFactory): void; | ||
|
||
/** | ||
* NEXT_MAJOR: Uncomment this method. | ||
*/ | ||
// public function getFieldDescriptionFactory(): FieldDescriptionFactoryInterface; | ||
|
||
/** | ||
* @return void | ||
*/ | ||
|
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
?