API page: https://api.drupal.org/api/examples/form_example!form_example_states.inc...
I'm very surprised that this code has an #options
array example using drupal_map_assoc()
on arrays of translated strings. The keys of the array, which contain the data to be stored upon submit, should not be passed through t()
and translated, otherwise the form will break and stored data will be corrupted on multilingual sites. The corrected array for the example of a 'select' element would be
array(
'uk' => t('UK'),
'other' => t('Other'),
)
https://api.drupal.org/api/drupal/developer%21topics%21forms_api_referen...
Similarly for the checkboxes example:
array(
'SAT' => t('SAT'),
'ACT' => t('ACT'),
)
These bad examples were copied over to the Form API documentation, so I've filed an issue there too: #2680521: Use of drupal_map_assoc to create options arrays is wrong in the form API reference.
Comment | File | Size | Author |
---|---|---|---|
#8 | form-example-states-avoid-drupal-map-assoc-in-options-2680529-8.patch | 1.17 KB | martin_q |
|
Comments
Comment #2
martin_qComment #3
martin_qPatch attached.
Comment #5
martin_qRerolled with corrected paths to sub-module files.
Comment #6
martin_qComment #8
martin_qLine numbers were out. Rerolled again.
Comment #9
omkar06 CreditAttribution: omkar06 as a volunteer and at Faichi Solutions Pvt Ltd commentedPretty straight forward. Patch tested and working as expected.
Comment #11
valthebaldCommitted and pushed to 7.x-1.x, thank you @martin_q!
8.x branch doesn't have this issue, so closing as fixed
Comment #12
valthebald