Problem/Motivation
Core option fields don't allow using predefined select list. One can use https://github.com/Realityloop/list_predefined_options to add the functionality which is an unoffical port of http://drupal.org/project/list_predefined_options.
It is a very generic functionality i.e. country list, states lists, timezones and language list.
Proposed resolution
Move https://github.com/Realityloop/list_predefined_options functionality to the core. It shouldn't have to be an experimental module.
Remaining tasks
- Create Patch (WIP)
- Update the docs with the new plugin type
User interface changes
None
API changes
The 'allowed_values_function' will be deprecated since its functionality is covered by the new plugin type.
Comment | File | Size | Author |
---|---|---|---|
#52 | 1909744-51.patch | 30.18 KB | asanchezs |
#41 | 1909744-41.patch | 30.74 KB | BryanGullan |
#38 | 1909744-38.patch | 30.65 KB | alvolvfdez |
#35 | 1909744-35.patch | 31.35 KB | alvolvfdez |
#34 | interdif_32-34.txt | 4.64 KB | vsujeetkumar |
Issue fork drupal-1909744
Show commands
Start within a Git clone of the project using the version control instructions.
Or, if you do not have SSH keys set up on git.drupalcode.org:
Comments
Comment #1
jibranTagging 'Field API' as per @swentel suggestion.
Comment #2
swentel CreditAttribution: swentel commentedRemoving tags for now (just to get focus on our personal issue list scraper)
Comment #2.0
swentel CreditAttribution: swentel commentedAdd module http://drupal.org/project/list_predefined_options
Comment #3
jibranComment #7
jibranThere is a D8 port now in #2635932-3: Drupal 8? now which is maintained at https://github.com/Realityloop/list_predefined_options.
Comment #8
joachim CreditAttribution: joachim as a volunteer commentedNeeds a summary update, as the Proposed resolution shouldn't be a hook :)
It should be fairly simple to add the D8 code I wrote to Options core module, IIRC it's mostly a plugin and a little bit of UI.
Comment #9
jibranIs it better?
Comment #10
joachim CreditAttribution: joachim as a volunteer commentedYup, much! Thanks!
One question: should this be a separate modoule, or should the functionality be added to Options module?
Comment #11
jibranLet's add it to options module.
Comment #12
joachim CreditAttribution: joachim as a volunteer commentedI had another look at the the D8 port of list_predefined_options (at #2635932: Drupal 8?), and I see now that there are some changes that would be needed or desirable to bring it into core:
- rather than store the plugin ID in third party settings, we should instead add a new property ('predefined_options_plugin') to the schemas for the storage settings, eg field.storage_settings.list_integer
- options_allowed_values() should use the predefined_options_plugin setting if present to get the options. (basically, do the work of list_predefined_options_allowed_values() in my module code)
- the allowed_values_function system should be deprecated (the allowed_values_function property, and callback_allowed_values_function()). This is because the same thing can be accomplished with a plugin.
- list_predefined_options_form_field_storage_config_edit_form_builder() isn't necessary, since there's no longer a need to set the 'allowed_values_function' property.
- additionally, the 'US states' plugin should be removed IMO, as it's country-specific, and Drupal has an international audience.
Also, it might be an idea to consider the UI, since adding the elements in the actual field storage settings form rather than using hook_form_alter() means we could do something different, if it improved the usability of the form.
Comment #17
d70rr3s CreditAttribution: d70rr3s as a volunteer and at Hiberus commentedHi, I just found my self using the list_predefined_options port quite a lot and felt eager to see it in core. The attached patch ports the feature with the design notes from #12 also added some tests but still some tasks remaining. I put it on Needs review so once most of the patch get reviewed I will continue working on the remaining tasks.
Comment #18
d70rr3s CreditAttribution: d70rr3s as a volunteer commentedTook the lease of updating the issue summary.
Comment #20
d70rr3s CreditAttribution: d70rr3s as a volunteer commentedAttached new patch with more tests, also fixed CS from bot and removed the @trigger_error that was making some tests to fail. Still needs work seems some tests keep failing. If is okay for maintainers this can be assigned to me.
Comment #21
d70rr3s CreditAttribution: d70rr3s at Hiberus commentedSeems that removing
allowed_values_function
from options.schema.yml is breaking some integration tests on other core's components. I just add it back and restore the field storage settings form element (the warning message still be shown) and the tests. I think I'll need some assistance here on how to proceed.Comment #22
d70rr3s CreditAttribution: d70rr3s as a volunteer commentedAdded the
predefined_options_plugin
key to Umami profile configuration files to avoid configuration tests failures.Comment #23
d70rr3s CreditAttribution: d70rr3s at Hiberus commentedWell seems this is working fine now putting it to Needs review.
Comment #24
d70rr3s CreditAttribution: d70rr3s at Hiberus commentedI've queue for another test round to check everything is working with latest codebase. @joachim if someone could review this since seems complete by your guidelines.
Comment #26
yonailo CreditAttribution: yonailo as a volunteer commentedHi,
I don't understand what the problem is with the current 'allowed_values_function' and what this issue adds that could not be done using the current 'allowed_values_function' functionality. Can you develop please ?
Comment #27
d70rr3s CreditAttribution: d70rr3s at Hiberus commentedThere's none, this feature request is about replacing it with a new plugin type. The #1909744-21: Allow adding predefined lists to the options fields refers about BC, the
allowed_values_function
is used on tests outside the options module also other contrib modules may be using it to define their own options selectors. Please read the whole thread specially #1909744-12: Allow adding predefined lists to the options fields, @joachim describes the proposed solution. There is also a previous issue seeking this exact same feature #2305385: Convert "allowed_values_function" to a plugin.Comment #32
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedRe-roll patch created for 9.3.x.
Comment #34
vsujeetkumar CreditAttribution: vsujeetkumar at Srijan | A Material+ Company for Drupal India Association commentedFixed fail tests, Please have a look.
Comment #35
alvolvfdez CreditAttribution: alvolvfdez at Hiberus commentedRerolled for 9.2.x
Comment #36
joachim CreditAttribution: joachim as a volunteer commentedThis is looking good! And the new hook has documentation -- great!
Mostly typos, but I do wonder whether we should use a proper metadata object instead of the old D7-era $cacheable boolean.
(Yay! Documentation!)
This line is moving for no reason. Since the patch needs work anyway, would be good to remove this change.
Stray scaffolding code.
In fact, we don't need this class since it's empty.
Typo.
I wonder if we should take this opportunity of a new system to change from this boolean $cacheable parameter to returning a proper cache metadata object.
'cannot not'
Comment #38
alvolvfdez CreditAttribution: alvolvfdez at Hiberus commentedReroll for 9.3.x.
Removed the lines where typing was added in some variables. In Drupal 9.3.0 they are already added. File: core/modules/options/tests/src/Functional/OptionsFieldUITest.php
Comment #41
BryanGullan CreditAttribution: BryanGullan as a volunteer commentedRe-rolled patch for 9.5.x
Comment #45
BramDriesen#36 Still needs to be addressed.
Comment #46
BramDriesen1 (nothing to do), 2, 4 and 6 are fixed.
For 3, not really sure what's going on, as the interface is defining getAllowedValues. I guess we could add a stub instead of removing it?
For 5, Still a to-do, currently too late for me to start on that.
Comment #47
LendudeSounds like a good improvement over callbacks!
Some observations:
This needs an upgrade path to set the empty value
I don't think this currently has test coverage?
Versions need updating and needs to point to a Change record and not this issue
Do we usually fail silently on these? I think in other instances we just let it fail hard, something is seriously wrong if a plugin disappears.
An indication of which field is using this might help people track down what they need to update? And why the watchdog_exception and not just a trigger_error("bla bla", E_USER_DEPRECATED);?
Add a type hint
add a type hint
Setting vars in ifs I think we try to avoid (it's pretty clumsy to read)
Why not just trigger_error("bla bla", E_USER_DEPRECATED);
Are we sure we want to supply this for all installs of Drupal, it feels like a good test case to use, but I'd say it should probably be in a test module. I don't think the need for this is in the '80% of all sites' category.
Do we need test coverage for this?
We need a test for this I feel, so use the options list in the Views UI and make sure it works
This does exactly the opposite of what the method docblock says you should do when $entity is NULL, that sounds like a bad example to set
We tend not to set messages for assertions anymore so we can probably leave that out
We use $this and not self:: for assertions
This is not using the browser I think? so does this need to be a functional test or can it be a kernel test?
Expected values go first
Add a check that $allowed_values is not empty, currently if it is empty, the test passes
We use $this not self:: for assertions
Comment #48
LendudeOh forgot to refresh and missed the MR, so some of that might have been addressed already!
Comment #49
BramDriesenActually not 😉 Think all your comments still apply. Although the deprecated watchdog_exceptions are replaced with the logger API.
Comment #50
dpiWe have another issue for using backed enums as a form of predefined list: #3249599: Add support for PHP 8.1 Backed Enums in Select, Checkboxes, Radio elements
Can we update/refocus this issue so it specifically outlines this is a plugin based solution?
Bonus for why plugin based solution is worthwhile alongside the enum solution (presumably dynamism?)
I understand
allowed_values_function
is due to be deprecated by this issue, it would be ideal to have a solution compatible with both plugin and enum solutions simultaneously.Comment #51
joachim CreditAttribution: joachim at Factorial GmbH commentedI've made a D9/10 release of https://www.drupal.org/project/list_predefined_options.
#3249599: Add support for PHP 8.1 Backed Enums in Select, Checkboxes, Radio elements is not the same as this issue -- it's operating at the FormAPI level. This issue is at the FieldAPI level.
> Bonus for why plugin based solution is worthwhile alongside the enum solution (presumably dynamism?)
Dynamism is one. That would include things like being able to depend on other Drupal components, e.g. #1412946: Allow Views to be used for predefined lists. Reusability is another.
> it would be ideal to have a solution compatible with both plugin and enum solutions simultaneously.
Define a plugin which returns an enum.
Comment #52
asanchezs CreditAttribution: asanchezs as a volunteer and at Hiberus commentedPatch for Drupal 10.2.x