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

No comments:

Post a Comment