以编程方式保存内容类型上的数据

时间:2015-06-16 16:46:59

标签: drupal-7

我刚创建了一个内容类型“Cars”,如何以编程方式从表单中保存“Cars”数据?

function form_tutorial_my_form_submit($form, &$form_state){
    $title = $form_state['values']['title'];
    $body = $form_state['values']['body'];
    $field_color = $form_state['values']['field_color'];
    $nid = db_insert('cars')->fields(array(
        'title' => $title,
        'uid' => 1,
        'created' => REQUEST_TIME
    ))->execute();
}

1 个答案:

答案 0 :(得分:3)

如果要在

之后保存新节点
function form_tutorial_my_form_submit($form, &$form_state){
    $title = $form_state['values']['title'];
    $body = $form_state['values']['body'];
    $field_color = $form_state['values']['field_color'];
    $nid = db_insert('cars')->fields(array(
           'title' => $title,
           'uid' => 1,
           'created' => REQUEST_TIME
    ))->execute();

    // Save new node 
    $node = new stdClass();
    // Set node title
    $node->title = $title;
    // set node type ex: article etc
    $node->type = "YOUR_NODE_TYPE";
    // set node language
    $node->language = LANGUAGE_NONE;
    //(1 or 0): published or not
    $node->status = 1;
    //(1 or 0): promoted to front page
    $node->promote = 0; 
    $node->body['und'][0]['value'] = $body;
    $node->body['und'][0]['format'] = 'full_html';
    node_object_prepare($node);
    node_save($node);   
}

希望这适合你。