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;
}

评论 (写第一个评论)