I'm new to developing Drupal 7 modules, and I can't figure out why my custom module's form data is not stored in my database's table called 'media_skins
'.
I followed the info explained in Creating modules - a tutorial: Drupal 7.x but I can't get it working. What's wrong with my code?
Here's my code:
function media_skins_menu() {
$items = array();
$items['admin/config/content/media_skins'] = array(
'title' => 'Media Skins',
'description' => 'Configuration for Skins module',
'page callback' => 'drupal_get_form',
'page arguments' => array('media_skins_form'),
'access arguments' => array('access administration pages'),
'type' => MENU_NORMAL_ITEM,
);
return $items;
}
function media_skins_form($form, &$form_state) {
$form['media_skins_test'] = array(
'#type' => 'textfield',
'#title' => t('Test field'),
'#size' => 20,
'#maxlength' => 20,
'#description' => t('Test info for the field.'),
'#required' => FALSE,
);
return system_settings_form($form);
}
function media_skins_form_validate($form, &$form_state) {
// For now, I will no validate anything
}
function media_skins_form_submit($form, &$form_state) {
$form = db_insert('media_skins')->fields(
array(
'start_date' => $form_state['values']['media_skins_test'],
)
)
->execute();
drupal_set_message('Data stored in database successfully');
}
Any help will be very appreciated.
EDITING THE SCENARIO
After reading your comments, I tried the following, but I get "Unexpected error, try it again":
function media_skins_form($form, &$form_state) {
$form['media_skins_test'] = array(
'#type' => 'textfield',
'#title' => t('Test field'),
'#size' => 20,
'#maxlength' => 20,
'#description' => t('Test info for the field.'),
'#required' => FALSE,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Enviar'),
);
return $form;
}
system_settings_form($form)
in the form function, and simply usereturn $form;
. Also check that you are really getting values in$form_state['values']['media_skins_test']
by usingdpm()
(install Devel module) or by doing adrupal_set_message($form_state['values']['media_skins_test']);
in the submit handlersystem_settings_form($form)
creates an automatic submit handler that basically assigns the submitted values to Drupal variables named by the keys of the $form array. In your case, usingsystem_settings_form($form)
assigns the submitted value to the Drupal variablemedia_skins_test
. Drupal variables ARE STORED in the database table calledvariable
. There is no need for you to create a custom handler to store the submitted value in a different table if you are usingsystem_settings_form($form)
system_settings_form($form)
What's worng on it? I can't figure out what's happenning