2.2 慢API替换

龹龸龶龲龯龬龨龤龠龟龞龝龚龘

龸龔龯龐龏龋龊龉龇龄龃龂龀齽齻齺齷齴齲齮齬齪齧API齚齗龘

齕齑user_save

1.鼼齗鼸鼷鼳鼰鼭

db_insert('users_roles')
    ->fields(array('uid', 'rid'));
    ->values(array(
        'uid' => $user->uid,
        'rid' => TEMP_EDITOR_RID,
      ));
    ->execute();

2.鼼齗鼸黧黥鼰鼭

db_delete('users_roles')
    ->condition('uid', $user->uid, 'rid' => TEMP_EDITOR_RID)
    ->execute();

3.黟黜黚龉

$data = array(
  'uid' => '12',
  'pass' => '123456',
);

require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

$data['pass'] = user_hash_password(trim($data['pass']));

drupal_write_record('users', $data, array('uid'));

4.麵麲麯齗鼸

/**
 * 保存用户基本信息
 *
 * @param $data
 *   users表字段信息   
 *
 * @return
 *   FALSE 失败,1 新增成功,2 更新成功
 */
function user_basic_save(&$data) {
  if (isset($data['uid'])) { // 更新
    if (isset($data['pass'])) { // 修改密码
      require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

      $data['pass'] = user_hash_password(trim($data['pass']));
    }

    return drupal_write_record('users', $data, array('uid'));
  }
  else { // 新增
    require_once DRUPAL_ROOT . '/' . variable_get('password_inc', 'includes/password.inc');

    // 处理密码
    if (isset($data['pass'])) {
      $data['pass'] = user_hash_password(trim($data['pass']));
    }
    else {
      $data['pass'] = ''; // 生成随机密码
      $data['pass'] = user_hash_password(trim($data['pass']));
    }
    
    $data['uid'] = db_next_id(db_query('SELECT MAX(uid) FROM {users}')->fetchField());

    $data['created'] = REQUEST_TIME;
    $data['access'] = REQUEST_TIME;
    $data['login'] = REQUEST_TIME;

    if (!isset($data['mail'])) {
      $data['mail'] = $data['name'] . '@yoursite.com';
      $data['init'] = $data['name'] . '@yoursite.com';
    }

    $data['status'] = 1;

    return drupal_write_record('users', $data);
  }
}

5.黟黜齗鼸麫麪麧麤

鹱黟黜users麡鹮龀龇鹬齗鼸鹩齑鹥鹤

API鹠鹜龘

6.黟黜齗鼸鹙鹘麧麤

鹬龯鹗鹩齑齷鹓鹑龇鹐鹌龂鹉鹅齬齪齧麡鹄龯{user_profile}

/**
 * 保存用户信息
 *
 * @param $user_profile
 *   array('uid', 'realname', 'gender', ...)
 *
 * @return
 *   FALSE 失败,1 新增成功,2 更新成功
 */
function user_profile_save($user_profile) {
  if (user_profile_load($user_profile['uid'])) { // 更新
    return drupal_write_record('user_profile', $user_profile, array('uid'));
  }
  else { // 新增
    return drupal_write_record('user_profile', $user_profile);
  }
}

7.鹀鸾鸽齗鼸鸺鸶鸵鸲鸯authmap龇drupal龸鸭龋鸬鸨鸥龯user_set_authmaps龇鸤鸥鸢鸠鸟$user鸝鸚鸗鼼鸖鸨龇鸕鸔鹄鸒鸵鸐鸠鸟user鸝鸚龇鸏齽齬齪齧鸋齕鸊鸬鸨鸆鸅uid龘黜鸂鷿鷻龇龄龃龀齽齬鷺鸝鷷龐龘

/**
 * Save mappings of which external authentication module(s) authenticated
 * a user. Maps external usernames to user ids in the users table.
 *
 * @param $uid
 * @param $authmaps
 *   An associative array with a compound key and the username as the value.
 *   The key is made up of 'authname_' plus the name of the external authentication
 *   module.
 * @see user_external_login_register()
 */
