3

i have a basic admin form, which shows all content types with checkboxes and save button:

function MYMODULE_admin_settings() {
    $types = node_type_get_types();

    $form['content_types'] = array(
        '#type' => 'fieldset',
        '#title' => t('Content types'),
        '#description' => t('test'),
        '#collapsible' => TRUE,
        '#collapsed' => TRUE,
    );
    foreach ($types AS $typename => $type) {
        $form['content_types'][$typename] = array(
            '#type' => 'checkbox',
            '#title' => $type->name,
        );
        }
        $form['submit'] = array(
            '#type' => 'submit',
            '#value' => t('Submit'),
        );
        return $form;

}

When i submit i want to save checked content types somewhere(i don't know what is the best way - save to database or somewhere else like variable_set). And the question is how to show checked content types after submit form - #default_value. For example if i check Article and Basic page c.types submit it and save them to db i want to see them checked after page reload, how can i do this?

7

Most core (and contrib) modules make use of the system_settings_form() function which handily wraps around a form, and saves element values as variables automatically.

function MYMODULE_admin_settings() {
  $types = node_type_get_types();

  $form['content_types'] = array(
    '#type' => 'fieldset',
    '#title' => t('Content types'),
    '#description' => t('test'),
    '#collapsible' => TRUE,
    '#collapsed' => TRUE,
  );
  foreach ($types as $typename => $type) {
    $form['content_types']["MYMODULE_settings_$typename"] = array(
        '#type' => 'checkbox',
        '#title' => $type->name,,
        '#default_value' => variable_get("MYMODULE_settings_$typename", 0)
    );
  }
  $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
  );

  return system_settings_form($form);

}

You can then grab the value anywhere else in code like this (using the 'page' content type as an example):

$val = variable_get('MYMODULE_settings_page');

Which is a bit counter-intuitive. You might want to make use of a checkboxes type instead, e.g.

$options = array();
foreach ($types as $typename => $type) {
  $options[$typename] = $type->name;
}
$form['content_types']['MYMODULE_content_types'] = array(
  '#type' => 'checkboxes',
  '#title' => $type->name,
  '#options' => $options,
  '#default_value' => variable_get('MYMODULE_content_types', array())
);

Then you can get an array of all selected content types like this:

$selected_types = array_filter(variable_get('MYMODULE_content_types', array()));
  • yep, i know about variable_get really forgot about system_settings_form($form); i read about this before, but forgot. This thing with checkboxes really nice. Thanks for answer – Petro Popelyshko Dec 11 '12 at 10:12
  • btw, how can i perform some actions on system_settings_form_submit ? – Petro Popelyshko Dec 11 '12 at 12:43
  • 1
    oh nevermind :D, added $form['#submit'][] = 'MYMODULE_admin_settings_submit'; – Petro Popelyshko Dec 11 '12 at 12:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.