0

I am working on a custom module. I am currently using system_settings_form to enter an API key and it works fine. However, I need users to be able to enter different api keys. What is the best way to do this?

1
  • The variables table shouldn't be used for per-user data so you shouldn't use system_settings_form...just a normal form with a normal table defined in hook_schema() will do the trick – Clive Jun 10 '14 at 16:07
2

You need to create module with .install file that would help you to create custom table where you can store individual user API key. I am sharing with you some code to achieve this. It should be something like that. Rest your code will be same you just need to make some minor changes like use return $form instead of return system_settings_form(). You also need to write hook_form_submit() function.

Module Name: custom 1. custom.install

/**
 * Implements hook_install().
 */
function custom_install() {
    // Create tables.
    drupal_install_schema('custom_table');
}

/**
 * Implements hook_uninstall().
 */
function custom_uninstall() {
    // Drop tables.
    drupal_uninstall_schema('custom_table');
}

/**
 * Implements hook_schema().
 */
function custom_schema() {
  $schema = array();
    $schema['custom_table'] = array(
        'description' => 'Stored user api key',
        'fields' => array(
          'id' => array(
            'type' => 'serial',
            'not null' => TRUE,
            'description' => 'Primary Key: Unique ID.',
          ),
          'api_key' => array(
            'type' => 'varchar',
            'length' => 256,
            'default' => '',
            'not null' => TRUE,
            'description' => 'Store api key',
          ),
        ),  
    'primary key' => array('id'),
  );
  return $schema;
}

custom.module

/* Submit hook function for submission form. */
function custom_form_submit($form, &$form_state) {
  // insert your api key in your custom table using db_insert() function or you can use db_merge() for both insert/update operation.
}
1
  • Thanks alot for getting me headed in the right direction. I came across this in D7 API i thought I would share it: As of Drupal 7, a call to drupal_install_schema() is no longer necessary. See drupal.org/update/modules/6/7: "A module no longer should explicitly install or uninstall its database schema in hook_install() or hook_uninstall()." – Mirakolous Jun 11 '14 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.