[英]Drupal customize node add form
I am creating a custom module that allows for users to create blog posts that will be private and related to a project. 我正在创建一个自定义模块,该模块允许用户创建私有的与项目相关的博客文章。 So I created a new content_type called "tasker_blog" that just has a title and body.
因此,我创建了一个新的content_type,称为“ tasker_blog”,其中只有标题和正文。 The user goes to view their projects and clicks a link which takes them to 'tasker_project/%/blog' % being the id of the project.
用户去查看他们的项目并单击一个链接,将其带到'tasker_project /%/ blog'%作为项目的ID。 I have the form being displayed correctly but when I submit the form I have two problems, 1) the title is lost on submit 2) getting this error: EntityMalformedException: Missing bundle property on entity of type node.
我有正确显示的表单,但是提交表单时遇到两个问题:1)提交时标题丢失了2)得到此错误:EntityMalformedException:类型节点的实体上缺少捆绑属性。 in entity_extract_ids().
在entity_extract_ids()中。
Here is the code I have written (some non-relevant parts taken out). 这是我编写的代码(删除了一些不相关的部分)。
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;
}
If I submit with nothing else added I get the error I mentioned above. 如果我未添加任何其他内容而提交,则会收到上面提到的错误。 If I add this (using Kint to display variables):
如果我添加此代码(使用Kint显示变量):
function tasker_project_node_validate($node, $form, &$form_state) {
s($_REQUEST);
s($form_state['values']);
die();
}
$_REQUEST array output: $ _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 output: $ 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
)
So I'm not sure if the two problems are related or not. 所以我不确定这两个问题是否相关。 Once the node is saved I am going to add a row to a custom table that store the nid and the project_id.
保存节点后,我将向自定义表中添加一行以存储nid和project_id。
Found the solution if anyone else ever has this issue: 如果有人遇到此问题,请找到解决方案:
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),
);
I used drupal_retrieve_form and drupal_prepare_form and everything seems to work correctly now. 我使用了drupal_retrieve_form和drupal_prepare_form,现在似乎一切正常。 Not sure why drupal_build_form or drupal_get_form don't work correctly.
不知道为什么drupal_build_form或drupal_get_form无法正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.