Tuesday, 26 May 2015

Anatomy of drupal theme settings process

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';
}

In the previous function in the system module mytheme_form_system_theme_settings_alter function is called. The submit and validate function of theme settings are also set up here. 

The default submit and validate function are set in system module, but we can also add our custom submit and validate function in our theme-settings.php file.

Monday, 25 May 2015

submit function in theme-settings.php not found by drupal

reason of this issue

In some themes, it doesn't define $form_state['build_info']['files'][], so in some cases the theme-settings.php file wouldn't be loaded again when the form is submitted. Therefore, in the web page we will get a submit function not found error.

Solution

Add $form_state['build_info']['files'][] definition for every theme in a custom module by using theme hook of hook_form_FORM_ID_alter as the following

/**
 * Implements hook_form_FORM_ID_alter() for system_theme_settings().
 *
 * Workaround for bug https://drupal.org/node/1862892.
 */
function MYMODULE_form_system_theme_settings_alter($form, &$form_state) {
  if ($form['var']['#value'] != 'theme_settings') {
    $key = preg_replace(array('/^theme_/', '/_settings$/'), '', $form['var']['#value']);
    $themes = list_themes();

    if (isset($themes[$key]->base_themes)) {
      $theme_keys = array_keys($themes[$key]->base_themes);
      $theme_keys[] = $key;
    }
    else {
      $theme_keys = array($key);
    }

    foreach ($theme_keys as $theme) {
      $theme_path = drupal_get_path('theme', $theme);
      if (file_exists($theme_path . '/theme-settings.php')) {
        $form_state['build_info']['files'][] = $theme_path . '/theme-settings.php';
      }
    }
  }
}