Drupal7 常用的数据读写API

2013.04.17 No Comments
// node load & change
node_load($nid = NULL, $vid = NULL, $reset = FALSE);
node_load_multiple($nids =array(), $conditions = array(), $reset = FALSE);
$node = node_load($nid);
$node->title = 'xxx';
node_save($node);
// field load & change
if($node->field_xxx) {
$node->field_xxx[LANGUAGE_NONE][0]['value'] ='new value';
node_save($node);
}
print render(field_view_field('node', $node, 'field_image'));
field_get_items('node', $node, 'field_image'); // 与$node->field_image[LANGUAGE_NONE]等价
// print images
$images = field_get_items('node', $node, 'field_image');
foreach($images as $image):
print theme('image', array('path'=> $image['uri']));

 

// print images with thumbnail
$images = field_get_items('node', $node, 'field_image');
foreach ($images as $key=>$value) {
$output =field_view_value('node', $node, 'field_image', $images[$key], array(
'type' =>'image',
'settings' =>array(
'image_style' =>'thumbnail', //place your image style here
'image_link' =>'content',
),
));
print render($output);
}
// field change (fast)
$node = node_load($uid);
if($node->body) {
$node->body[LANGUAGE_NONE][0]['value'] ='new value';
field_attach_presave('node', $node);
field_attach_update('node', $node);
}
// user load & change
user_load($uid, $reset = FALSE);
user_load_multiple($uids = array(), $conditions =array(), $reset = FALSE);
$user = user_load($uid);
$user->name = 'xxxx';
user_save($user);// menu tree
menu_tree_all_data($menu_name, $link = NULL, $max_depth = NULL);
menu_tree_page_data($menu_name, $max_depth = NULL, $only_active_trail = FALSE);

// term
taxonomy_term_load($tid) : object
taxonomy_term_load_multiple($tids = array(), $conditions = array()) : array

// get more terms
$tree = taxonomy_get_tree($vid, $parent = 0, $max_depth);
foreach($tree as $leaf) {
print $leaf->tid, $leaf->vid, $leaf->name;
}
// block
block_load($module, $delta);// Pager
db_select('node', 'n')
->extend('PagerDefault')->limit(5)
->fields('n');
$statement->fetchField();
db_query_range('SELECT n.nid, n.title, n.created
FROM {node} n WHERE n.uid = :uid', 0, 10, array(':uid'=> $uid));

// insert
$fields =array('nid'=> 1, 'title' =>'my title', 'body'=> 'my body');
db_insert('node')->fields($fields)->execute();

// update
db_update('example')
->condition('id', $id)
->fields(array('field2' => 10))
->execute();

// select
$query = db_select('comment', 'c')
->fields('c', array('subject', 'name'))
->fields('n', array('title'))
->extend('PagerDefault')->limit(5)
->condition('n.type', array('article'), 'IN')
->orderBy('c.cid', 'DESC');
$query->join('node', 'n', 'n.nid = c.nid');
$statement = $query->execute();

$query = db_select('node', 'n')->fields('n', array('title'))->distinct();
$query->join('taxonomy_index', 't', 't.nid = n.nid');
$or= db_or()->condition('n.uid', $authorId)->condition('t.tid', $cats, 'IN');
$query->condition($or)->execute();

// range select
$nids = db_query_range("SELECT nid FROM {node} WHERE nid > :nid", 0, $limit, array(':nid'=> 1))->fetchCol();

// fetch
foreach ($query->execute() as $object) {
echo $object->name;
}

// get nodes by type
node_load_multiple(NULL, array('type' => 'article', 'status' => true));
// get nodes by term
$tid = 2;
$nodeType = 'article';
$termField ='field_term';
$query = db_select('node', 'n');
$query->join('field_data_'.$termField, 't', 't.entity_id = n.nid');
$nids = $query->fields('n', array('nid'))
->condition('t.entity_type','node')
->condition('n.type', $nodeType)
->condition('n.status' ,1)
->condition('t.'.$termField.'_tid', $tid)->execute()->fetchCol();
$nodes = node_load_multiple($nids);

// print views result
$views=views_get_view($viewsName);
$views->set_arguments(array(3));
$views->preview(); // print HTML
$results=$views->result; // object results
// change user role

$uid = 123;// User ID of user that you want to add role to.
$role_name = 'Role to add'; // The name of the role to add.
if ($role = user_role_load_by_name($role_name)) {
user_multiple_role_edit(array($uid), 'add_role', $role->rid);
}

评论已关闭。