function custom_user_set_authmaps($uid, $authmaps) {
  foreach ($authmaps as $key => $value) {
    $module = explode('_', $key, 2);
    if ($value) {
      db_merge('authmap')
        ->key(array(
          'uid' => $uid,
          'module' => $module[1],
        ))
        ->fields(array('authname' => $value))
        ->execute();
    }
    else {
      db_delete('authmap')
        ->condition('uid', $uid)
        ->condition('module', $module[1])
        ->execute();
    }
  }
}

鷴齑node_load

1.鷱鷯nid鷬鷪鷨鷧麫麪麧麤龇鷷鹬鷨鷧鷣鷡龇鷞鷚鷘鷗鷖鷪node麡鹮龀龘

鷷鹬龇鸕鷔齗龋鸥鷑鷐id鷬鷪name龇鸕鹱齪齧鸋鹬龐API龯

/**
 * 获取以英文逗号分隔的多个id字符串所对应的名字
 */
function get_names_from_ids($ids, $type='term') {
  $names = array();
  $ids = explode(',', $ids);

  switch ($type) {
    case 'term':
      foreach ($ids as $value) {
        $names[] = ; // 从taxonomy_term_data表里获取name
      }
      break;

    case 'node':
      foreach ($ids as $value) {
        $record = ; // 从node表获取title
        $names[] = $record['title'];
      }
      break;

    case 'users':
      foreach ($ids as $value) {
        $record = ''; // 从users表获取name
        $names[] = $record['name'];
      }
      break;

    default:
      # code...
      break;
  }

  return implode($names, ',');
}

2.鷖鷪鸾鸊鶯鸾鸊齽鹜麟麞麧麤龇鹅鶭齷鶬鶩鷡龋龹龸龐龇龀齽齗node_load + entity_metadata_wrapper龇鶣鸟鸥鼼鸋鸽鶡鶞鶚龘

$node = node_load($nid);
$node_wrapper = entity_metadata_wrapper('node', $node);
$field_1_value = $node_wrapper->field_field_1->value();
$field_2_value = $node_wrapper->field_field_2->value();

3.鷖鷪鸾鸊齽龐麟麞麧麤龇龀齽鷄鶘鶗鶔鶒鶐麟麞麡鶍鹬鶉鶆鶃鶁鷔齗龇龀齴齲API龇鹬龯鷬鷪鵿鵾鸏鵻鵷鵴龘鵲龯鷬鷪鵷鵴鵯鵬麧麤龇鹮齚鸒鶿麀鶒鶐龇鵫鸐鸟齴齲鸋龇鶭鵪齧鵧

/**
 * 获取项目所属机构id
 */
function get_project_institution($project_id) {
  $query = db_select('field_data_field_project_company', 'fpc');
  $query->condition('fpc.entity_id', $project_id);
  $query->fields('fpc', array('field_project_company_target_id'));
  
  return $query->execute()->fetchField();
}

4.鵝鵚鵖鶃龀齽鷑鷐龤龠龟龞鵕鹠鵒齕鸊齬齪齧麡龇鷖鷪麧麤鸵鹱鷖齬齪齧麡龘

鹬龯齪齧齕鸊龝鵤麧麤麡龇{institutions}龇鵐龝鵤鵝鵚鵖鶃龋鸏鸒麟麞麧麤鵫龂鼳鵎齮鸔鸊麡鹄龇鹅hook_node_insert, hook_node_update, hook_node_delete鸵龇鹠鵒麎麯鸢龘鸔鵌鹅鷖鷪龝鵤麧麤龋鵊鸽龇鵈鵅鸥app鷗鵃鹄龇鷘鷗鷖齬齪齧龋institutions麡龇鹱鶞鶿鸋鴿

鸾齑node_save

1.鴽鹬鸥鸆黟黜鷨鷧鷣鷡龇鷨鷧鴼鴸鹑node麡鹄鸒龋麧麤龇龀齽鷘鷗鴵鸗node麡龘

2.鴽鹬鸆鸥黟黜鷨鷧龋齕鸊鴱鴰鸊麟麞麧麤龇龀齽鷘鷗鴬鴵鸗麟麞麡龇麟麞version麡鹬鶉鴫鸒齗齮龋鴨鵫鸐齗鴧龘

鴥齑user_load

1.鷬鷪齗鼸麫麪麧麤龇鷖鷪users麡鹮龀

/**
 * 获取用户基本信息
 *
 * @return
 *   FALSE || 用户信息数组
 */
