Skip to content

Commit ac9e2b9

Browse files
committed
Issue #17: Update field identifiers if coming from Drupal 7.
Fixes #17.
1 parent 9f79c28 commit ac9e2b9

File tree

1 file changed

+40
-16
lines changed

1 file changed

+40
-16
lines changed

conditional_fields.install

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,48 @@ function conditional_fields_update_last_removed() {
4949
}
5050

5151
/**
52-
* Adjust schema for new `entity_type.bundle.field_name` ID types (rather than
53-
* numeric IDs which Backdrop does not use).
52+
* Adjust schema and data for new field instance identifier format.
5453
*/
5554
function conditional_fields_update_1000() {
5655
$config = config('conditional_fields.settings');
56+
if (db_table_exists('conditional_fields')) {
57+
db_change_field('conditional_fields', 'dependee', 'dependee', array(
58+
'description' => 'The id of the dependee field instance.',
59+
'type' => 'varchar',
60+
'length' => 128,
61+
'not null' => TRUE,
62+
'default' => '',
63+
));
64+
db_change_field('conditional_fields', 'dependent', 'dependent', array(
65+
'description' => 'The id of the dependent field instance.',
66+
'type' => 'varchar',
67+
'length' => 128,
68+
'not null' => TRUE,
69+
'default' => '',
70+
));
71+
72+
$select = db_select('conditional_fields', 'cf');
73+
$select->join('field_config_instance', 'dependee', 'cf.dependee = dependee.id');
74+
$select->join('field_config_instance', 'dependent', 'cf.dependent = dependent.id');
75+
$select->fields('cf', array('id', 'options', 'dependee', 'dependent'));
76+
$select->addField('dependee', 'field_name', 'dependee_field_name');
77+
$select->addField('dependee', 'entity_type', 'dependee_entity_type');
78+
$select->addField('dependee', 'bundle', 'dependee_bundle');
79+
$select->addField('dependent', 'field_name', 'dependent_field_name');
80+
$select->addField('dependent', 'entity_type', 'dependent_entity_type');
81+
$select->addField('dependent', 'bundle', 'dependent_bundle');
82+
$select->orderBy('cf.dependent');
83+
$result = $select->execute();
84+
foreach ($result as $conditional) {
85+
// Update the ID with the new format.
86+
db_update('conditional_fields')
87+
->fields(array(
88+
'dependee' => $conditional->dependee_entity_type . '.' . $conditional->dependee_bundle . '.' . $conditional->dependee_field_name,
89+
'dependent' => $conditional->dependent_entity_type . '.' . $conditional->dependent_bundle . '.' . $conditional->dependent_field_name,
90+
))
91+
->condition('id', $conditional->id)
92+
->execute();
93+
}
94+
}
5795

58-
db_change_field('conditional_fields', 'dependee', 'dependee', array(
59-
'description' => 'The id of the dependee field instance.',
60-
'type' => 'varchar',
61-
'length' => 128,
62-
'not null' => TRUE,
63-
'default' => '',
64-
));
65-
db_change_field('conditional_fields', 'dependent', 'dependent', array(
66-
'description' => 'The id of the dependent field instance.',
67-
'type' => 'varchar',
68-
'length' => 128,
69-
'not null' => TRUE,
70-
'default' => '',
71-
));
7296
}

0 commit comments

Comments
 (0)