Create user picture field.
function user_update_8011() {
global $user;
// User pictures can only be migrated to the new user picture image field
// if Image module is installed.
if (!module_exists('image')) {
$old_schema = module_enable(array(
'image',
));
if ($old_schema['image'] == SCHEMA_UNINSTALLED) {
// Install image.module with schema version 8002 as a previous version
// would have to create tables that would be removed again.
update_set_schema('image', 8002);
// Inlined version of image_install(), make sure that the styles
// directory exists.
$directory = update_variable_get('file_default_scheme', 'public') . '://styles';
file_prepare_directory($directory, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS);
}
}
if ($default_image = update_variable_get('user_picture_default', '')) {
$picture_directory = update_variable_get('file_default_scheme', 'public') . '://' . update_variable_get('user_picture_path', 'pictures');
file_prepare_directory($picture_directory, FILE_CREATE_DIRECTORY);
$destination = file_stream_wrapper_uri_normalize($picture_directory . '/' . drupal_basename($default_image));
// The file entity needs to be created manually as entities can not be
// created/saved during the upgrade path. Attempt to replicate the behavior
// of file_save_data() by updating an eventually existing record for that
// file.
file_unmanaged_save_data($default_image, $destination, FILE_EXISTS_REPLACE);
$uuid = new Uuid();
db_merge('file_managed')
->key(array(
'uri' => $destination,
))
->fields(array(
'uid' => $user->uid,
'status' => FILE_STATUS_PERMANENT,
'filename' => drupal_basename($destination),
'uuid' => $uuid
->generate(),
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
'filesize' => filesize($destination),
'filemime' => file_get_mimetype($destination),
'timestamp' => REQUEST_TIME,
))
->execute();
$default_image_fid = db_query('SELECT fid FROM {file_managed} WHERE uri = :uri', array(
':uri' => $destination,
))
->fetchField();
}
// Create the field and instance.
$field = array(
'id' => 'user_picture',
'module' => 'image',
'type' => 'image',
'cardinality' => 1,
'locked' => FALSE,
'indexes' => array(
'fid' => array(
'fid',
),
),
'settings' => array(
'uri_scheme' => 'public',
'default_image' => FALSE,
),
);
_update_8003_field_create_field($field);
$instance = array(
'id' => 'user.user.user_picture',
'entity_type' => 'user',
'label' => 'Picture',
'bundle' => 'user',
'description' => update_variable_get('user_picture_guidelines', ''),
'required' => FALSE,
'settings' => array(
'file_extensions' => 'png gif jpg jpeg',
'file_directory' => update_variable_get('user_picture_path', 'pictures'),
'max_filesize' => update_variable_get('user_picture_file_size', '30') . ' KB',
'alt_field' => 0,
'title_field' => 0,
'max_resolution' => update_variable_get('user_picture_dimensions', '85x85'),
'min_resolution' => '',
'default_image' => !empty($default_image_fid) ? array(
$default_image_fid,
) : array(),
),
);
_update_8003_field_create_instance($field, $instance);
module_load_install('entity');
if (update_variable_get('user_pictures', 0)) {
$formatter = 'image';
$widget = 'image_image';
}
else {
$formatter = $widget = 'hidden';
}
// Assign form settings for the 'default' form mode.
$form_display = _update_8000_entity_get_form_display('user', 'user', 'default');
$form_display
->set('content.user_picture', array(
'type' => $widget,
'settings' => array(
'progress_indicator' => 'throbber',
'preview_image_style' => 'thumbnail',
),
'weight' => -1,
))
->save();
// Assign display settings for the 'default' and 'compact' view modes.
$display = _update_8000_entity_get_display('user', 'user', 'default');
$display
->set('content.user_picture', array(
'label' => 'hidden',
'type' => $formatter,
'settings' => array(
'image_style' => 'thumbnail',
'image_link' => 'content',
),
'weight' => 0,
))
->save();
$display = _update_8000_entity_get_display('user', 'user', 'compact');
$display
->set('content.user_picture', array(
'label' => 'hidden',
'type' => $formatter,
'settings' => array(
'image_style' => update_variable_get('user_picture_style', ''),
'image_link' => 'content',
),
'weight' => 0,
));
$display
->set('content.member_for', array(
'visible' => FALSE,
));
$display
->save();
// Add file usage for the default field.
if (!empty($default_image_fid)) {
db_insert('file_usage')
->fields(array(
'fid' => $default_image_fid,
'module' => 'image',
'type' => 'default_image',
'id' => $field['uuid'],
'count' => 1,
))
->execute();
}
// Delete old variables.
update_variable_del('user_pictures');
update_variable_del('user_picture_path');
update_variable_del('user_picture_default');
update_variable_del('user_picture_style');
update_variable_del('user_picture_dimensions');
update_variable_del('user_picture_file_size');
update_variable_del('user_picture_guidelines');
}