Problem/Motivation
When reusing an existing field on the "Manage fields" page (for the Basic Page type, /admin/structure/types/manage/page/fields
) we do not get the expected configuration. The "field storage" settings will be the same as the original, but the "field" settings will be set to the defaults for the field type.
For example, if the Tags field on the Article content type is configured to target the Tags vocabulary and to use the "Autocomplete (Tags style)" form widget, then neither of these settings is copied when we reuse the field on the Page content type.
Another example: when reusing a field provided by the Paragraphs module, the existing field has a widget provided by the module, and the Paragraphs module would like to use that widget on the new field, but instead the field gets a different widget. (Paragraphs uses the Entity Reference Revisions module, and that module specifies an autocomplete field as the default widget.)
The problem with Paragraphs is pretty well described, with steps to reproduce, in #2668678: Make paragraphs widget the default for paragraphs fields and #2719593: Unable to reuse existing paragraphs field. (These are two issues describing the same problem.)
After #2446511: Add a "preconfigured field options" concept in Field UI, the Field UI allows "Preconfigured options" for a field, but these are used only when adding a new field, not when reusing an existing one. In Drupal core, this is only used to set the target type for Entity Reference fields (a "field storage" setting). The Paragraphs module uses preconfigured options to set the form widget, and it is also possible to set options for the view mode.
Proposed resolution
Option 1
When reusing a field, give the user the choice of available bundle/field combinations, and then copy settings as appropriate. The user will still be able to change the settings on the later steps in the form.
The select list for reusing a field would be grouped, just as the list of field options already is. We could either group by content type, with "Other" for existing fields that are not attached to any bundle, or else we could group by field, with the corresponding content types listed under each field.
The UI would get cluttered if there are a lot of fields on a lot of content types. There are many cases where the extra level of organization would be helpful.
Option 2
When creating a new field, keep track of whether it is using preconfigured options. If it is, then keep track of which key is used in the array returned by getPreconfiguredOptions()
. Then, when reusing a field, get the preconfigured options that were used when the field was created, and use them for defaults.
Again, the user will be able to modify the settings in later steps.
This option is close to the suggestion in the original summary of this issue.
Option 3
Deprecate the mechanism of preconfigured options. Require each entry in the "Add a new field" select list to come from a field type plugin. Provide a BC layer for field type plugins that currently use preconfigured options.
Remaining tasks
Decide on which option to use.
Create a patch.
Add tests.
User interface changes
Option 1: This will change the UI of the "Add field" page (/admin/structure/types/manage/*/fields/add-field
).
Option 1 and Option 2: When reusing an existing field, the user will see different default values on later steps and on the "Manage form display" and "Manage display" tabs.
Option 3: none
API changes
Option 1 and Option 2: none
Option 3: deprecate the preconfigured options mechanism, including PreconfiguredFieldUiOptionsInterface
.
Data model changes
Option 1 and Option 3: none
Option 2: Save an additional key (say "preset_key") with a "field storage" config item. It can be NULL or an empty string if no preconfigured options were used, or else it will be the selected key from the array returned by getPreconfiguredOptions()
.
Original report by @yongt9412
Problem/Motivation
In the FieldStorageAddForm the preconfigured options are merged only in submitForm thus when we reuse a field the preconfigured values are not properly initialized.
Proposed resolution
Fix it. Merge the preconfigured options also when reusing.