nodeapi_example.install 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the nodeapi_example module.
  5. */
  6. /**
  7. * Implements hook_schema().
  8. *
  9. * @ingroup nodeapi_example
  10. */
  11. function nodeapi_example_schema() {
  12. $schema['nodeapi_example'] = array(
  13. 'description' => 'Stores information of extended content.',
  14. 'fields' => array(
  15. 'nid' => array(
  16. 'description' => 'Node ID that the rating is applied to.',
  17. 'type' => 'int',
  18. 'unsigned' => TRUE,
  19. 'not null' => TRUE,
  20. 'default' => 0,
  21. ),
  22. 'vid' => array(
  23. 'description' => 'Revision ID, as we are tracking rating with node revisions',
  24. 'type' => 'int',
  25. 'unsigned' => TRUE,
  26. 'not null' => TRUE,
  27. 'default' => 0,
  28. ),
  29. 'rating' => array(
  30. 'description' => 'The rating of the node.',
  31. 'type' => 'int',
  32. 'unsigned' => TRUE,
  33. 'not null' => TRUE,
  34. 'default' => 0,
  35. ),
  36. ),
  37. 'primary key' => array('vid'),
  38. 'indexes' => array(
  39. 'nid' => array('nid'),
  40. ),
  41. );
  42. return $schema;
  43. }
  44. /**
  45. * Implements hook_uninstall().
  46. *
  47. * We need to clean up our variables data when uninstalling our module.
  48. *
  49. * Our implementation of nodeapi_example_form_alter() automatically
  50. * creates a nodeapi_example_node_type_<contentType> variable for each node type
  51. * the user wants to rate.
  52. *
  53. * To delete our variables we call variable_del for our variables'
  54. * namespace, 'nodeapi_example_node_type_'. Note that an average module would
  55. * have known variables that it had created, and it could just delete those
  56. * explicitly. For example, see render_example_uninstall(). It's important
  57. * not to delete variables that might be owned by other modules, so normally
  58. * we would just explicitly delete a set of known variables.
  59. *
  60. * hook_uninstall() will only be called when uninstalling a module, not when
  61. * disabling a module. This allows our data to stay in the database if the user
  62. * only disables our module without uninstalling it.
  63. *
  64. * @ingroup nodeapi_example
  65. */
  66. function nodeapi_example_uninstall() {
  67. // Simple DB query to get the names of our variables.
  68. $results = db_select('variable', 'v')
  69. ->fields('v', array('name'))
  70. ->condition('name', 'nodeapi_example_node_type_%', 'LIKE')
  71. ->execute();
  72. // Loop through and delete each of our variables.
  73. foreach ($results as $result) {
  74. variable_del($result->name);
  75. }
  76. }