3

I've been shifting through the drupal documentation and forums but it's all a little daunting. If anyone has a simple or straight forward method for adding fields to the Site information page in the administration section i'd really appreciate it.

As a background, i'm just trying to add user customizable fields site wide fields/values.

Isaac Waller
  • 32,709
  • 29
  • 96
  • 107
Shadi Almosri
  • 11,678
  • 16
  • 58
  • 80

3 Answers3

7

In a custom module, you can use hook_form_alter() to add extra fields to that form. For example:

function mymodule_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'system_site_information_settings') {
    $form['my_module_extra_setting'] = array(
      '#type' => 'checkbox',
      '#title' => t('Use my setting'),
      '#default_value' => variable_get('my_module_extra_setting', TRUE),
    );
  }
}

Anywhere in your code you need access to the saved setting itself, you can use the same call that's used to populate that form element's default value: variable_get('my_module_extra_setting', TRUE)

beth
  • 1,916
  • 4
  • 23
  • 39
Eaton
  • 7,405
  • 2
  • 26
  • 24
  • Right - excuse my noobness :) 1. Is the code you posted - ready to use? can i drop it in and it would work? 2. Where does it go? the template.php file? 3. I've done a workaround - Created a new content type and with CCK created all the variable i need. in the page.tpl.php file i call in all the fields that i had stored in that node - thus now any kind of variable i need easily accessible. Is this bad? I've redirected a link to that page using pathauto to the front page for security (not really required but just in case. – Shadi Almosri Jun 12 '09 at 01:16
  • 1
    As His Eatonness said, this code needs to go into a custom module. Since this function is a hook and starts with my_module, it belongs in a module with that name. Something like sites/all/modules/my_module/my_module.module. You will also need a sites/all/modules/my_module/my_module.info file to be able to enable the module. – mikl Jun 13 '09 at 07:40
2

In order to save the value from your new custom field you will need to add a second submit item to the submit array eg:

$form['#submit'][] = 'misc_system_settings_form_submit';

and then add a function to handle the submission, eg:

function misc_system_settings_form_submit($form_id, $form_values) {
    // Handle saving of custom data here
    variable_set('access_denied_message', $form_values['values']['custom_access_denied_message']);
}
Felix Eve
  • 3,811
  • 3
  • 40
  • 50
1

The function should be mymodule_form_alter instead of mymodule_hook_form_alter

Saggy
  • 11
  • 2