Project

General

Profile

Paste
Download (4.12 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / block_class / block_class.install @ ecb492a9

1
<?php
2

    
3
/**
4
 * @file
5
 * Install, update and uninstall functions for the block_class module.
6
 */
7

    
8
/**
9
 * Implements hook_install().
10
 */
11
function block_class_install() {
12
  $schema['block'] = array();
13
  block_class_schema_alter($schema);
14
  foreach ($schema['block']['fields'] as $field => $spec) {
15
    if (db_field_exists('block', $field)) {
16
      watchdog('system', 'Module install: Attempt to recreate field: "%field", when it already exists.', array('%field' => $field), WATCHDOG_WARNING);
17
    }
18
    else {
19
      db_add_field('block', $field, $spec);
20
    }
21
  }
22
}
23

    
24
/**
25
 * Implements hook_uninstall().
26
 */
27
function block_class_uninstall() {
28
  $schema['block'] = array();
29
  block_class_schema_alter($schema);
30
  foreach ($schema['block']['fields'] as $field => $specs) {
31
    db_drop_field('block', $field);
32
  }
33
}
34

    
35
/**
36
 * Implements hook_schema_alter().
37
 *
38
 * Other modules, such as i18n_block also modify the block database table.
39
 */
40
function block_class_schema_alter(&$schema) {
41
  if (isset($schema['block'])) {
42
    $schema['block']['fields']['css_class'] = array(
43
      'type' => 'varchar',
44
      'length' => 255,
45
      'not null' => TRUE,
46
      'default' => '',
47
      'description' => 'String containing the classes for the block.',
48
    );
49
  }
50
}
51

    
52
/**
53
 * Alters the structure of {block_class} schema.
54
 */
55
function block_class_update_7100() {
56
  // Check if the block_class table exists to prevent installation profiles
57
  // from running this update for versions without the block_class table.
58
  if (db_table_exists('block_class')) {
59
    // Update the schema.
60
    db_drop_primary_key('block_class');
61

    
62
    db_change_field('block_class', 'module', 'module',
63
      array(
64
        'type' => 'varchar',
65
        'length' => '64',
66
        'not null' => TRUE,
67
        'default' => '',
68
        'description' => 'The module to which the block belongs.',
69
      )
70
    );
71

    
72
    db_change_field('block_class', 'delta', 'delta',
73
      array(
74
        'type' => 'varchar',
75
        'length' => '32',
76
        'not null' => TRUE,
77
        'default' => '',
78
        'description' => "The ID of the module's block.",
79
      )
80
    );
81

    
82
    db_change_field('block_class', 'css_class', 'css_class',
83
      array(
84
        'type' => 'varchar',
85
        'length' => '255',
86
        'not null' => TRUE,
87
        'default' => '',
88
        'description' => 'String containing the classes for the block.',
89
      )
90
    );
91

    
92
    // Restore the primary key.
93
    db_add_primary_key('block_class', array('module', 'delta'));
94
  }
95
}
96

    
97
/**
98
 * Fix too long primary key length in {block_class}.
99
 */
100
function block_class_update_7101() {
101
  // Ensure the block_class table exists, which is not true for all versions.
102
  if (db_table_exists('block_class')) {
103
    // Drop current primary key.
104
    db_drop_primary_key('block_class');
105

    
106
    db_change_field('block_class', 'module', 'module', array(
107
      'type' => 'varchar',
108
      'length' => 64,
109
      'not null' => TRUE,
110
      'default' => '',
111
      'description' => 'The module to which the block belongs.',
112
    ));
113
    db_change_field('block_class', 'delta', 'delta', array(
114
      'type' => 'varchar',
115
      'length' => 32,
116
      'not null' => TRUE,
117
      'default' => '',
118
      'description' => "The ID of the module's block.",
119
    ));
120

    
121
    // Create new primary key.
122
    db_add_primary_key('block_class', array('module', 'delta'));
123
  }
124
}
125

    
126
/**
127
 * Migration from block_class table to new field css_class in core block table.
128
 */
129
function block_class_update_7103() {
130
  if (!db_field_exists('block', 'block_class')) {
131
    $schema['block'] = array();
132
    block_class_schema_alter($schema);
133
    foreach ($schema['block']['fields'] as $field => $specs) {
134
      db_add_field('block', $field, $specs);
135
    }
136
  }
137

    
138
  if (db_table_exists('block_class')) {
139
    // Migrate all existing records from block_class table to block table.
140
    $result = db_query('SELECT css_class, module, delta FROM {block_class}');
141
    while ($record = $result->fetchObject()) {
142
      db_update('block')
143
        ->fields(array('css_class' => $record->css_class))
144
        ->condition('module', $record->module)
145
        ->condition('delta', $record->delta)
146
        ->execute();
147
    }
148
    // Remove the block_class table.
149
    db_drop_table('block_class');
150
  }
151
}