Projet

Général

Profil

Paste
Télécharger (11,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / field_group / field_group.install @ e9f59589

1
<?php
2

    
3
/**
4
 * @file
5
 * Fieldgroup module install file.
6
 */
7

    
8
/**
9
 * Implements hook_schema().
10
 */
11
function field_group_schema() {
12
  $schema['field_group'] = array(
13
    'description' => t('Table that contains field group entries and settings.'),
14

    
15
    // CTools export definitions.
16
    'export' => array(
17
      'key' => 'identifier',
18
      'identifier' => 'field_group',
19
      'default hook' => 'field_group_info',
20
      'save callback' => 'field_group_group_save',
21
      'delete callback' => 'field_group_group_export_delete',
22
      'can disable' => TRUE,
23
      'api' => array(
24
        'owner' => 'field_group',
25
        'api' => 'field_group',
26
        'minimum_version' => 1,
27
        'current_version' => 1,
28
      ),
29
    ),
30

    
31
    'fields' => array(
32
      'id' => array(
33
        'type' => 'serial',
34
        'not null' => TRUE,
35
        'description' => 'The primary identifier for a group',
36
        'no export' => TRUE,
37
      ),
38
      'identifier' => array(
39
        'type' => 'varchar',
40
        'length' => 255,
41
        'not null' => TRUE,
42
        'default' => '',
43
        'description' => 'The unique string identifier for a group.',
44
      ),
45
      'group_name' => array(
46
        'type' => 'varchar',
47
        'length' => 32,
48
        'not null' => TRUE,
49
        'default' => '',
50
        'description' => 'The name of this group.',
51
      ),
52
      'entity_type' => array(
53
        'type' => 'varchar',
54
        'length' => 32,
55
        'not null' => TRUE,
56
        'default' => '',
57
      ),
58
      'bundle' => array(
59
        'type' => 'varchar',
60
        'length' => 128,
61
        'not null' => TRUE,
62
        'default' => ''
63
        ),
64
      'mode' => array(
65
        'type' => 'varchar',
66
        'length' => 128,
67
        'not null' => TRUE,
68
        'default' => ''
69
      ),
70
      // @todo 'parent_name' is redundant with the data in the 'children'
71
      // entry, brings a risk of inconsistent data. This should be removed from
72
      // the schema and pre-computed it if needed in field_group_get_groups().
73
      'parent_name' => array(
74
        'type' => 'varchar',
75
        'length' => 32,
76
        'not null' => TRUE,
77
        'default' => '',
78
        'description' => 'The parent name for a group',
79
      ),
80
      'data' => array(
81
        'type' => 'blob',
82
        'size' => 'big',
83
        'not null' => TRUE,
84
        'serialize' => TRUE,
85
        'description' => 'Serialized data containing the group properties that do not warrant a dedicated column.',
86
      ),
87
    ),
88
    'primary key' => array('id'),
89
    'indexes' => array(
90
      'group_name' => array('group_name'),
91
    ),
92
    'unique keys' => array(
93
      'identifier' => array('identifier'),
94
    ),
95
  );
96
  return $schema;
97
}
98

    
99
/**
100
 * Utility function: fetch all the field_group definitions from the database.
101
 */
102
function _field_group_install_read_groups() {
103
  $groups = array();
104
  if (db_table_exists('content_group')) {
105
    $query = db_select('content_group', 'cg', array('fetch' => PDO::FETCH_ASSOC))
106
      ->fields('cg')
107
      // We only want non-multigroups.
108
      ->condition('group_type', 'standard');
109
    foreach ($query->execute() as $record) {
110
      $record['settings'] = unserialize($record['settings']);
111
      $groups[$record['group_name'] . '-' . $record['type_name']] = $record;
112
    }
113
    foreach ($groups as $key => $group) {
114
      $query2 = db_select('content_group_fields', 'cgf', array('fetch' => PDO::FETCH_ASSOC))
115
        ->fields('cgf')
116
        ->condition('group_name', $group['group_name']);
117
      foreach ($query2->execute() as $field) {
118
        $groups[$field['group_name'] . '-' . $field['type_name']]['children'][] = $field;
119
      }
120
    }
121
  }
122
  return $groups;
123
}
124

    
125
/**
126
 * Implements of hook_install().
127
 *
128
 * Because this is a new module in D7, hook_update_N() doesn't help D6
129
 * users who upgrade to run the migration path. So, we try that here as
130
 * the module is being installed.
131
 */
132
function field_group_install() {
133

    
134
  $groups = _field_group_install_read_groups();
135
  module_load_include('module', 'field_group');
136

    
137
  if (!empty($groups)) {
138

    
139
    module_load_include('module', 'ctools');
140
    ctools_include('export');
141

    
142
    foreach ($groups as $group) {
143

    
144
      $group = (object) $group;
145

    
146
      $new = new stdClass();
147
      $new->group_name = $group->group_name;
148
      $new->entity_type = 'node';
149
      $new->bundle = $group->type_name;
150
      $new->label = $group->label;
151
      $new->parent_name = '';
152
      $new->children = array();
153
      foreach ($group->children as $child) {
154
        $new->children[] = $child['field_name'];
155
      }
156

    
157
      // The form.
158
      $new->id = NULL;
159
      $new->weight = $group->weight;
160
      $new->mode = 'form';
161
      $new->format_type = 'fieldset';
162
      $new->format_settings = array(
163
        'formatter' => preg_match("/fieldset/", $group->settings['form']['style']) ? 'collapsible' : 'collapsed',
164
        'instance_settings' => array(),
165
      );
166
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
167
      ctools_export_crud_save('field_group', $new);
168

    
169
      // The full node.
170
      $new->id = NULL;
171
      $new->weight = $group->weight;
172
      $new->mode = 'default';
173
      $new->format_type = $group->settings['display']['full']['format'];
174
      $new->format_settings = array(
175
        'formatter' => 'collapsible',
176
        'instance_settings' => array(),
177
      );
178
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
179
      ctools_export_crud_save('field_group', $new);
180

    
181
      // The teaser node.
182
      $new->id = NULL;
183
      $new->weight = $group->weight;
184
      $new->mode = 'teaser';
185
      $new->format_type = $group->settings['display']['teaser']['format'];
186
      $new->format_settings = array(
187
        'formatter' => 'collapsible',
188
        'instance_settings' => array(),
189
      );
190
      $new->identifier = $new->group_name . '|' . $new->entity_type . '|' . $new->bundle . '|' . $new->mode;
191
      ctools_export_crud_save('field_group', $new);
192

    
193
    }
194

    
195
  }
196

    
197
  // Set weight to 1.
198
  db_update('system')
199
    ->fields(array('weight' => 1))
200
    ->condition('name', 'field_group')
201
    ->execute();
202

    
203
}
204

    
205
/**
206
 * Update hook on the field_group table to add an unique identifier.
207
 */
208
function field_group_update_7001() {
209

    
210
  if (!db_field_exists('field_group', 'identifier')) {
211
    // Add the new string identifier field for ctools.
212
    db_add_field('field_group', 'identifier', array(
213
      'type' => 'varchar',
214
      'length' => 255,
215
      'not null' => TRUE,
216
      'default' => '',
217
      'description' => 'The unique string identifier for a group.',
218
    ));
219
    // Force drupal's schema to be rebuilt
220
    drupal_get_schema('field_group', TRUE);
221

    
222
    module_load_include('module', 'field_group');
223
    _field_group_recreate_identifiers();
224

    
225
  }
226

    
227
  db_update('system')
228
    ->fields(array('weight' => 1))
229
    ->condition('name', 'field_group')
230
    ->execute();
231

    
232
}
233

    
234
/**
235
 * Update hook to clear cache for new changes to take effect.
236
 */
237
function field_group_update_7002() {
238

    
239
  module_load_include('module', 'field_group');
240

    
241
  // This hook is called to satify people with older version of field_group.
242
  // This will recreate all identifiers for the field_groups known in database.
243
  // At the moment, we only trigger field_groups that are stored in the database, where
244
  // we should maybe get all field_groups as ctools has registered them.
245
  // See http://drupal.org/node/1169146.
246
  // See http://drupal.org/node/1018550.
247
  _field_group_recreate_identifiers();
248

    
249
}
250

    
251
/**
252
 * Update hook to recreate identifiers.
253
 * @see function field_group_update_7002.
254
 */
255
function field_group_update_7003() {
256

    
257
  module_load_include('module', 'field_group');
258
  _field_group_recreate_identifiers();
259

    
260
}
261

    
262
/**
263
 * Update hook to make sure identifier is set as unique key.
264
 */
265
function field_group_update_7004() {
266
  db_drop_unique_key('field_group', 'identifier');
267
  db_add_unique_key('field_group', 'identifier', array('identifier'));
268
}
269

    
270
/**
271
 * Checks all existing groups and removes optional HTML classes
272
 * while adding them as extra classes.
273
 */
274
function field_group_update_7005() {
275

    
276
  // Migrate the field groups so they have a unique identifier.
277
  $result = db_select('field_group', 'fg')
278
    ->fields('fg')
279
    ->execute();
280
  $rows = array();
281
  foreach($result as $row) {
282
    //$row->identifier = $row->group_name . '|' . $row->entity_type . '|' . $row->bundle . '|' . $row->mode;
283
    $row->data = unserialize($row->data);
284
    $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
285
    $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
286
    foreach ($optional_classes as $optional_class) {
287
      if (!in_array($optional_class, $classes)) {
288
        $classes[] = $optional_class;
289
      }
290
    }
291
    $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
292
    $rows[] = $row;
293
  }
294
  foreach ($rows as $row) {
295
    drupal_write_record('field_group', $row, array('id'));
296
  }
297

    
298
}
299

    
300
/**
301
 * Save all optional HTML classes for fieldgroups located in features.
302
 * If you need the optional classes, recreate feature after this update.
303
 * If not, you can revert it.
304
 */
305
function field_group_update_7006() {
306
  ctools_include("export");
307
  // Migrate the field groups so they have a unique identifier.
308
  $field_groups = ctools_export_load_object("field_group");
309
  foreach ($field_groups as $row) {
310

    
311
    // Only update feature field_groups this time.
312
    // Don't touch the fieldgroups in db.
313

    
314
    if ($row->export_type == EXPORT_IN_CODE) {
315
      $classes = array();
316

    
317
      if (isset($row->data['format_settings'], $row->data['format_settings']['instance_settings'], $row->data['format_settings']['instance_settings']['classes'])) {
318
        $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
319
      }
320

    
321
      $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
322
      foreach ($optional_classes as $optional_class) {
323
        if (!in_array($optional_class, $classes)) {
324
          $classes[] = $optional_class;
325
        }
326
      }
327
      $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
328
      unset($row->id);
329
      drupal_write_record('field_group', $row);
330
    }
331
  }
332

    
333
}
334

    
335
/**
336
 * Id attributes are now a setting. This update will insert the old id in the setting so old
337
 * markup doesn't get broken. If you don't want an attribute, you can delete
338
 * them on the fieldgroup settings.
339
 */
340
function field_group_update_7007() {
341

    
342
  ctools_include("export");
343

    
344
  // Migrate the field groups so they have a unique identifier.
345
  $field_groups = ctools_export_load_object("field_group");
346
  foreach ($field_groups as $row) {
347
    if ($row->data['format_type'] == 'div' || $row->data['format_type'] == 'html5' || $row->data['format_type'] == 'html-element') {
348

    
349
      // If mode is default, we don't know what view mode it was. Take full then.
350
      $view_mode = $row->mode == 'default' ? 'full' : $row->mode;
351
      $id = $row->entity_type . '_' . $row->bundle . '_' . $view_mode . '_' . $row->group_name;
352
      $row->data['format_settings']['instance_settings']['id'] = $id;
353
    }
354

    
355
    if ($row->export_type == EXPORT_IN_CODE) {
356
      unset($row->id);
357
      drupal_write_record('field_group', $row);
358
    }
359
    else {
360
      drupal_write_record('field_group', $row, array('id'));
361
    }
362
  }
363
}