As we know it's very easy to implement the theme settings in a theme as below
First, we need to create a file called theme-settings.php under the theme root folder.Second, we implement drupal hook named hook_form_system_theme_settings_alter by creating a function named MYTHEME__form_system_theme_settings_alter and add custom drupal form elements in this function. Here is a sample below:
function mytheme_form_system_theme_settings_alter( &$form, &$form_state )
At last, if we need custom submit function or reset function, we just need to explicit the submit or reset function in the form elements defined under the hook function at step 2, and create the associated submit or reset function. The following is a sample snippet:
/* file theme-settings.php*/
function mytheme_form_system_theme_settings_alter( &$form, &$form_state ){
/* Submit function */
$form['#submit'][] = 'mytheme_form_system_theme_settings_submit';
/* Add reset button */
$form['actions']['reset'] = array(
'#submit' => array( 'mytheme_form_system_theme_settings_reset' ),
'#type' => 'submit',
'#value' => t( 'Reset to defaults' ),
);
//some other custom form code
}
function mytheme_form_system_theme_settings_submit( $form, &$form_state ){
//custom submit code
}
function mytheme_form_system_theme_settings_reset( $form, &$form_state ){
//custom reset code
}
When drupal display the settings page in the admin panel, it starts from the admin menu definition in system module. Here is the snippet defining the menu.
/* in the file modules/system/system.module at line 646*/foreach (list_themes() as $theme) {
$items['admin/appearance/settings/' . $theme->name] = array(
'title' => $theme->info['name'],
'page arguments' => array('system_theme_settings', $theme->name),
'type' => MENU_LOCAL_TASK,
'access callback' => '_system_themes_access',
'access arguments' => array($theme),
'file' => 'system.admin.inc',
);
}
From the aforementioned snippet, we can get that system_theme_settings is called to generate the form with the theme name passed.
/* in modules/system/system.admin.inc at line 399 */function system_theme_settings($form, &$form_state, $key = '') {
//some code
//at line 572
// Process the theme and all its base themes.
foreach ($theme_keys as $theme) {
// Include the theme-settings.php file.
$filename = DRUPAL_ROOT . '/' . str_replace("/$theme.info", '', $themes[$theme]->filename) . '/theme-settings.php';
if (file_exists($filename)) {
require_once $filename;
}
// Call theme-specific settings.
$function = $theme . '_form_system_theme_settings_alter';
if (function_exists($function)) {
$function($form, $form_state);
}
}
//some code
//at line 597
// We don't want to call system_settings_form_submit(), so change #submit.
array_pop($form['#submit']);
$form['#submit'][] = 'system_theme_settings_submit';
$form['#validate'][] = 'system_theme_settings_validate';
}