Commit e4f8d658 authored by Eric Bremner's avatar Eric Bremner
Browse files

FDSU-1841: Fixing update hook for changing fields in virtual site homepage.

parent 4662c1b9
......@@ -2,14 +2,29 @@
function uw_virtual_site_homepage_update_dependencies() {
// Indicate that the uw_virtual_site_homepage_update_7001() function provided by this module
// must run before the os_profiles_update_7045() function provided by the 'os_profiles' module.
// must run before the one of the os_profiles_update_N() function provided by the 'os_profiles' module.
$dependencies['os_profiles'][7045] = array(
'uw_virtual_site_homepage' => 7100,
);
$dependencies['os_profiles'][7048] = array(
'uw_virtual_site_homepage' => 7100,
);
$dependencies['os_profiles'][7049] = array(
'uw_virtual_site_homepage' => 7100,
);
$dependencies['os_profiles'][7050] = array(
'uw_virtual_site_homepage' => 7100,
);
$dependencies['os_profiles'][7051] = array(
'uw_virtual_site_homepage' => 7100,
);
$dependencies['os_profiles'][7052] = array(
'uw_virtual_site_homepage' => 7100,
);
return $dependencies;
}
/**
* Changing Office Hours field name and instance for virtual homepages.
*/
......@@ -20,107 +35,402 @@ function uw_virtual_site_homepage_update_7100() {
// Set variables, changing the below fields for uw homepage virtual sites.
// Profile image, Site description, Faculty, Office location, Phone number, Extension, Email and Office hours.
$entity_type = 'node';
$bundle = 'uw_virtual_site_homepage';
$old_names = array(
'field_profile_image',
'body',
'field_faculty',
'field_office_location',
'field_phone_number',
'field_extension',
'field_email_homepage',
'field_office_hours'
'field_office_hours',
'field_profile_image',
'body',
);
// Step through each of the old names and convert to new names.
foreach ($old_names as $old_name) {
// If this is the body, special new name, else insert uw into old field name.
if ($old_name == 'body') {
$new_name = 'field_uw_site_description';
}
else {
$new_name = str_replace("field", "field_uw", $old_name);
}
// Get old field info.
$old_field = field_info_field($old_name);
// Create new field.
$new_field = $old_field;
$new_field['field_name'] = $new_name;
// If there is no new field, create a new field, if there is, update the new field.
if (!field_info_field($new_name)) {
field_create_field($new_field);
}
else {
field_update_field($new_field);
}
// Get old field instance.
$old_instance = field_info_instance($entity_type, $old_name, $bundle);
// Set new instance.
$new_instance = $old_instance;
$new_instance['field_name'] = $new_name;
// If there is no instance of new field, create the new field, if there is an instance, update.
if (!field_info_instance($entity_type, $new_name, $bundle)) {
field_create_instance($new_instance);
}
else {
field_update_instance($new_instance);
}
// Migrate old fields' data to the new ones.
// Get old fields' data.
$field_data = db_select('field_data_' . $old_name, 'old')
->fields('old')
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute();
// Store old fields data.
$counter = 0;
$data_new = array();
while ($data = $field_data->fetchAssoc()) {
$data_new = array();
foreach ($data as $column => $value) {
$column = str_replace($old_name, $new_name, $column);
$data_new[$column] = $value;
}
db_insert('field_data_' . $new_name)
->fields($data_new)
->execute();
foreach ($data as $column => $value) {
$column = str_replace($old_name, $new_name, $column);
$data_new[$counter][$column] = $value;
}
$counter++;
}
// Migrate old fields' revision data to the new ones.
// Get old fields' revisions.
$field_revision = db_select('field_revision_' . $old_name, 'old')
->fields('old')
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute();
// Store old fields' revisions.
$counter = 0;
$revision_new = array();
while ($revision = $field_revision->fetchAssoc()) {
$revision_new = array();
foreach ($revision as $column => $value) {
$column = str_replace($old_name, $new_name, $column);
$revision_new[$column] = $value;
$revision_new[$counter][$column] = $value;
}
db_insert('field_revision_' . $new_name)
->fields($revision_new)
->execute();
$counter++;
}
// Delete all entries in old field of uw virtual site homepage.
$num_field_deleted = db_delete('field_data_' . $old_name)
->condition('bundle', $bundle)
->execute();
// As long as not body, delete the old field.
if($old_name !== 'body') {
field_delete_field($old_name);
}
// Delete all entries in old field revisions of uw virtual site homepage.
$num_revision_deleted = db_delete('field_revision_' . $old_name)
->condition('bundle', $bundle)
->execute();
// If there is no new field, create the new field.
if(!db_table_exists('field_data_' . $new_name)) {
$new_field = _get_new_field($new_name);
field_create_field($new_field);
}
// If there is an instance of the old field, remove ie (i.e. removing from content type).
if ($instance = field_info_instance('node', $old_name, $bundle)) {
// Get the old instance.
$check_old_instance = db_select('field_config_instance', 'fc')
->fields('fc')
->condition('field_name', $old_name)
->condition('entity_type', $entity_type)
->condition('bundle', $bundle)
->execute()->fetchAll();
// If there is an old instance, delete it.
if(count($check_old_instance) > 0) {
$instance['field_name'] = $old_name;
$instance['entity_type'] = $entity_type;
$instance['bundle'] = $bundle;
field_delete_instance($instance);
}
// Step through each of the old fields data and set it to the new fields data.
foreach($data_new as $data) {
// If there is a new field, insert the old data.
if(db_table_exists('field_data_' . $new_name)) {
// Get the data from the new field.
$check_for_field = db_select('field_data_' . $new_name, 'data')
->fields('data');
foreach($data as $key => $value) {
$check_for_field->condition($key, $value);
}
$results = $check_for_field->execute();
// Check if there is data from the old field in the new field.
$insert = true;
if($record = $results->fetchAssoc()) {
$insert = false;
break;
}
// If there is no data from old field in new field, insert the old field data into the new field.
if($insert) {
db_insert('field_data_' . $new_name)
->fields($data)
->execute();
}
}
}
// Step through each old revision and set it to the new revision.
foreach($revision_new as $revision) {
// If there is a new revision, insert the old revision.
if(db_table_exists('field_revision_' . $new_name)) {
// Get the data from the new revision.
$check_for_revision = db_select('field_revision_' . $new_name, 'data')
->fields('data');
foreach($revision as $key => $value) {
$check_for_field->condition($key, $value);
}
$results = $check_for_field->execute();
// Check if there is data from the old revision in the new revision.
$insert = true;
if($record = $results->fetchAssoc()) {
$insert = false;
break;
}
// If there is no old revision in the new revision, insert the old revision in the new revision.
if($insert) {
db_insert('field_revision_' . $new_name)
->fields($revision)
->execute();
}
}
}
}
// Perform feature revert to place new instances of fields.
features_revert_module('uw_virtual_site_homepage');
}
/**
* Helper function to get the new field information
*/
function _get_new_field($new_name) {
if($new_name == "field_uw_email_homepage") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_uw_email_homepage',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);
}
if($new_name == "field_uw_extension") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_uw_extension',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);
}
if($new_name == "field_uw_faculty") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_uw_faculty',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);
}
if($new_name == "field_uw_office_hours") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_uw_office_hours',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(),
'translatable' => 0,
'type' => 'text_long',
);
}
if($new_name == "field_uw_office_location") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_uw_office_location',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);
}
if($new_name == "field_uw_phone_number") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_uw_phone_number',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(
'max_length' => 255,
),
'translatable' => 0,
'type' => 'text',
);
}
if($new_name == "field_uw_profile_image") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(),
'field_name' => 'field_uw_profile_image',
'foreign keys' => array(
'fid' => array(
'columns' => array(
'fid' => 'fid',
),
'table' => 'file_managed',
),
),
'indexes' => array(
'fid' => array(
0 => 'fid',
),
),
'locked' => 0,
'module' => 'image',
'settings' => array(
'default_image' => 0,
'uri_scheme' => 'public',
),
'translatable' => 0,
'type' => 'image',
);
}
if($new_name == "field_uw_site_description") {
$new_field = array(
'active' => 1,
'cardinality' => 1,
'deleted' => 0,
'entity_types' => array(
0 => 'node',
),
'field_name' => 'field_uw_site_description',
'foreign keys' => array(
'format' => array(
'columns' => array(
'format' => 'format',
),
'table' => 'filter_format',
),
),
'indexes' => array(
'format' => array(
0 => 'format',
),
),
'locked' => 0,
'module' => 'text',
'settings' => array(),
'translatable' => 0,
'type' => 'text_with_summary',
);
}
return $new_field;
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment