Projet

Général

Profil

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

root / drupal7 / sites / all / modules / field_group / field_group.install @ 59ae487e

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
      'load callback' => 'field_group_group_load_by_identifier',
21
      'save callback' => 'field_group_group_save',
22
      'delete callback' => 'field_group_group_export_delete',
23
      'can disable' => TRUE,
24
      'api' => array(
25
        'owner' => 'field_group',
26
        'api' => 'field_group',
27
        'minimum_version' => 1,
28
        'current_version' => 1,
29
      ),
30
    ),
31

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

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

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

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

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

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

    
143
    foreach ($groups as $group) {
144

    
145
      $group = (object) $group;
146

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

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

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

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

    
194
    }
195

    
196
  }
197

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

    
204
}
205

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

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

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

    
226
  }
227

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

    
233
  // Clear drupal and static cache.
234
  field_group_info_groups(NULL, NULL, NULL, TRUE);
235
}
236

    
237
/**
238
 * Update hook to clear cache for new changes to take effect.
239
 */
240
function field_group_update_7002() {
241

    
242
  module_load_include('module', 'field_group');
243

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

    
252
  // Clear drupal and static cache.
253
  field_group_info_groups(NULL, NULL, NULL, TRUE);
254
}
255

    
256
/**
257
 * Update hook to recreate identifiers.
258
 * @see function field_group_update_7002.
259
 */
260
function field_group_update_7003() {
261

    
262
  module_load_include('module', 'field_group');
263
  _field_group_recreate_identifiers();
264

    
265
  // Clear drupal and static cache.
266
  field_group_info_groups(NULL, NULL, NULL, TRUE);
267
}
268

    
269
/**
270
 * Update hook to make sure identifier is set as unique key.
271
 */
272
function field_group_update_7004() {
273
  db_drop_unique_key('field_group', 'identifier');
274
  db_add_unique_key('field_group', 'identifier', array('identifier'));
275
}
276

    
277
/**
278
 * Checks all existing groups and removes optional HTML classes
279
 * while adding them as extra classes.
280
 */
281
function field_group_update_7005() {
282

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

    
305
  // Clear drupal and static cache.
306
  field_group_info_groups(NULL, NULL, NULL, TRUE);
307
}
308

    
309
/**
310
 * Save all optional HTML classes for fieldgroups located in features.
311
 * If you need the optional classes, recreate feature after this update.
312
 * If not, you can revert it.
313
 */
314
function field_group_update_7006() {
315
  ctools_include("export");
316
  // Migrate the field groups so they have a unique identifier.
317
  $field_groups = ctools_export_load_object("field_group");
318
  foreach ($field_groups as $row) {
319

    
320
    // Only update feature field_groups this time.
321
    // Don't touch the fieldgroups in db.
322

    
323
    if ($row->export_type == EXPORT_IN_CODE) {
324
      $classes = array();
325

    
326
      if (isset($row->data['format_settings'], $row->data['format_settings']['instance_settings'], $row->data['format_settings']['instance_settings']['classes'])) {
327
        $classes = explode(" ", $row->data['format_settings']['instance_settings']['classes']);
328
      }
329

    
330
      $optional_classes = array(str_replace("_", "-", $row->group_name), 'field-group-' . $row->data['format_type']);
331
      foreach ($optional_classes as $optional_class) {
332
        if (!in_array($optional_class, $classes)) {
333
          $classes[] = $optional_class;
334
        }
335
      }
336
      $row->data['format_settings']['instance_settings']['classes'] = implode(" ", $classes);
337
      unset($row->id);
338
      drupal_write_record('field_group', $row);
339
    }
340
  }
341

    
342
  // Clear drupal and static cache.
343
  field_group_info_groups(NULL, NULL, NULL, TRUE);
344
}
345

    
346
/**
347
 * Id attributes are now a setting. This update will insert the old id in the setting so old
348
 * markup doesn't get broken. If you don't want an attribute, you can delete
349
 * them on the fieldgroup settings.
350
 */
351
function field_group_update_7007() {
352

    
353
  ctools_include("export");
354

    
355
  // Migrate the field groups so they have a unique identifier.
356
  $field_groups = ctools_export_load_object("field_group");
357
  foreach ($field_groups as $row) {
358
    if ($row->data['format_type'] == 'div' || $row->data['format_type'] == 'html5' || $row->data['format_type'] == 'html-element') {
359

    
360
      // If mode is default, we don't know what view mode it was. Take full then.
361
      $view_mode = $row->mode == 'default' ? 'full' : $row->mode;
362
      $id = $row->entity_type . '_' . $row->bundle . '_' . $view_mode . '_' . $row->group_name;
363
      $row->data['format_settings']['instance_settings']['id'] = $id;
364
    }
365

    
366
    if ($row->export_type == EXPORT_IN_CODE) {
367
      unset($row->id);
368
      drupal_write_record('field_group', $row);
369
    }
370
    else {
371
      drupal_write_record('field_group', $row, array('id'));
372
    }
373
  }
374

    
375
  // Clear drupal and static cache.
376
  field_group_info_groups(NULL, NULL, NULL, TRUE);
377
}
378

    
379
/**
380
 * Clear cache to notice the CTools load callback.
381
 */
382
function field_group_update_7008() {
383
  drupal_flush_all_caches();
384
}