Download & Extend

Replace $form['array_filter'] hack with #element_validate

Project:Drupal core
Version:8.x-dev
Component:system.module
Category:bug report
Priority:normal
Assigned:Dave Reid
Status:needs review
Issue tags:DrupalWTF

Issue Summary

I noticed that when I am using

<?php
$form
['array_filter'] = array('#type' => 'value', '#value' => TRUE);
?>

in my forms with checkboxes, that there is a variable 'array_filter' being saved into my variables table.

I looked back at some of the history of array_filter issues, and noticed that #41170: Don't save junk as variables during settings save was meant to fix this, but a different fix was committed that does not prevent the saving of array_filter. The attached patch modifies system_settings_form to not save the 'array_filter' form option as a variable.

AttachmentSizeStatusTest resultOperations
array_filter-D7.patch1.46 KBIgnoredNoneNone

Comments

#1

Title:system_settings_form saves undesired 'array_filter' variable» system_settings_form_submit saves undesired 'array_filter' variable

This bug is also present in D6 and D5 and should be backported if this is deemed an acceptable solution.

#2

Here are the D5 and D6 patches for this issue just for kicks. Don't hurt me testing bot...

AttachmentSizeStatusTest resultOperations
array_filter-D6.patch1.48 KBIgnoredNoneNone
array_filter-D5.patch1.26 KBIgnoredNoneNone

#3

Here 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:

<?php
$form
['item']['#element_validate'] = array('drupal_filter_array');
?>
AttachmentSizeStatusTest resultOperations
array_filter-2-D7.patch2.64 KBIgnoredNoneNone

#4

Status:needs review» needs work

I love #3, that's so Form API compliant :)

We should include an update function that remove array_filter from the variable table.

#5

Status:needs work» needs review

New 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.

AttachmentSizeStatusTest resultOperations
array_filter-3-D7.patch4.48 KBIgnoredNoneNone

#6

Title:system_settings_form_submit saves undesired 'array_filter' variable» Remove $form['array_filter'] hack with #element_validate

Renaming for new direction and bump for review or thoughts. Anyone?

#7

Title:Remove $form['array_filter'] hack with #element_validate» Replace $form['array_filter'] hack with #element_validate

#8

Status:needs review» needs work

According to the recommendations of chx in IRC, it's better to use #process. Any #process handlers attached to a specific element are executed before they are processed.

#9

I 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'? :)

#10

#11

Issue tags:+DrupalWTF

Adding WTF tag.

#12

Unless 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?

#13

subscribing

#14

Version:7.x-dev» 8.x-dev

Too late for D7.

#15

Agreed, although I'm still interested in helping fix.

#16

#17

subscribe

#18

Status:needs work» needs review

I kind of re-rolled to 8.x

I didn't know what to do with update function on .install, is it needed for D8?

AttachmentSizeStatusTest resultOperations
315176-array_filter_hack_removal.patch2.66 KBIdlePASSED: [[SimpleTest]]: [MySQL] 33,646 pass(es).View details | Re-test
nobody click here