drupal_get_form 没有传递节点数组
我无法让 drupal_get_form 传递节点数据。代码片段如下。 drupal_get_form 文档 (api.drupal.org) 声明它将传递额外的参数。我基于未传递的节点数据,因为(显然) $node['language'] 未在 hook_form 中定义,这导致 $form['qqq'] 未创建,因此预览按钮显示。
我的目标是使用路径“node/add/author”显示预览按钮,但不显示“milan/author/add”。实现这一目标的任何替代方法都会有所帮助,但我想回答的问题在前一段中。我读过的所有内容都表明它应该有效。
此菜单项
$items['milan/author/add'] = array( 'title' => 'Add Author', 'page callback' => 'get_author_form', 'access arguments' => array('access content'), 'file' => 'author.pages.inc', );
调用此代码
function get_author_form() { //return node_form(NULL,NULL); //return drupal_get_form('author_form'); return author_ajax_form('author'); } function author_ajax_form($type) { global $user; module_load_include('inc', 'node', 'node.pages'); $types = node_get_types(); $type = isset($type) ? str_replace('-', '_', $type) : NULL; // If a node type has been specified, validate its existence. if (isset($types[$type]) && node_access('create', $type)) { // Initialize settings: $node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => 'bbb','bbb' => 'TRUE'); $output = drupal_get_form($type .'_node_form', $node); } return $output; }
这是 hook_form 和 hook_form_alter 代码
function author_form_author_node_form_alter(&$form, &$form_state) { $form['author']=NULL; $form['taxonomy']=NULL; $form['options']=NULL; $form['menu']=NULL; $form['comment_settings']=NULL; $form['files']=NULL; $form['revision_information']=NULL; $form['attachments']=NULL; if($form["qqq"]) { $form['buttons']['preview']=NULL; } } function author_form(&$node) { return make_author_form(&$node); } function make_author_form(&$node) { global $user; $type = node_get_types('type', $node); $node = author_make_title($node); drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t($node->title), 'node/' . $node->nid))); $form['authorset'] = array( '#type' => 'fieldset', '#title' => t('Author'), '#weight' => -50, '#collapsible' => FALSE, '#collapsed' => FALSE, ); $form['author_id'] = array( '#access' => user_access('create pd_recluse entries'), '#type' => 'hidden', '#default_value' => $node->author_id, '#weight' => -20 ); $form['authorset']['last_name'] = array( '#type' => 'textfield', '#title' => t('Last Name'), '#maxlength' => 60, '#default_value' => $node->last_name ); $form['authorset']['first_name'] = array( '#type' => 'textfield', '#title' => t('First Name'), '#maxlength' => 60, '#default_value' => $node->first_name ); $form['authorset']['middle_name'] = array( '#type' => 'textfield', '#title' => t('Middle Name'), '#maxlength' => 60, '#default_value' => $node->middle_name ); $form['authorset']['suffix_name'] = array( '#type' => 'textfield', '#title' => t('Suffix Name'), '#maxlength' => 14, '#default_value' => $node->suffix_name ); $form['authorset']['body_filter']['body'] = array( '#access' => user_access('create pd_recluse entries'), '#type' => 'textarea', '#title' => 'Describe Author', '#default_value' => $node->body, '#required' => FALSE, '#weight' => -19 ); $form['status'] = array( '#type' => 'hidden', '#default_value' => '1' ); $form['promote'] = array( '#type' => 'hidden', '#default_value' => '1' ); $form['name'] = array( '#type' => 'hidden', '#default_value' => $user->name ); $form['format'] = array( '#type' => 'hidden', '#default_value' => '1' ); // NOTE in node_example there is some addition code here not needed for this simple node-type $thepath='milan/author'; if($_REQUEST["theletter"]) { $thepath .= "/" . $_REQUEST["theletter"]; } if($node['language']) { $thepath='milan/authorajaxclose'; $form['qqq'] = array( '#type' => 'hidden', '#default_value' => '1' ); } $form['#redirect'] = $thepath; return $form; }
该菜单路径与此主题一致(PHPTemplate)
I have not been able to get drupal_get_form to pass on the node data. Code snippets are below. The drupal_get_form documentation (api.drupal.org) states that it will pass on the extra parameters. I am basing the node data not being passed because (apparently) $node['language'] is not defined in hook_form which causes $form['qqq'] not to be created and thus the preview button shows up.
My goal here is that the preview button show up using path "node/add/author" but doesn't show up for "milan/author/add". Any alternative methods for achieving this goal would be helpful but the question I want answered is in the preceding paragraph. Everything I've read indicates that it should work.
This menu item
$items['milan/author/add'] = array( 'title' => 'Add Author', 'page callback' => 'get_author_form', 'access arguments' => array('access content'), 'file' => 'author.pages.inc', );
calls this code
function get_author_form() { //return node_form(NULL,NULL); //return drupal_get_form('author_form'); return author_ajax_form('author'); } function author_ajax_form($type) { global $user; module_load_include('inc', 'node', 'node.pages'); $types = node_get_types(); $type = isset($type) ? str_replace('-', '_', $type) : NULL; // If a node type has been specified, validate its existence. if (isset($types[$type]) && node_access('create', $type)) { // Initialize settings: $node = array('uid' => $user->uid, 'name' => (isset($user->name) ? $user->name : ''), 'type' => $type, 'language' => 'bbb','bbb' => 'TRUE'); $output = drupal_get_form($type .'_node_form', $node); } return $output; }
And here is the hook_form and hook_form_alter code
function author_form_author_node_form_alter(&$form, &$form_state) { $form['author']=NULL; $form['taxonomy']=NULL; $form['options']=NULL; $form['menu']=NULL; $form['comment_settings']=NULL; $form['files']=NULL; $form['revision_information']=NULL; $form['attachments']=NULL; if($form["qqq"]) { $form['buttons']['preview']=NULL; } } function author_form(&$node) { return make_author_form(&$node); } function make_author_form(&$node) { global $user; $type = node_get_types('type', $node); $node = author_make_title($node); drupal_set_breadcrumb(array(l(t('Home'), NULL), l(t($node->title), 'node/' . $node->nid))); $form['authorset'] = array( '#type' => 'fieldset', '#title' => t('Author'), '#weight' => -50, '#collapsible' => FALSE, '#collapsed' => FALSE, ); $form['author_id'] = array( '#access' => user_access('create pd_recluse entries'), '#type' => 'hidden', '#default_value' => $node->author_id, '#weight' => -20 ); $form['authorset']['last_name'] = array( '#type' => 'textfield', '#title' => t('Last Name'), '#maxlength' => 60, '#default_value' => $node->last_name ); $form['authorset']['first_name'] = array( '#type' => 'textfield', '#title' => t('First Name'), '#maxlength' => 60, '#default_value' => $node->first_name ); $form['authorset']['middle_name'] = array( '#type' => 'textfield', '#title' => t('Middle Name'), '#maxlength' => 60, '#default_value' => $node->middle_name ); $form['authorset']['suffix_name'] = array( '#type' => 'textfield', '#title' => t('Suffix Name'), '#maxlength' => 14, '#default_value' => $node->suffix_name ); $form['authorset']['body_filter']['body'] = array( '#access' => user_access('create pd_recluse entries'), '#type' => 'textarea', '#title' => 'Describe Author', '#default_value' => $node->body, '#required' => FALSE, '#weight' => -19 ); $form['status'] = array( '#type' => 'hidden', '#default_value' => '1' ); $form['promote'] = array( '#type' => 'hidden', '#default_value' => '1' ); $form['name'] = array( '#type' => 'hidden', '#default_value' => $user->name ); $form['format'] = array( '#type' => 'hidden', '#default_value' => '1' ); // NOTE in node_example there is some addition code here not needed for this simple node-type $thepath='milan/author'; if($_REQUEST["theletter"]) { $thepath .= "/" . $_REQUEST["theletter"]; } if($node['language']) { $thepath='milan/authorajaxclose'; $form['qqq'] = array( '#type' => 'hidden', '#default_value' => '1' ); } $form['#redirect'] = $thepath; return $form; }
That menu path coincides with this theme (PHPTemplate)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
可能不是这样,但我看到您首先在
make_author_form()
中使用$node
作为对象(标题),然后作为数组(以获取语言) > 方法。如果$node
是一个对象,那么这就解释了为什么您无法检索$node['language']
。不确定我是否完全理解你想要做什么,但我认为使用页面参数是个好主意。
This might not be it but I see that you use
$node
as an object at first (title) and then as an array (to get the language) in themake_author_form()
method. If$node
is an object, then that explains why you cant retrieve$node['language']
.Not sure if I completely understand what you are trying to do but it would be a good idea to use page arguments for it, I think.
事实证明,这是 make_author_form 函数第 4 行的基本编程错误。我自己将 $node 变量清零。
Turned out to be a basic programming error in line 4 of the make_author_form function. I was zeroing out the $node variable myself.