MYTHEME/theme-settings.php: THEME_form_system_theme_settings_alter() builds an input form for /admin/appearance/THEME and the input (values) of the user will not be known by Drupal nor by theme_get_settings() before saving the user given input.

I'm kind of curious how to pass this value to THEME_form_system_theme_settings_submit() in theme-settings.php?

In other words: I need the value from the user input of the theme settings form BEFORE we leave theme-settings.php - Is that possible? Or do I need another way around this?

Test: Let's say the value gets stored in a 'textfield', build by the THEME_form_system_theme_settings_alter() function:

// theme-settings.php
function THEME_form_system_theme_settings_alter(&$form, &$form_state) {
// Test form      
$form['whatever_fieldset_group']['user_input'] = array(
    '#type' => 'textfield',
    '#title' => t('Test to get value'),
    '#default_value' => theme_get_setting('user_input'),
    '#description' => t('
         Type some text in textfield 
         to get shown in submit message'),
);
}

To test if I can pass that value to THEME_form_system_theme_settings_submit() we try to show the value of this field in a submit message:

// theme-settings.php
function THEME_form_system_theme_settings_submit($form, &$form_state) {

    // the following will not work inside theme-settings.php
    // because value is not known yet by theme_get_setting()
    $data = theme_get_setting('user_input');

    drupal_set_message(
          'For testing purposes we submit that you have 
           filled the textfield with the following 
           mindless value: '.$data
          );
}

This will not work. Of course. And variable_get() and variabel_set() seems to be no option ether, because we can not fill any variable without knowing the value in theme-settings.php.

The reason for asking this is, I want to write css from user input in a css file while saving theme settings. Maybe there is a way to fire a function after theme-settings got saved? Not sure. Maybe I need more examples of custom themes and how they solved it there without an additional module ...

  • Welcome to Drupal Answers :) it's very difficult to follow what you're saying, and I fear you might not get any answers as people simply won't know what you're asking. I might be wrong, but making the question shorter and directly to the point would probably help to clarify. You should be able to distill what you're trying to do into a single problem statement (your end goal essentially), and then build on that with the details. Like I said though, I might be wrong and it might just be me that's struggling to understand – Clive Jun 23 '13 at 11:57
  • 1
    Clive, thank you very much for you help. I will try to make the question more clear. – nilsun Jun 23 '13 at 12:01
  • 1
    The more I think about my question, the more I come to the conclusion that it is impossible to do that. The reason for asking this is, I want to write css in a css file after saving theme settings. Maybe there is a way to fire a function after theme-settings got saved? Not sure. Maybe I need more examples of custom themes and how they solved it there without an additional module ... (should put that in the question) – nilsun Jun 23 '13 at 12:43
  • 2
    You're right that there's a race condition there, but don't forget the form processing that saves the theme settings uses what's been submitted in the form itself...and you can get those values easily in the submit handler: $data = $form_state['values']['user_input'];. – Clive Jun 23 '13 at 12:52
  • 2
    Clive and Jack-PL! Thank you sooo much!! That was the cue I needed! That's exactly what I was looking for! OMG. I can't believe that I finally have a way to put the needle on the record! How can I thank you for that. Please let me know. I will report back the working code for followers in the next minutes. – nilsun Jun 23 '13 at 13:10
up vote 0 down vote accepted

The answer to the question is quite simple, but not easy to find in the d.o. docs. Thanks to Clive and Jack-PL who gave me the right direction. To reuse the example from the start question we need to summarize:

A form field input value needs to get splitt off (intercepted) from its usual way getting stored for a short moment. Why? For us to do our own processing while submission before theme-settings.php Party is over. Our form field:

// theme-settings.php
function THEME_form_system_theme_settings_alter(&$form, &$form_state) {
// Test form      
$form['whatever_fieldset_group']['user_input'] = array(
    '#type' => 'textfield',
    '#title' => t('Test to get value'),
    '#default_value' => theme_get_setting('user_input'),
    '#description' => t('
         Type some text in textfield 
         to get shown in submit message'),
);
}

Now the value will get stored when the user hits submit button. But what if we need to get in between before?

Injecting ...

drupal_set_message('<pre>' . print_r($form_state['input'],1));

... in function THEME_form_system_theme_settings_submit for a moment has revealed, that we were looking for $form_state['input']['user_input'], were our fruits are hanging. Searching the value with drupal_set_message('<pre>' . print_r($form_state['input'],1)); gives us orientation to be able to intercept the right value for our own value processing, demonstrated here in a simple representational form with another little drupal_set_message() scenario:

// theme-settings.php
function THEME_form_system_theme_settings_submit($form, &$form_state) {

    // the following will not work inside theme-settings.php
    // because value is not known yet by theme_get_setting()
    // $data = theme_get_setting('user_input');

    // But this works!
    $data = $form_state['input']['user_input'];

    drupal_set_message(
          'For testing purposes we submit that you have 
           filled the textfield with the following 
           mindless value: '. $data
          );
}

In my case, I was able to save user input into a file now with file_unmanaged_save_data($form_state['input']['user_input'],$filepath); while the user hits submit.

Thanks to Clive and Jack-PL for the fast reply. It helped me a lot!

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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