0

我正在创建一个自定义模块,该模块允许用户创建与项目相关的私有博客文章。所以我创建了一个名为“tasker_blog”的新内容类型,它只有一个标题和正文。用户去查看他们的项目并单击一个链接,该链接将他们带到 'tasker_project/%/blog'% 作为项目的 id。我的表单正确显示,但是当我提交表单时,我遇到了两个问题,1) 提交时标题丢失 2) 收到此错误:EntityMalformedException: Missing bundle property on entity of type node。在 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 的自定义表添加一行。

4

1 回答 1

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 不能正常工作。

于 2012-09-10T15:10:03.117 回答