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

评论 (写第一个评论)