Use of drupal_map_assoc to create options arrays is wrong in the form API reference

Created on 3 March 2016, about 9 years ago
Updated 26 March 2025, 18 days ago

API page: https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h...

I'm very surprised that this documentation 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'),
)

The actual documentation text shows things correctly. It is really just the examples that are poor. These come from https://api.drupal.org/api/examples/form_example!form_example_states.inc... which should also know better! :)

🐛 Bug report
Status

Closed: won't fix

Component

API documentation files

Created by

🇩🇪Germany martin_q Leipzig, Germany | Manchester, UK

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024