It's very easy to create an admin configuration page in Drupal with the help of system_settings_form(), this function adds a Save Configuration button and handle the saving of the variables, you don't have to create a submit function and do a variable_set to save your settings.
This are the functions that we need:
- hook_permission(); // Create your own permission using this function.
- hook_menu(); // Create your own menu using this function.
- drupal_get_form(); // Create your own form.
- your_own_form_function(); // This will be your form name that we will pass on drupal_get_form().
- variable_get(); // This function will get the saved configuration on the database.
- system_settings_form();
- your_own_form_function_validate(); // You can add this if you want to do custom validation for your forms.
(I expect that you already know how to create a .info and .module file, if not then I suggest that you learn that first before proceeding...)
First we need to create a permission so no one can just edit your configuration.
/** * Implements hook_permission(). */ function MODULENAME_permission() { return array( // You can check if a user has an access permission by using user_access('administer your module'), // this will return a boolean value. 'administer your module' => array( 'title' => 'Administer your module', 'description' => 'This is your module permission', ), ) ; }
After we create a permission, we will now create a page. Most of the config path is located at admin/config so it is best to do the same.
/** * Implements hook_menu(). */ function MODULENAME_menu() { $items['admin/config/YourPathHere'] = array( 'title' => t('My Module Config'), 'page callback' => 'drupal_get_form', // This will call drupal_get_form() function and pass in the page arguments. 'page arguments' => array('any_function_name_admin_config'), // This will be the name of your function form. 'access arguments' => array('administer your module'), // This will check if the user have the correct permission. 'file' => 'MODULENAME.admin.inc', // We can put our code in different file to make our code organized. ); return $items; }
If you specify a file path on your hook_menu then this function should be added to that file. Don't forget to add <?php on the first line of your config file.
Visit Form API Reference for the list of properties that you can do with the form.
/** * My Module configuration form. */ function any_function_name_admin_config($form, &$form_state) { $form['mymodule_simple_text'] = array( '#type' => 'textfield', '#title' => t('Text field'), '#description' => t('This is the description of the text field.'), '#required' => TRUE, '#default_value' => variable_get('mymodule_simple_text', ''), ); return system_settings_form($form); }
You can add your own validation by adding _validate to your admin config function name. Use form_set_error to return an error message and stop submitting the form.
/** * Implements hook_form(). */ function any_function_name_admin_config_validate($form, &$form_state) { if (empty($form_state['values']['mymodule_simple_text'])) { form_set_error('mymodule_simple_text', t('Text field cannot be empty.')); } }