Skip to content

Keep only common option in FieldDescriptionOptions type#7103

Merged
VincentLanglet merged 3 commits intosonata-project:3.xfrom
VincentLanglet:removeOptions
Apr 27, 2021
Merged

Keep only common option in FieldDescriptionOptions type#7103
VincentLanglet merged 3 commits intosonata-project:3.xfrom
VincentLanglet:removeOptions

Conversation

@VincentLanglet
Copy link
Member

@VincentLanglet VincentLanglet commented Apr 22, 2021

Subject

Follow up of the discussion
#7101

This type is currently not describing correctly the possible options ATM.
Since @franmomu is not agreeing with this, I prefer to wait for his approval before any merge.

Changelog

### Fixed
- FieldDescriptions options phpdoc to keep only common options.

@VincentLanglet VincentLanglet marked this pull request as ready for review April 22, 2021 08:09
@VincentLanglet VincentLanglet requested review from a team and franmomu April 22, 2021 08:09
@franmomu
Copy link
Member

you are right, I don't agree with this, I think we are missing information and going backwards, if we need more options we can add them, if we want to allow users to add any option (not documented), we should add an option for that, otherwise it's a mess.

Anyway, I do not tend to block things because it's my opinion and I might be wrong, so if there are people that agree with this I'm fine.

@jordisala1991
Copy link
Member

IMO if it causes some kind of regression to current code or we are not all agree with that type, we should first revert it and have green builds again. And then discuss about how this can be done.

@franmomu
Copy link
Member

franmomu commented Apr 22, 2021

But it's not because of this that test are not passing, it is after the addition of https://github.com/sonata-project/SonataAdminBundle/pull/7040/files#diff-dba1a2c25ea11b814213238e6c62006084f246e9be5cd8ca251b4f9a65a3a292R56 and the upgrade of phpstan (they can be ignored too)

@jordisala1991
Copy link
Member

Then it would fall under the "we are not all agree this is the correct move".

We should agree on: Maybe this is a good move, but the current code does not reflects this behavior, and must be changed before enforcing some options that are not realistic. wdyt?

@VincentLanglet
Copy link
Member Author

From the slack discussion:

I think we should only keep the option use by the DatagridMapper, ListMapper, ShowMapper and FormMapper

DatagridMapper use field_name, label, role
ListMapper use associated_property, sortable, sort_field_mapping, actions, label, role, virtual_field
FormMapper use property_path , role, property_path, type, translation_domain, label
ShowMapper use label role safe
So the only common option are label and role
So FieldDescriptionOptions = array{label: ..., role: ...}&array<string, mixed>

But we could create
ListFieldDescriptionOptions = FieldDescriptionOptions&array{sortable: ...., ...}
and so on

Type-specific options like timezone won't still be added to these types, but at least it allows us to restrict some values.
WDYT @franmomu ?

@VincentLanglet VincentLanglet marked this pull request as draft April 22, 2021 19:29
@VincentLanglet
Copy link
Member Author

VincentLanglet commented Apr 22, 2021

I made some research about the current typed options:

 * @psalm-type FieldDescriptionOptions = array{
 *  accessor?: string|callable|PropertyPathInterface,               // Used by BaseFieldDescription
 *  actions?: array,                                                // Used by a specific type of the List
 *  admin_code?: string,                                            // Used by List, Show and Filter for field with relation
 *  associated_property?: string|callable|PropertyPathInterface,    // Used by List and show for relation
 *  block_name?: string|false,                                      // Used by Form
 *  catalogue?: string,                                             // Used by a specific type of the List/Show
 *  data_transformer?: DataTransformerInterface,                    // Used by Form
 *  edit?: string,                                                  // Used by Form
 *  editable?: bool,                                                // Used by some type of the List
 *  field_name?: string,                                            // Used by Filter
 *  field_options?: array,                                          // Used by Filter and form
 *  field_type?: string,                                            // Used by Filter and form
 *  header_class?: string,                                          // Used by List
 *  identifier?: bool,                                              // Used by List field with relation
 *  inline?: string,                                                // Used by Show/List/Form
 *  label?: string|false|null,                                      // Used by all
 *  link_parameters?: array,                                        // Used by List, Show and Filter for field with relation
 *  multiple?: bool,                                                // Used by some type of the List (related to editable)
 *  placeholder?: string|false,                                     // Used by Form with relation
 *  required?: bool,                                                // Used by List field with relation (related to editable)
 *  role?: string|string[],                                         // Used by all
 *  route?: array,                                                  // Used (differently ?) for type url and some relations of List/Show
 *  safe?: bool,                                                    // Used by some type of the show
 *  sort_field_mapping?: array,                                     // Used by BaseFieldDescription, but only for the List
 *  sort_parent_association_mappings?: array,                       // Used by BaseFieldDescription, but only for the List
 *  sortable?: bool,                                                // Used by BaseFieldDescription, but only for the List
 *  template?: string,                                              // Used by BaseFieldDescription
 *  timezone?: string|\DateTimeZone,                                // Use by a specific type of show/list
 *  translation_domain?: string,                                    // Used by BaseFieldDescription
 *  type?: string,                                                  // Used by BaseFieldDescription
 *  virtual_field?: bool                                            // Used by BaseFieldDescription
 * }&array<string, mixed>

IMHO:

  • Template-type specific options should be removed
  • Options used by BaseFieldDescriptions can be kept in the FieldDescriptionOptions
  • label and role should be kept too.
  • placeholder and link_parameters shouldn't be set by default in the BaseFieldDescriptions since it's only used for some type/situations.
  • I'm not sure it makes sens to have isSortable, getSortFieldMapping and getSortParentAssociationMapping in the FieldDescriptionInterface since it's only related to the List...

@VincentLanglet VincentLanglet marked this pull request as ready for review April 23, 2021 08:26
@VincentLanglet VincentLanglet changed the title Remove FieldDescriptionOptions Keep only common option in FieldDescriptionOptions type Apr 24, 2021
'linkParameters': sonata_admin.field_description.option('link_parameters', {})
})) }}
{% elseif sonata_admin.field_description.option('placeholder') %}
{% elseif sonata_admin.field_description.option('placeholder', 'short_object_description_placeholder') %}
Copy link
Member

Choose a reason for hiding this comment

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

this can't be correct, otherwise it is an always true if and can be removed with just else

Copy link
Member Author

Choose a reason for hiding this comment

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

To disabled the placeholder, 'false' should be use.

'linkParameters': sonata_admin.field_description.option('link_parameters', {}))
})) }}
{% elseif sonata_admin.field_description.option('placeholder') %}
{% elseif sonata_admin.field_description.option('placeholder', 'short_object_description_placeholder') %}
Copy link
Member

Choose a reason for hiding this comment

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

same here


// set default placeholder
// NEXT_MAJOR: Remove this.
if (!isset($options['placeholder'])) {
Copy link
Member

Choose a reason for hiding this comment

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

if not set, it will set a default, but if it is set to null, it won't, I think the new behavior you implemented don't cover this case

Copy link
Member Author

@VincentLanglet VincentLanglet Apr 26, 2021

Choose a reason for hiding this comment

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

isset(null) is false. So null wasn't the way to disable the placeholder. false is the way.

@SonataCI
Copy link
Collaborator

Could you please rebase your PR and fix merge conflicts?

jordisala1991
jordisala1991 previously approved these changes Apr 26, 2021
@VincentLanglet
Copy link
Member Author

I'll try to fix the psalm issue with empty array vimeo/psalm#5677

@VincentLanglet
Copy link
Member Author

Use both phpstan-type and psalm-type is a temporary workaroung since psalm need array<empty, empty> (it's a bug) but phpstan does not support this.

@VincentLanglet VincentLanglet requested review from a team and jordisala1991 April 27, 2021 00:25
jordisala1991
jordisala1991 previously approved these changes Apr 27, 2021
@jordisala1991 jordisala1991 requested a review from a team April 27, 2021 05:09
* - type (m): define the field type (use to tweak the form or the list)
* - template (o) : the template used to render the field
* - name (o) : the name used (label in the form, title in the list)
* - link_parameters (o) : add link parameter to the related Admin class when
Copy link
Member

Choose a reason for hiding this comment

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

Apparently link_parameters it us used in the route generator, so I guess is kind of global:

$parameters = array_merge($parameters, $admin->getParentFieldDescription()->getOption('link_parameters', []));

* - associated_tostring : (deprecated, use associated_property option)
* the method to retrieve the "string" representation
* of the collection element.
* - associated_property : property path to retrieve the "string" representation
Copy link
Member

Choose a reason for hiding this comment

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

associated_property it is used in RenderElementExtension which means it is used in render_relation_element Twig filter that it is used globally: https://github.com/sonata-project/SonataAdminBundle/search?q=render_relation_element

@VincentLanglet VincentLanglet merged commit 1e90e64 into sonata-project:3.x Apr 27, 2021
@VincentLanglet VincentLanglet deleted the removeOptions branch April 27, 2021 14:00
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.

4 participants