Read the following comment, from system.module:

/**
 * Execute the system_settings_form.
 *
 * If you want node type configure style handling of your checkboxes,
 * add an array_filter value to your form
 *
 */
function system_settings_form_submit($form_id, $values) {...

What does this comment mean? Here's what I think it means. When you are writing the settings hook for a module that can be applied to various node types, you want checkboxes like

page [ ]
story [ ]

But the settings don't save properly because of form problems with checkbox values. So the comment is instructing you to add an array_filter element to your form; it is picked up in the code ca. line 789:

      if (is_array($value) && isset($values['array_filter'])) {
        $value = array_keys(array_filter($value));
      }

After some trial and error, I got the saving of checkbox values for node types to work by defining the form element $form['array_filter'] = array('#type' => 'hidden'); (the #value key is not actually used; it's only checking if the key is set).

So to clarify this cryptic comment, I propose adding an example to the comment as follows:

/**
 * Execute the system_settings_form.
 *
 * If you want node type configure style handling of your checkboxes,
 * add an array_filter value to your form, e.g.,
 * $form['array_filter'] = array('#type' => 'hidden');
 *
 */
function system_settings_form_submit($form_id, $values) {...

chx, is this what the comment means? FYI, It was originally added here.

CommentFileSizeAuthor
settings_example.diff667 bytesjvandyk

Comments

jvandyk’s picture

Status: Active » Reviewed & tested by the community

Talked to chx, he was in agreement.

dries’s picture

Frankly, I don't understand what "node type configure style handling" means, or why it is a special case. Sounds like an ugly hack. While the proposed description is better, it is still too obscure.

dries’s picture

Status: Reviewed & tested by the community » Needs work
robertdouglass’s picture

Why is the check for array_filter there anyway? What bad thing happens if it isn't there?

jvandyk’s picture

Version: x.y.z » 5.x-dev

Assume a basic settings form with checkboxes for node types:

/**
 * Define a module settings form.
 */
function foo_admin_settings() {
  $form['foo_nodetypes'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Users may foo-ize these node types'),
    '#options' => node_get_types('names'),
    '#default_value' => variable_get('foo_nodetypes', array('story')),
    '#description' => t('The foo-izer will be available on the selected node types.'),
);
  $form['array_filter'] = array('#type' => 'hidden');
  return system_settings_form($form);
}

This is a simple checkbox form that stores the results in the Drupal variable foo_nodetypes. With the array_filter line included, the variable stores:

Array
(
    [0] => page
    [1] => story
)

Comment out the array_filter line and the variable stores

Array
(
    [page] => page
    [story] => story
    [blog] => 0
)

Storing the first is preferred because with many nodetypes you're transferring more unneccesary data from the database on each request.

Chill35’s picture

It is an ugly hack.

What's happening to this anyway...?

ragaskar’s picture

Wow. that is a little confusing. Glad this page is the first result for 'array_filter system_settings_form' on google, otherwise I'd probably be scratching my head for another hour over the phrasing of that on the api ref. (as it is, I'm still unsure whether or not i'll need to include a filter).

Is it possible to at least point people here in the interim from that docpage? Might save a couple people some headaches.

dave reid’s picture

marcingy’s picture

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

bumping version

dave reid’s picture

Status: Needs work » Closed (duplicate)