2

I want to create a custom form with a few textfields and then use variable_set to save those textfields as variables.

I am not able to save the variable with the code below.

Question: I do not see the drupal message on hitting submit and when I go to the 'variables' table, I do not see the variable 'id_event'. I have tried clearing all caches several times.

Code below is from *.module file of my custom module "mymodule"

    function mymodule_menu() {

     $items['example_form'] = array(
         'title'        =>  'Progress Numbers',
         'description'  =>  'Form to enter variables that will be shown on Progress Update Block on Front Page',
         'page callback'    => 'drupal_get_form',
         'page arguments'   => array('example_form_form1'),
         'access arguments' => array('access administration menu'),

     );

     return $items;
 }

The callback function:

 function example_form_form1($form, &$form_state) {
  $form['id_event'] = array(
    '#type' => 'textfield',
    '#title' => t('Id Event'),
    '#required' => TRUE,
    '#default_value' => variable_get('id_event', 7),
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
  );
  return $form;
}

Submit Function

function example_form_form1_submit(&$form, &$form_state) {
  variable_set('id_event', $form_state['values']['id_event']);
  drupal_set_message($id_event);
}
1
  • 1
    If you are using devel...then can you please dpm($form_state['values']['id_event']); & see whats it returning here in submit function ?
    – RajeevK
    Mar 8, 2013 at 10:16

4 Answers 4

0

I have tried the solution and my result was excelent!

function felicitacion_mensaje_form($form, &$form_state) {
$form = array();
$form['felicitacion'] = array(
'#type' => 'textfield',
'#title' => t('Mensaje de felicitacion'),
'#default_value' => variable_get('congrat','Feliz cumpleaños !nombre te deseamos!.'),
'#description'   => t("Some description"),
);
return $form;
//   return system_settings_form($form);
}
function felicitacion_mensaje_form_submit(&$form, &$form_state) {
variable_set('congrat', $form_state['values']['felicitacion']);
$id_event = variable_get('congrat', '');
drupal_set_message($id_event);
}
4

Modify your example_form_form1_submit function as follows

function example_form_form1_submit(&$form, &$form_state) {
  variable_set('id_event', $form_state['values']['id_event']);
  $id_event = variable_get('id_event', '');
  drupal_set_message($id_event);
}

enter image description here

1
  • It was useful to me
    – mjimcua
    Sep 2, 2014 at 14:36
0

Drupal's variable API allows for storage of persistent variables across separate requests.

If you want to set a Drupal variable, you need to use variable_set().

Example:

$my_variable = 'very_important_value';          // Here we set a php variable

variable_set('my_variable_name', $my_variable); // Here we store the php variable
                                                // into a Drupal variable

If you want to retrieve the Drupal variable's value, you need to use variable_get().

$saved_value = variable_get('my_variable_name', 'default_value'); // Get the value of the
                                                                  // Drupal variable. If
                                                                  // it does not exist, then
                                                                  // use the second argument

You are seeing the value stored correctly in the database because you are making use of variable_set() correctly. However, when you are trying to retrieve the value, you need to be sure to call variable_get() to retrieve the value and store it into a php variable for further usage.

Suggestion:

function example_form_form1_submit(&$form, &$form_state) {

  variable_set('id_event', $form_state['values']['id_event']); // Stores the value

  $saved_value = variable_get('id_event', 'Default');          // Retrieve the stored value.
                                                               // If it is not saved,
                                                               // $saved_value will be set
                                                               // to 'Default'.
  drupal_set_message($saved_value);
}
0

I resolved my problem using "system_settings_form($form);" since the solutions above did not work for me. (Will try later and if it works, will post any learnings here).

Following is what worked for me for reference:

My .module file:

function neil_menu() {

$items['admin/settings/neil'] = array(
    'title' => 'Node images',
    'description' => 'Control how to upload node images.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('neil_admin_settings'),
    'access arguments' => array('administer site configuration'),
    'file' => 'neil.admin.inc',
);
 return $items;

}

neil.admin.inc file:

function neil_admin_settings() {
   $form['progress_so_far'] = array(
'#type' => 'textfield',
'#title' => t('Progress So Far'),
'#default_value' => variable_get('progress_so_far', '0'),
'#maxlength' => 255,
);

return system_settings_form($form);
}

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.