function user_update_8011

Create user picture field.

Related topics

File

drupal/core/modules/user/user.install, line 668
Install, update and uninstall functions for the user module.

Code

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')) {
    module_enable(array(
      'image',
    ));
  }
  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_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();
  }
  $settings = array(
    // In D7, user pictures did not require Image module to work. Image module
    // only allowed usage of an image style to format user pictures in the
    // output. The 'user_pictures' variable had a global effect on the
    // presence of the user picture functionality before. The new user picture
    // image field is created regardless of that global setting, which means
    // the field appears on the user account form after migration, even if
    // user pictures were disabled previously. The picture is only hidden in
    // the output.
    'formatter' => update_variable_get('user_pictures', 0) ? 'image' : 'hidden',
    'file_directory' => update_variable_get('user_picture_path', 'pictures'),
    'default_image' => !empty($default_image_fid) ? $default_image_fid : 0,
    'image_style' => update_variable_get('user_picture_style', ''),
    'max_resolution' => update_variable_get('user_picture_dimensions', '85x85'),
    'max_filesize' => update_variable_get('user_picture_file_size', '30') . ' KB',
    'description' => update_variable_get('user_picture_guidelines', ''),
  );
  $field = _user_install_picture_field($settings);

  // 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['id'],
      'count' => 1,
    ))
      ->execute();
  }

  // Update the user bundle settings and hide the member_for extra field.
  $settings = update_variable_get('field_bundle_settings_user__user');
  $settings['extra_fields']['display']['member_for']['compact'] = array(
    'weight' => 0,
    'visible' => FALSE,
  );
  update_variable_set('field_bundle_settings_user__user', $settings);

  // 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');
}