Drupal自定义节点添加表单

时间:2012-09-07 18:09:58

标签: php drupal module

我正在创建一个自定义模块,允许用户创建私有且与项目相关的博客帖子。所以我创建了一个名为“tasker_blog”的新content_type,它只有一个标题和正文。用户查看他们的项目并单击一个链接,该链接将他们带到'tasker_project /%/ blog'%作为项目的ID。我有正确显示的表单,但当我提交表单时,我有两个问题,   1)提交时标题丢失   2)收到此错误:EntityMalformedException:在类型node的实体上缺少bundle属性。在entity_extract_ids()中。

这是我写的代码(取出了一些不相关的部分)。

function tasker_project_menu() {
    $items = array();

    $items['tasker_project/%/blog'] = array(
        'title' => 'Private Blog Post',
        'page callback' => 'drupal_get_form',
        'page arguments' => array('tasker_project_blog_form',1),
        'access arguments' => array('view tasker projects'),
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function tasker_project_blog_form($form, &$form_state) {
    drupal_add_css(drupal_get_path('module', 'tasker_project') . '/tasker_project.css'); 
    global $user;
    module_load_include('inc', 'node', 'node.pages');
    $node = (object) array(
        'uid' => $user->uid,
        'name' => (isset($user->name) ? $user->name : ''),
        'type' => 'tasker_blog',
        'language' => 'und',
    );
    node_object_prepare($node);

    $form = drupal_get_form('tasker_blog_node_form',$node);
    $form['hidden_project_id'] = array(
        '#type' => 'hidden',
        '#value' => arg(1),
    );

    return $form;
}

如果我提交时没有添加任何其他内容,我会收到上面提到的错误。如果我添加它(使用Kint显示变量):

function tasker_project_node_validate($node, $form, &$form_state) {
    s($_REQUEST);
    s($form_state['values']);
    die();
}

$ _ REQUEST数组输出:

array (9) (
    'title' => string (4) "test"
    'body' => array (1) (
        'und' => array (1) (
             array (2) (
                'format' => string (13) "filtered_html"
                'value' => string (13) "<p>test</p>
"
            )
        )
    )
    'changed' => string (0) ""
    'form_build_id' => string (48) "form-eKWwyFlBOzi4LsajaAiEZBG7J0uOSI1UDVIhiDomeJE"
    'form_token' => string (43) "N421-IiWecixBJGbxTHGcJAIrd6ZutzAW0LAtVSsrJ4"
    'form_id' => string (24) "tasker_project_blog_form"
    'hidden_project_id' => string (2) "10"
    'additional_settings__active_tab' => string (0) ""
    'op' => string (4) "Save"
)

$ form_state ['values']数组输出:

array (27) (
    'nid' => NULL
    'vid' => NULL
    'uid' => string (1) "5"
    'created' => integer 1347038959
    'type' => string (11) "tasker_blog"
    'language' => string (3) "und"
    'changed' => string (0) ""
    'title' => string (0) ""
    'additional_settings__active_tab' => string (0) ""
    'revision' => bool FALSE
    'log' => string (0) ""
    'name' => string (10) "ndenlinger"
    'date' => string (0) ""
    'status' => integer 0
    'promote' => integer 0
    'sticky' => integer 0
    'submit' => string (4) "Save"
    'preview' => string (7) "Preview"
    'body' => array (1) (
        'und' => array (1) (
             array (3) (
                'summary' => string (0) ""
                'format' => string (13) "filtered_html"
                'value' => string (13) "<p>test</p>
"
            )
        )
    )
    //...MORE HERE REMOVED SINCE NOT RELEVANT
)

所以我不确定这两个问题是否相关。保存节点后,我将向存储nid和project_id的自定义表中添加一行。

1 个答案:

答案 0 :(得分:0)

如果有其他人遇到此问题,请找到解决方案:

    drupal_add_css(drupal_get_path('module', 'tasker_project') . '/tasker_project.css'); 
    global $user;
    module_load_include('inc', 'node', 'node.pages');
    $node = (object) array(
        'uid' => $user->uid,
        'name' => (isset($user->name) ? $user->name : ''),
        'type' => 'tasker_blog',
        'language' => 'und',
    );
    node_object_prepare($node);
    $form_state['build_info']['args'] = array($node);

    $form = drupal_retrieve_form('tasker_blog_node_form', $form_state);
    drupal_prepare_form('tasker_blog_node_form', $form, $form_state);
    $form['hidden_project_id'] = array(
        '#type' => 'hidden',
        '#value' => arg(1),
    );

我使用了drupal_retrieve_form和drupal_prepare_form,现在一切似乎都正常了。不确定为什么drupal_build_form或drupal_get_form无法正常工作。