function user_basic_load($uid) {
  $params = array(
    'table' => 'users',
    'conditions' => array('uid' => $uid),
    'single' => TRUE,
  );

  return get_table_record_value2($params);
}

鴝龀齽鴙鴘鴗龯user_basic_load_by_name, user_basic_load_by_mail鹑齬齪齧API龘

2.鷬鷪齗鼸鼰鼭麧麤龇鷖鷪users_roles麡鹮龀龘鴕鸽API鸥user_has_role龇鴱鴔龄龃齕鴐鷖鷪user鸝鸚鹄龋roles麧麤龇龂鸠鸟鸒user鸝鸚龇龊鴏鸐鴍龇鴌鼼鸕鴊鸒鸵鴉鴆鴃龋鸐鸥鳿龹鳻鳸齗鼸龋鼰鼭龇鸏齽鸐鸠鸟齕鷘鸅$user龘 龐龏鳷鸊鳴龞龇鸕鸥鵐鳰鸊鼰鼭龋鴆鴃齴齲鸋鶗鶔龋API龇鹬鴆鴃齗鼸鸥鳯鶘鳮鴔鼰鼭龯

/**
 * 判断用户是否是开发者角色
 */
function user_is_developer($uid = NULL) {
  if (!$uid) {
    global $user;
    $uid = $user->uid;
  }
  elseif (!is_numeric($uid)) {
    $uid = $uid->uid;
  }

  $developer_role = variable_get('user_admin_role', '');

  // 查询users_roles表
  // 查到了 return TRUE

  return FALSE;
}

3.鷬鷪齗鼸鴙鴘鹙鹘麧麤龇鳪鳩鸐齗齗鼸麟麞龇鷘鷗齚齗齬齪齧麡龇鹬龯user_profile

/**
 * 获取用户信息
 *
 * @return
 *   FALSE || 用户信息数组
 */
function user_profile_load($uid) {
  // 查询user_profile表
}

鴝龀齽鴙鴘鴗user_profile_load_by_phone鹑API龘

4.鹀鸾鸽鳻鳸鸵龇鸠鸟鶶authmap鹄鳨鴗齗鼸麧麤龇drupal龸鸭龋鸬鸨鸥龯user_external_load龇鸢麑麐鸵鳤鳢鸋user鸝鸚龇鸒鸵鴉鸐鸠鸟龇鸏齽齬齪齧齕鸊鸬鸨龇鸆鳤鳢uid龘

/**
 * Fetches a user object based on an external authentication source.
 *
 * @param string $authname
 *   The external authentication username.
 *
 * @return
 *   UID if the user is found or FALSE if not found.
 */
function custom_user_external_load($authname) {
  return db_query("SELECT uid FROM {authmap} WHERE authname = :authname", array(':authname' => $authname))->fetchField();
}

鳗齑file_load

1.鵅鳔麫麪鹜鸆鸠鸟鷖鷪file_managed麡鹱鶬鳓齮鷔齗龋鷌龲麧麤龘

/**
 * 获取文件信息
 */
function get_file_info($fid) {
  $params = array(
    'table' => 'file_managed',
    'single' => TRUE,
    'conditions' => array('fid' => $fid),
  );

  return get_table_record_value2($params);
}

鳑齑file_save

1.鵅鳔麫麪鹜鸆鸠鸟麵麲齮file_managed麡鹱龀齽鸋龘

/**
 *  更新文件基本信息
 */
function file_basic_save(&$file) {
  if (isset($file['fid'])) {
    return drupal_write_record('file_managed', $file, 'fid');
  }
  else {
    return drupal_write_record('file_managed', $file);
  }
}

鳏齑file_copy

1.鹬鶉鸐鳋鹽麑麯龋鷌龲鳈鳸龇齗file_unmanaged_copy龊鳅

鳁齑file_save_data

1.龀齽齬齪齧鸊custom_file_save_data龇鳀鹓鹱鸥鸐齗鸅file鸝鸚龘

