README.txt 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. Drupal module: Variable API
  2. ===========================
  3. Variable module will provide a registry for meta-data about Drupal variables.
  4. Module Developers: Please declare your variables.
  5. Why?
  6. ====
  7. - So other modules can know about your module's variables and they can be translated, exported, etc.
  8. - You'll get automatic variable edit forms, tokens, access control and uninstall for free.
  9. How?
  10. ====
  11. Easy: Implement hook_variable_info();
  12. /**
  13. * Implements hook_variable_info().
  14. */
  15. function mymodule_variable_info($options) {
  16. $variable['mymodule_number'] = array(
  17. 'title' => t('Magic number', array(), $options),
  18. 'description' => t('Magic number, array(), $options),
  19. 'type' => 'number',
  20. 'access' => 'administer menus',
  21. );
  22. $variable['mymodule_name'] = array(
  23. 'title' => t('Name', array(), $options),
  24. 'description' => t('Enter your name, please.', array(), $options),
  25. 'type' => 'string',
  26. 'default' => t('Drupal user', array(), $options),
  27. );
  28. $variable['mymodule_mail'] = array(
  29. 'title' => t('Mail'),
  30. 'type' => 'mail_text',
  31. // This type will spawn into two real variables: mymodule_mail_subject, mymodule_mail_body
  32. // Everything, included the form elements, will be handled automatically
  33. );
  34. return $variable;
  35. }
  36. Note: You can have your variables declared in a separate file that just will be loaded when needed.
  37. yourmodule.variable.inc
  38. FAQ
  39. ===
  40. - Will I need to add a dependency on the variable.module?
  41. Not neccessarily. Just if you want to enjoy some of the module's features advanced features like:
  42. - Getting variable values or defaults in different languages. Use variable_get_value().
  43. - Let other modules alter my variable defaults. Implement hook_variable_info_alter().
  44. - Let other modules know when variables are changed. Use variable_set_value(). Implement hook_variable_update().
  45. - Getting automatic forms for all the module's variables, a group of variables, etc..
  46. - Having variables with multiple values handled automatically like mail body and subject or variables for node types.
  47. Otherwise you can just provide the meta-data for other modules to use. You still get:
  48. - Tokens for your variables like [variable:myvariable_name]
  49. - Variables deleted automatically when the module is uninstalled
  50. - Localizable texts for your variables when using the Internationalization module.
  51. - How do I get a form with all of my module's variables?
  52. drupal_get_form('variable_module_form', 'mymodule');
  53. - Once I have declared a default for my variable, how can I benefit from it?
  54. variable_get_value('variable_name');
  55. - What if I don't want to provide any administration form for my variables?
  56. That's ok, people will still be able to see and edit them by enabling the 'Variable Admin' module included.