2

i am working on a responsive D7 theme and need some JS settings to be saved via theme settings form not a custom module. i did following codes to give what i want but it seems that Using drupal_add_js() function to add JS settings in theme_settings.php file doesn't work. could you please point out what is the problem and is there any alternative solution? some of my codes in theme_settings.php file is as followings:

/**
* Implements hook_form_system_theme_settings_alter()
*/
function glossy_form_system_theme_settings_alter(&$form, &$form_state) {
    $theme = alpha_get_theme();

    $lib_options = false;

    /** add mobileMenu options to theme settings */
    if (isset($theme->settings['libraries']['glossy_mobileMenu'])) {
        $lib_options = true;
        $form['alpha_settings']['glossy_lib_options']['mobileMenu'] = array(
            '#type' => 'fieldset',
            '#weight' => -10,
            '#title' => t('mobileMenu options'),
        );

        $form['alpha_settings']['glossy_lib_options']['mobileMenu']['mobileMenu_selector'] = array(
            '#type' => 'textfield',
            '#description' => t('Select the CSS selector of the container that all inner menus will be responsive. e.g. %example', array('%example' => '.navigation')),
            '#title' => t('responsive menu container selector'),
            '#default_value' => theme_get_setting('mobileMenu_selector'),
        );

        $form['alpha_settings']['glossy_lib_options']['mobileMenu']['mobileMenu_title'] = array(
            '#type' => 'textfield',
            '#description' => t('e.g. Select a page'),
            '#title' => t('First option text'),
            '#default_value' => theme_get_setting('mobileMenu_title'),
        );

        $form['alpha_settings']['glossy_lib_options']['mobileMenu']['mobileMenu_indent'] = array(
            '#type' => 'textfield',
            '#description' => t('e.g. !string', array('!string' => '   ')),
            '#title' => t('string for indenting nested items'),
            '#default_value' => theme_get_setting('mobileMenu_indent'),
        );

        // add submit handler
        $form['#submit'][] = 'glossy_mobileMenu_submit';       
    }

    if ($lib_options) {
        $form['alpha_settings']['glossy_lib_options']['#type'] = 'fieldset';
        $form['alpha_settings']['glossy_lib_options']['#weight'] = -10;
        $form['alpha_settings']['glossy_lib_options']['#title'] = t('Libraries options');
    }
}

function glossy_mobileMenu_submit($form, &$form_state){
    // Add the mobileMenu options variables.
    drupal_add_js(array(
        'responsive_menu_options' => array(
            'selector' => $form_state['values']['mobileMenu_selector'],
            'title' => $form_state['values']['mobileMenu_title'],
            'indent' => $form_state['values']['mobileMenu_indent'],
        ),
    ), array('type' => 'setting', 'scope' => JS_DEFAULT));
}

thanks in advance.

1 Answer 1

3

I think you problem is this: drupal_add_js needs to be called at runtime. It's not some kind of registry function that adds your js file for every future page request. In other words, your code only adds the js when the form is being submitted.

Instead of calling drupal_add_js in your submit handler, store a variable using variable_set() to indicate that the js must be added. In your template.php file you can use the _preprocess_page function to retrieve the setting using variable_get() and call drupal_add_js if needed.

1
  • Thanks for your reply, yours was my last solution to overcome that problem.
    – Dropletz
    Dec 4, 2011 at 11:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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