/**
 * 保存文件
 *
 * @param $data
 *   文件数据
 *
 * @param $destination
 *   文件保存路径
 *
 * @param $author
 *   文件作者uid
 *
 * @param $replace
 *   Replace behavior when the destination file already exists:
 *   - FILE_EXISTS_REPLACE - Replace the existing file.
 *   - FILE_EXISTS_RENAME - Append _{incrementing number} until the filename is
 *                          unique.
 *   - FILE_EXISTS_ERROR - Do nothing and return FALSE.
 *
 * @return
 *   文件信息数组 || FALSE
 */
function custom_file_save_data($data, $destination, $author = NULL, $replace = FILE_EXISTS_RENAME) {
  if ($author === NULL) {
    global $user;

    $author = $user->uid;
  }

  // 验证路径是否合法
  if (!file_valid_uri($destination)) {
    return FALSE;
  }

  // 保存物理文件
  if ($uri = file_unmanaged_save_data($data, $destination, $replace)) {
    $file = array(
      'uid' => $author,
      'uri' => $uri,
      'filename' => drupal_basename($uri),
      'filemime' => file_get_mimetype($uri),
      'filesize' => filesize($uri),
      'status' => 1,
      'timestamp' => REQUEST_TIME,
    );

    if (file_basic_save($file)) {
      return $file;
    }
    else {
      return FALSE;
    }
  }

  return FALSE;
}

鲫齑taxonomy_vocabulary_load

1.鷖鷪鲪鲦麡麧麤龇鸆鸠鸟鴬鷖鷪taxonomy_vocabulary麡鹮龀

鲥齑taxonomy_vocabulary_machine_name_load

1.鷖鷪鲪鲦麡麧麤龇鸆鸠鸟鴬鷖鷪taxonomy_vocabulary麡鹮龀

/**
 * 根据词汇表的机读名获取词汇表id
 */
function get_voc_id_from_machine_name($machine_name) {
  // 查询taxonomy_vocabulary表
}

鲥齕齑taxonomy_term_load

1.鷖鷪鲡鲟麧麤龇鸆鸠鸟鴬鷖鷪taxonomy_term_data麡鹮龀

鷔齗龋鸽鲲龯

/**
 * 根据tid获取分类名字
 */
function get_term_name_from_tid($tid) {
  // 查询taxonomy_term_data表
}

/**
 * 根据分类名字获取tid
 */
function get_tid_from_term_name($term_name, $vid) {
  // 查询taxonomy_term_data表
}

鲥鷴齑taxonomy_term_save

1.麵麲鲡鲟龇鸆鸠鸟麵麲taxonomy_term_data鲜taxonomy_term_hierarchy鴰鸊麡鹮龀龘

/**
 * 保存术语
 */
function custom_term_save($name, $vid) {
  $term = array(
    'name' => $name,
    'vid' => $vid,
  );

  if (drupal_write_record('taxonomy_term_data', $term)) {
    // 保存术语等级记录
    $temp = array(
      'tid' => $term['tid'],
    );

    drupal_write_record('taxonomy_term_hierarchy', $temp);

    return $term['tid'];
  }

  return FALSE;
}

鲥鸾齑taxonomy_get_tree

1.鷖鷪鲪鲦麡龐龋鲡鲟麧麤鲔麡龇鶶taxonomy_term_data麡齬鲐鷖鷪鴠鲎鹮龀

/**
 * 获取词汇表一级术语数组
 * 
 * @param $voc_machine_name
 *   词汇表机读名
 *
 * @param $not_multiple
 *   若词汇表术语为多层及,则传值TRUE,否则默认为FALSE,即:单层级
 *
 * @return
 *   术语数组,array('tid' => 'name')
 */
function get_vocabulary_first_level_terms($voc_machine_name, $multiple = FALSE) {
  $vid = get_vid_from_machine_name($voc_machine_name);

  if (!$multiple) {
    $query = "SELECT tid, name FROM {taxonomy_term_data} WHERE vid = :vid ORDER BY weight DESC";
  }
  else {
    $query = "SELECT td.tid, td.name FROM {taxonomy_term_data} td INNER JOIN {taxonomy_term_hierarchy} th ON td.tid = th.tid WHERE td.vid = :vid AND th.parent = 0 ORDER BY weight DESC";
  }

  $result = db_query($query, array(':vid' => $vid));

  $return_array = array();
  foreach ($result as $value) {
    $return_array[$value->tid] = $value->name;
  }

  return $return_array;
}

评论 (写第一个评论)