2

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;
}
5
  • Try getting rid of system_settings_form($form) in the form function, and simply use return $form;. Also check that you are really getting values in $form_state['values']['media_skins_test'] by using dpm() (install Devel module) or by doing a drupal_set_message($form_state['values']['media_skins_test']); in the submit handler
    – argiepiano
    Commented Dec 29, 2015 at 1:25
  • A couple clarifications about configuration forms. Using system_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, using system_settings_form($form) assigns the submitted value to the Drupal variable media_skins_test. Drupal variables ARE STORED in the database table called variable. There is no need for you to create a custom handler to store the submitted value in a different table if you are using system_settings_form($form)
    – argiepiano
    Commented Dec 29, 2015 at 1:57
  • also create a file "media_skins.install" in your custom module
    – user52318
    Commented Dec 29, 2015 at 7:26
  • try this :'start_date' => $form_state['input']['media_skins_test'],
    – user52318
    Commented Dec 29, 2015 at 7:26
  • Please check out my edited version, getting rid of system_settings_form($form) What's worng on it? I can't figure out what's happenning
    – donbuche
    Commented Dec 29, 2015 at 8:58

2 Answers 2

0

To use your own form submit handler, you should not use system_settings_form in your form, instead just return your form.

If you are just going to save your value in your submit handler, go with system_settings_form. To get the values, use variable_get('your form field name'). In your case, you can use variable_get('media_skins_test');

4
  • Yeah, but, where I shall to use variable_get? In the submit form? Instead 'start_date' => $form_state['input']['media_skins_test'] maybe?
    – donbuche
    Commented Dec 29, 2015 at 8:53
  • Are you going to do any custom functionality in your submit handler?
    – Manikandan
    Commented Dec 29, 2015 at 9:10
  • I only want to store on a db table the data retrieved from the inputs. Nothing else. I don't know if it can be called 'functionality'.
    – donbuche
    Commented Dec 29, 2015 at 14:31
  • okay. I seen your edited question. Please post your recent log error message at (admin/reports/dblog) when you tried to submit your form.
    – Manikandan
    Commented Dec 30, 2015 at 5:04
0

You have to variable_get like below.Now you can get the values why because it will store in the variable table.

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,
    '#default_value' => variable_get('media_skins_test', 10),
  );
  return system_settings_form($form);
}

function media_skins_form_submit($form, $form_state) {
    variable_set('media_skins_test', $edit['demo_number']);

}

in devel/php you can see the results of the textfield

$test = variable_get('media_skins_test'); dpm($test);

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.