field值结构
$entity->{$field_name}[$langcode][$delta][$column_name]
// $node是entity对象,
// entity是drupal最基础的存储结构,它可以是node, user, term甚至更多
// field_name是field的名称,可以在content type, profile type等定义
$node->field_name = array(
// LANGUAGE_NONE是语言标识,但drupal7没有真正使用到,所以现在为固定值。
LANGUAGE_NONE => array(
// field的item,
// field可以有多个item,因为一个field可以是多值类型,如复选框,多图片等。
array(
// value是column,
// column的名称会以field type来决定,例如表达user的是uid,
// value是大多数field type的column
'value' => 'value 1'
),
array(
'value' => 'value 2'
)
)
);
clone a node
$oNode = node_load($nid);
$node = clone $oNode;
$node->title = 'new ' . $data['title' . $lang];
$node->nid = NULL;
$node->vid = NULL;
$node->tnid = NULL;
node_save($node);
create a node
$node = new stdClass();
$node->type = 'article';
node_object_prepare($node);
$node->title = 'Node Created Programmatically on ' . date('c');
$node->uid = 1;
$node->language = LANGUAGE_NONE;
$node->body[$node->language][0]['value'] = $body_text;
$node->body[$node->language][0]['summary'] = text_summary($body_text);
$node->body[$node->language][0]['format'] = 'filtered_html';
$node->path = array('alias' => 'content/programmatically_created_node_' . date('YmdHis'));
node_save($node);
$node->nid; // Get node id of saved node
node load & update
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 & update
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,包括label
$opt = array (
'type' => 'image',
'settings' => array (
'image_style' => 'thumbnail',
'image_link' => 'content',
),
);
$items = field_get_items('node', $node, 'field_image'); // 与$node->field_image[LANGUAGE_NONE]等价
print render(field_view_value('node', $node, 'field_image', $items[0], $opt)); // 只输出value
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(能继承Manage Display的设置)
$view_mode = 'full';
$entity_type = 'user';
$entity = user_load(1);
$entity_id = 1;
field_attach_prepare_view($entity_type, array($entity_id => $entity), $view_mode, $entity->language);
entity_prepare_view($entity_type, array($entity_id => $entity), $entity->language);
$content = field_attach_view($entity_type, $entity, $view_mode);
print render($content);
提取node edit form
require_once(DRUPAL_ROOT.'/modules/node/node.pages.inc');
$node = (object) array('uid' => $account->uid, 'name' => (isset($account->name) ? $account->name : ''), 'type' => 'service', 'language' => LANGUAGE_NONE);
$form += node_form($form, $form_state, $node);
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);
}
get all field
field_info_instances('node', $node->type);
Add file / image fields
$file_path = drupal_realpath('somefile.png');
$file = (object)array(
'uid' => 1,
'uri' => $file_path,
'filemime' => file_get_mimetype($filepath) ,
'status' => 1,
);
$file = file_copy($file, 'public://');
$node->field_image[LANGUAGE_NONE][0] = (array)$file;
empty value
$node->field_image = array();
create language of node
$node->tnid = $mainNid;
node_save($node);
$mainNode->tnid = $mainNode->nid;
node_save($mainNode);
date field change
$node->field_date[LANGUAGE_NONE][0] = array(
'value' => format_date(strtotime('now'), 'custom', 'Y-m-d H:i:s', 'UTC'),
'timezone' => 'UTC',
'timezone_db' => 'UTC',
);
get nodes by type
node_load_multiple(NULL, array('type' => 'article', 'status' => true));
node list
$nids = db_select('node', 'n')
->condition('n.status', 1)
->condition('n.type', array('article'), 'IN')
->fields('n', array('nid'))
->orderBy('n.nid', 'DESC')
->addTag('node_access')
->extend('PagerDefault')->limit(10)
->execute()->fetchCol();
if(count($nids)) {
$nodes_view = node_view_multiple(node_load_multiple($nids));
$output = render($nodes_view);
} else {
$output = t('No Posts');
}
return array(
'content' => $output,
'pager' => theme('pager')
);
create a vocabulary
$vocab = array(
'name' => t('my new vocab'),
'description' => t('whatever'),
'nodes' => array('my_type_X' => 1,
'my_type_Y' => 1,
'my_type_Z' => 1),
'hierarchy' => 0,
'relations' => 0,
'tags' => 0,
'multiple' => 0,
'required' => 0,
'weight' => 5,
);
taxonomy_save_vocabulary($vocab);
create a term
$term = array(
'vid' => 5, // hopefully it's the vocabulary created above
'name' => 'my_term',
);
taxonomy_save_term($term);
get bondes by field(term)
$node_type = 'article';
$field_name = 'field_term';
$field_key = 'tid';
$field_value = 2;
$query = db_select('node', 'n');
$query->join('field_data_'.$field_name, 't', 't.entity_id = n.nid');
$nids = $query->fields('n', array('nid'))
->condition('n.uid', $uid)
->condition('n.type', $node_type)
->condition('t.entity_type','node')
->condition('t.'.$field_name.'_'.$field_key, $field_value)->execute()->fetchCol();
$nodes = node_load_multiple($nids);
get field allowed values
$data = field_info_field('field_xxxx');
$data['settings']['allowed_values'];
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。