-
Notifications
You must be signed in to change notification settings - Fork 6
/
profile.install
251 lines (230 loc) · 7.11 KB
/
profile.install
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<?php
/**
* @file
* Install, update and uninstall functions for the profile module.
*/
/**
* Implements hook_install().
*/
function profile_install() {
}
/**
* Implements hook_schema().
*/
function profile_schema() {
$schema['profile'] = array(
'description' => 'Stores profile items.',
'fields' => array(
'pid' => array(
'type' => 'serial',
'not null' => TRUE,
'description' => 'Primary Key: Unique profile item ID.',
),
'type' => array(
'description' => 'The {profile_type}.type of this profile.',
'type' => 'varchar',
'length' => 32,
'not null' => TRUE,
'default' => '',
),
'uid' => array(
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
'default' => NULL,
'description' => "The {users}.uid of the associated user.",
),
'label' => array(
'description' => 'A human-readable label for this profile.',
'type' => 'varchar',
'length' => 255,
'not null' => TRUE,
'default' => '',
),
'created' => array(
'description' => 'The Unix timestamp when the profile was created.',
'type' => 'int',
'not null' => FALSE,
),
'changed' => array(
'description' => 'The Unix timestamp when the profile was most recently saved.',
'type' => 'int',
'not null' => FALSE,
),
),
'indexes' => array(
'uid' => array('uid'),
),
'foreign keys' => array(
'uid' => array(
'table' => 'users',
'columns' => array('uid' => 'uid'),
),
'type' => array(
'table' => 'profile_type',
'columns' => array('type' => 'type'),
),
),
'primary key' => array('pid'),
);
return $schema;
}
/**
* Implements hook_uninstall().
*/
function profile_uninstall() {
$config_names = config_get_names_with_prefix('profile');
foreach ($config_names as $config_file) {
$config = config($config_file);
$type = $config->get('type');
field_attach_delete_bundle('profile', $type);
}
}
/**
* Implements hook_requirements().
*
* This is here to deal with an update numbering problem in early versions of
* this module (updates started numbering at 10000, not 1000). If the wrong
* number is detected for the last update version, we fix it so that updates
* proceed properly going forward.
*/
function profile_requirements($phase) {
if ($phase == 'update' || $phase == 'runtime') {
if (backdrop_get_installed_schema_version('profile') == 10000) {
backdrop_set_installed_schema_version('profile', 1000);
}
}
return array();
}
/**
* Implements hook_update_dependencies().
*
* Make sure field module has already created the config files before running
* update 1001.
*
* @see field_update_1001().
*/
function profile_update_dependencies() {
$dependencies['profile'][1001] = array(
'field' => 1001,
);
}
/**
* Initialize the profile settings to use fieldsets.
*/
function profile_update_1000() {
config_set('profile.settings', 'profile_display', 'fieldsets');
}
/**
* Migrate any existing Profile2 profiles into this module.
*/
function profile_update_1001() {
if (!db_table_exists('profile_type')) {
return;
}
// Get Profile2 types from the table left behind in the db.
$profile2_types = db_query('
SELECT *
FROM {profile_type}
')
->fetchAllAssoc('id', PDO::FETCH_ASSOC);
// Create profile.type config files for each type.
foreach ($profile2_types as &$type) {
$type['data'] = unserialize($type['data']);
if (isset($type['data']['roles'])) {
// Authenticated role changes its key in Backdrop.
if (isset($type['data']['roles']['2'])) {
unset($type['data']['roles']['2']);
$type['data']['roles'][BACKDROP_AUTHENTICATED_ROLE] =
BACKDROP_AUTHENTICATED_ROLE;
}
}
else {
$type['data']['roles'] = array();
}
$config = config('profile.type.' . $type['type']);
$config_data = array(
'userview' => TRUE,
'type' => $type['type'],
'label' => $type['label'],
'registration' => $type['data']['registration'],
'roles' => $type['data']['roles'],
'weight' => $type['weight'],
'status' => $type['status'],
'module' => $type['module'],
'storage' => 1,
);
$config->setData($config_data);
$config->save();
}
// Drop the profile_type db table, no longer needed.
db_drop_table('profile_type');
// Backdrop's standard upgrade process converted the Profile2 bundles and
// their fields to the new config style, but it left their entity_type as
// 'profile2', whereas this module uses the entity_type of 'profile'. So we
// need to further convert config files and db tables.
// Get Profile2 bundles from config and convert to entity_type profile.
$config_names = config_get_names_with_prefix('field.bundle.profile2.');
foreach ($config_names as $config_file) {
$config = config($config_file);
$config->setName(str_replace('profile2', 'profile', $config->getName()));
$config->set('entity_type', 'profile');
$config->save();
// Remove the bundle data from the variables table.
$bundle = $config->get('bundle');
update_variable_del('field_bundle_settings__' . $bundle);
// Remove the old bundle config file that contains 'profile2'.
$old_config = config($config_file);
$old_config->delete();
}
// Get Profile2 field instances from config and convert to entity_type profile.
$config_names = config_get_names_with_prefix('field.instance.profile2.');
foreach ($config_names as $config_file) {
$config = config($config_file);
$config->setName(str_replace('profile2', 'profile', $config->getName()));
$config->set('entity_type', 'profile');
$config->save();
// The field tables exist (and have data), but their entity_type is left as
// profile2 after Backdrop does its upgrade, so we need to change that
// column for the data and revision tables.
$field_name = $config->get('field_name');
db_update('field_data_' . $field_name)
->fields(array(
'entity_type' => 'profile',
))
->condition('entity_type', 'profile2')
->execute();
db_update('field_revision_' . $field_name)
->fields(array(
'entity_type' => 'profile',
))
->condition('entity_type', 'profile2')
->execute();
// Remove the old field instance config file that contains 'profile2'.
$old_config = config($config_file);
$old_config->delete();
}
// Remove the residue of the D7 Profile2 module.
db_query("
DELETE FROM {system}
WHERE name = 'profile2'
AND type = 'module'");
}
/**
* Drop any leftover D7 Profile module tables.
*/
function profile_update_1002() {
db_drop_table('profile_field');
db_drop_table('profile_value');
}
/**
* Add _config_translatables property to Profile Type config files.
*/
function profile_update_1003() {
$config_names = config_get_names_with_prefix('profile.type');
foreach ($config_names as $config_file) {
$config = config($config_file);
$config->set('_config_translatables', array('label'));
$config->save();
}
}