Hi - I am tasked with quickly knocking up a Drupal module for a client. the module is supposed to provide centralized control of error messages across the site. This is not something I've done before but I'm an experienced PHP dev.

Anyway, perhaps there's something I'm deeply misunderstanding here, but I can't find any standard way to make my module store it's data in custom tables. I've seen plenty of tutorials on the _creation_ of these tables in the module.install file but nothing on actually _using_ them... here's my install file:

 function universal_errors_schema() {
  $schema['errors_table'] = array(
    'description' => t('All error messages are stored here.'),
    'fields' => array(
      'eid' => array(
        'description' => t('ID key.'),
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE),
      'name' => array(
        'description' => t('The short name.'),
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => ''),
      'description' => array(
        'description' => t('The description of the error type.'),
        'type' => 'varchar',
        'length' => 1024,
        'not null' => TRUE,
        'default' => ''),
      'text' => array(
        'description' => t('The actual text of the error.'),
        'type' => 'varchar',
        'length' => 1024,
        'not null' => TRUE,
        'default' => ''
	  ),
    ),
    'primary key' => array('eid'),
  );
  return $schema;
}

function universal_errors_install() {
  // create tables
  drupal_install_schema('errors_table');
}

function universal_errors_uninstall() {
  // drop tables
  drupal_uninstall_schema('errors_table');
}

Which I assume is fine, but then how do I save my settings in these tables? Here is my module file:

function universal_errors_perm() {
  return array('access universal_errors content');
}

function universal_errors_admin() {
	
	$form = array();
	
	// set 'name' errors
	$form['universal_errors_name_length'] = array(
		'#type' => 'textfield',
		'#title' => t('Name'),
		'#default_value' => variable_get('universal_errors_name', 'Name is too long'),
		'#size' => 30,
		'#maxlength' => 255,
		'#description' => t("Error when a name field is too long."),
		'#required' => TRUE,
	);
	
	return system_settings_form($form);
	
}

function universal_errors_menu() {

  $items = array();

  $items['admin/settings/universal_errors'] = array(
    'title' => 'Universal errors',
    'description' => 'Set error messages for all webforms here',
    'page callback' => 'drupal_get_form',
		// ref admin function above
    'page arguments' => array('universal_errors_admin'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
   );

  return $items;

}

I assume I need to define a callback function for the settings form which references the table names but I haven't seen any examples of this. Currently it appears to store the variable using some kind of behind the scenes magic...

Any help is much appreciated.

Cheers,

J.R.

Comments

jaypan’s picture

When you use system_settings_form(), there is no submit function for the form. What happens is that all the form elements are automatically saved to the variables table in the database.

This page explains it.

Contact me to contract me for D7 -> D10/11 migrations.