Posted by nhocmuonlanhoc on November 10, 2010 at 2:37am
mình muốn tạo ra 1 page sử dụng node ,
mình sử dụng đọan code sau
function create_node()
{
//body content
$body_text = 'Here code for body';
$node = new stdClass();
$node->type ='page';
//setup default value for the node
node_object_prepare($node);
$node->title = ' Page tested create';
$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'] = 2;
$path = 'content/tested_page';
$node->path= array('alias'=> $path);
node_save($node);
}//function create_nodevấn đề mình gặp là không hiển thị body , ( không thấy nội dung body )
và mình muốn lưu lại nid của node , để update node thì luư và lấy như thế nào nhỉ ?
mong các bạn giúp mình với
Comments
đã làm đc câu thứ 1
$node->body[$node->language][0]['format'] = 'filtered_html';thay vì
$node->body[$node->language][0]['format'] = 2;
[url]http://zblue.net[/url]
Về node ID
Sau khi kết thúc hàm node_save($node) , $node sẽ được trả ra , $node này sẽ bao gồm tất cả các thông tin của node , trong đó có nid vừa được tạo ra :)
Bạn có thể dùng var_dump($node) sau node_save($node) để kiểm tra
về lưu node id
cảm ơn bạn ngnhoan đã trả lời giúp mình !
vậy mình có thể lưu nid vào chổ nao trong ccdl nhỉ , drupal có hổ trợ cái nào giống option bên wp hok nhỉ . mình nghĩ là table variable, không biết đúng hok nhỉ ?
còn một câu nửa đối với phần body
$node->body[$node->language][0]['format'] = 'filtered_html';thay vì dùng "filtered_html" mìnhc òn có các kiểu formart nào khác không ?
[url]http://zblue.net[/url]
lưu node
$node=node_save($node);
variable_set('tested',$node->nid);
mình dùng thêm variable_set để thiết lập biến
nhưng kết quả trả về thế này
Notice: Trying to get property of non-object in create_node() (line 26 of....\modules\Tested\tested.module).
và trong api http://api.drupal.org/api/drupal/modules--node--node.module/function/nod...
node_save không return về 1 node
[url]http://zblue.net[/url]
fix it here
node_save($node);
variable_set('tested',$node->nid);
như thế này lại chạy tốt
[url]http://zblue.net[/url]
vấn đề mới
sau khi tạo xong node muốn node đó xuất hiện ở main menu thì làm thế nào nhỉ
Need help !!
<\code>
[url]http://zblue.net[/url]
create and save a new node in drupal 7
Khởi tạo 1 node
$node = new stdClass(); // We create a new node object
$node->type = "book"; // Or any other content type you want
$node->title = "Your title goes jere";
$node->language = LANGUAGE_NONE; // Or any language code if Locale module is enabled. More on this below *
$node->uid = 1; // Or any id you wish
$node->path = array('alias' => 'your node path'); // Setting a node path
node_object_prepare($node); // Set some default values.
$node->body[$node->language][0]['value'] = 'This is a body text';
$node->body[$node->language][0]['summary'] = 'Here goes a summary';
$node->body[$node->language][0]['format'] = 'filtered_html'; // If field has a format, you need to define it. Here we define a default filtered_html format for a body field
Lưu node
$node = node_submit($node); // Prepare node for a submit
node_save($node); // After this call we'll get a nid