Problem/Motivation
Removal of "filtering" checkboxes values has change record https://www.drupal.org/node/1910694
There was a set of issues per module, so this is follow-up to conversion #1703164-26: Convert book settings to the new configuration system
+++ b/core/modules/book/book.admin.inc
@@ -45,14 +47,14 @@ function book_admin_settings() {
$form['array_filter'] = array('#type' => 'value', '#value' => TRUE);
@@ -66,6 +68,18 @@ function book_admin_settings_validate($form, &$form_state) {
+ ->set('allowed_types', $form_state['values']['book_allowed_types'])
+ ->set('child_type', $form_state['values']['book_child_type'])
This property has supposed to filter out empty values before saving
Proposed resolution
Remove the form element & add filtering for empty values
Remaining tasks
Review, commit
Data model changes
May need update hook to clean-up existing empty values out of child_type key of book.settings
Comments
Comment #1
dave reidThis bug is also present in D6 and D5 and should be backported if this is deemed an acceptable solution.
Comment #2
dave reidHere are the D5 and D6 patches for this issue just for kicks. Don't hurt me testing bot...
Comment #3
dave reidHere is another proposed solution, that removes the FAPI hack 'array_filter' and adds a new function drupal_filter_array() in form.inc that can be used with #element_validate:
Comment #4
damien tournoud commentedI love #3, that's so Form API compliant :)
We should include an update function that remove array_filter from the variable table.
Comment #5
dave reidNew patch:
- Adds a function drupal_filter_form_array that replaces the FAPI hack $form['array_filter'] with
$form['item']['#element_validate'] = array('drupal_filter_form_array');- Adds system_update_7011 to delete the unwanted 'array_filter' variable that was accidentally saved.
- Picked up a few coding space standard issues.
Comment #6
dave reidRenaming for new direction and bump for review or thoughts. Anyone?
Comment #7
dave reidComment #8
dropcube commentedAccording to the recommendations of chx in IRC, it's better to use
#process. Any#processhandlers attached to a specific element are executed before they are processed.Comment #9
dave reidI need some help with this...I have no idea how to get #process working to filter array values on submission... Where's my patch status of 'code needs help'? :)
Comment #10
dave reidComment #11
dave reidAdding WTF tag.
Comment #12
sunUnless chx provided some hidden reasoning that's not stated here, I think that #element_validate is perfectly correct here.
Lovely patch! Just a bit old. Anyone up for re-rolling?
Comment #13
drewish commentedsubscribing
Comment #14
sunToo late for D7.
Comment #15
dave reidAgreed, although I'm still interested in helping fix.
Comment #16
andypostRelated #1120306: Do not save array_filter in system_settings_form_submit()
Comment #17
franzsubscribe
Comment #18
franzI kind of re-rolled to 8.x
I didn't know what to do with update function on .install, is it needed for D8?
Comment #19
dave reidRe-rolled again for HEAD.
Comment #20
sunThanks for moving this forward! :)
Some remarks on the proposed code:
This looks/reads like superfluous historical context to me and should thus be removed.
1) "FAPI" does not exist, should be changed to "Form API".
2) Missing "#type" before 'checkboxes'.
3) How about wrapping the code into @code and @endcode?
The first !isset() condition looks wrong to me -- a callback like this shouldn't perform any actions unless explicitly being told so.
I wonder whether there is a more self-descriptive property name than #filter_array?
Since this only applies to checkboxes, what do you think of
#submit_enabled_onlyor similar...?I wonder whether this entire form validation handler isn't obsolete? It seems to duplicate Form API's built-in options checker...?
8017 already exists in HEAD.
Comment #21
mgiffordComment #22
alan d. commentedHow about #filter_submitted_values, as that is what it is doing? :)
To me, the name #filter_array suggests to me that the input is filtered, and #submit_enabled_only was a bit confusing when I first read it.
Agree with Sun, this should only kick in if it is expressively defined, which I think makes the "+ '#filter_array' => FALSE," redundant.
Comment #26
joachim commentedThe issue summary is really out of date with the latest work on this issue.
Why do we need to add an option for whether to filter a checkboxes element's value? What's the use case for keeping the non-selected items in the stored value? Form API doesn't need them when you display the form later on.
Comment #27
andypostPatch removes remains but IS still needs update
@joachim the main usecase is storing only checked values on form submit and it makes sense only for checkboxes form element
But since this option no longer used https://www.drupal.org/node/1910694
it was https://api.drupal.org/api/drupal/modules%21system%21system.module/funct...
So suggestion is just get rid of remains and probably separate issue to add this option to checkboxes element
Comment #28
joachim commented@andypost I think you've misunderstood my question.
I understand that this issue is about storing only the checked values for a checkboxes element.
My point is, why do we need an option on the checkboxes element for this? What is the use case for storing the unchecked ones as well? Why no just always filter out the unchecked values?
Comment #29
andypost@joachim out of head - at least to be sure that list is not modified at frontend
Comment #30
andypost@joachim thanx a lot for idea, I used to dig history and found that conversion in #1703164: Convert book settings to the new configuration system was incomplete, this falg in d7 used to filter both config arrays, so I added lost filtering
Actually it needs to add to https://www.drupal.org/node/1910694 that and I updated IS
Comment #31
andypostCR updated https://www.drupal.org/node/1910694/revisions/view/8969893/10564998
Comment #32
joachim commentedI don't think this is the right way to go at all.
Having empty items in the values array for a checkboxes element is an inconvenience in FormAPI. I thought this issue was about fixing that in a way that can be used anywhere.
Comment #33
andypostThe code is http://cgit.drupalcode.org/drupal/tree/core/modules/book/src/Form/BookSe...
allowed_types- checkboxes are filtered alreadychild_typeis radios so no filtering requiredSo #27 is right patch that removes useless remains
Comment #34
joachim commentedOk so is this issue now just about book module?
Because when it was titled 'Replace $form['array_filter'] hack with #element_validate', I thought that meant it was about adding something that covers the whole of FormAPI. That's what I am interested in -- checkboxes values that automatically filter out the pointless unselected values.
If this issue isn't meant to be about that, should I file a new one?
Comment #35
andypostYes, that's why I renamed it.
Support for this property removed long ago before 8.0 but this is the only leftover in core
Comment #37
andypostComment #43
norman.lolLet's simply remove this line. It causes confusion to this day: https://drupal.stackexchange.com/questions/296592/what-does-the-array-fi...
Comment #44
larowlanAdding issue credits
Comment #48
larowlanCommitted 65b6ffc and pushed to 9.1.x. Thanks!
Only took us 12 years to get a resolution here even it was just to remove legacy stuff 👵🧓
@joachim do you still feel that we need a follow-up to be able to opt-in to filtering on checkboxes?
Comment #49
andypost@larowlan thanks!
I think follow-up for checkboxes element makes sense, every form with checkboxes doing array_filter() to exclude empty values, so having flag in element for that is DX++