block_class.install 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. /**
  3. * @file
  4. * Install, update and uninstall functions for the block_class module.
  5. */
  6. /**
  7. * Implements hook_install().
  8. */
  9. function block_class_install() {
  10. $schema['block'] = array();
  11. block_class_schema_alter($schema);
  12. foreach ($schema['block']['fields'] as $field => $spec) {
  13. if (db_field_exists('block', $field)) {
  14. watchdog('system', 'Module install: Attempt to recreate field: "%field", when it already exists.', array('%field' => $field), WATCHDOG_WARNING);
  15. }
  16. else {
  17. db_add_field('block', $field, $spec);
  18. }
  19. }
  20. }
  21. /**
  22. * Implements hook_uninstall().
  23. */
  24. function block_class_uninstall() {
  25. $schema['block'] = array();
  26. block_class_schema_alter($schema);
  27. foreach ($schema['block']['fields'] as $field => $specs) {
  28. db_drop_field('block', $field);
  29. }
  30. }
  31. /**
  32. * Implements hook_schema_alter().
  33. *
  34. * Other modules, such as i18n_block also modify the block database table.
  35. */
  36. function block_class_schema_alter(&$schema) {
  37. if (isset($schema['block'])) {
  38. $schema['block']['fields']['css_class'] = array(
  39. 'type' => 'varchar',
  40. 'length' => 255,
  41. 'not null' => TRUE,
  42. 'default' => '',
  43. 'description' => 'String containing the classes for the block.',
  44. );
  45. }
  46. }
  47. /**
  48. * Alters the structure of {block_class} schema.
  49. */
  50. function block_class_update_7100() {
  51. // Check if the block_class table exists to prevent installation profiles
  52. // from running this update for versions without the block_class table.
  53. if (db_table_exists('block_class')) {
  54. // Update the schema.
  55. db_drop_primary_key('block_class');
  56. db_change_field('block_class', 'module', 'module',
  57. array(
  58. 'type' => 'varchar',
  59. 'length' => '64',
  60. 'not null' => TRUE,
  61. 'default' => '',
  62. 'description' => 'The module to which the block belongs.',
  63. )
  64. );
  65. db_change_field('block_class', 'delta', 'delta',
  66. array(
  67. 'type' => 'varchar',
  68. 'length' => '32',
  69. 'not null' => TRUE,
  70. 'default' => '',
  71. 'description' => "The ID of the module's block.",
  72. )
  73. );
  74. db_change_field('block_class', 'css_class', 'css_class',
  75. array(
  76. 'type' => 'varchar',
  77. 'length' => '255',
  78. 'not null' => TRUE,
  79. 'default' => '',
  80. 'description' => 'String containing the classes for the block.',
  81. )
  82. );
  83. // Restore the primary key.
  84. db_add_primary_key('block_class', array('module', 'delta'));
  85. }
  86. }
  87. /**
  88. * Fix too long primary key length in {block_class}.
  89. */
  90. function block_class_update_7101() {
  91. // Ensure the block_class table exists, which is not true for all versions.
  92. if (db_table_exists('block_class')) {
  93. // Drop current primary key.
  94. db_drop_primary_key('block_class');
  95. db_change_field('block_class', 'module', 'module', array(
  96. 'type' => 'varchar',
  97. 'length' => 64,
  98. 'not null' => TRUE,
  99. 'default' => '',
  100. 'description' => 'The module to which the block belongs.',
  101. ));
  102. db_change_field('block_class', 'delta', 'delta', array(
  103. 'type' => 'varchar',
  104. 'length' => 32,
  105. 'not null' => TRUE,
  106. 'default' => '',
  107. 'description' => "The ID of the module's block.",
  108. ));
  109. // Create new primary key.
  110. db_add_primary_key('block_class', array('module', 'delta'));
  111. }
  112. }
  113. /**
  114. * Migration from block_class table to new field css_class in core block table.
  115. */
  116. function block_class_update_7103() {
  117. if (!db_field_exists('block', 'block_class')) {
  118. $schema['block'] = array();
  119. block_class_schema_alter($schema);
  120. foreach ($schema['block']['fields'] as $field => $specs) {
  121. db_add_field('block', $field, $specs);
  122. }
  123. }
  124. if (db_table_exists('block_class')) {
  125. // Migrate all existing records from block_class table to block table.
  126. $result = db_query('SELECT css_class, module, delta FROM {block_class}');
  127. while ($record = $result->fetchObject()) {
  128. db_update('block')
  129. ->fields(array('css_class' => $record->css_class))
  130. ->condition('module', $record->module)
  131. ->condition('delta', $record->delta)
  132. ->execute();
  133. }
  134. // Remove the block_class table.
  135. db_drop_table('block_class');
  136. }
  137. }