vấn đề về node trong drupal 7 .

Events happening in the community are now at Drupal community events on www.drupal.org.
nhocmuonlanhoc's picture

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_node

vấ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

nhocmuonlanhoc's picture

$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

nhuhoan's picture

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

nhocmuonlanhoc's picture

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

nhocmuonlanhoc's picture

$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

nhocmuonlanhoc's picture

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

nhocmuonlanhoc's picture

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

jonescuong's picture
  1. 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

  2. Lưu node
    $node = node_submit($node); // Prepare node for a submit
    node_save($node); // After this call we'll get a nid