drupal_get_form不传递节点数组

时间:2010-05-12 19:25:47

标签: drupal-6 drupal-modules

我无法让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)

重合
    

2 个答案:

答案 0 :(得分:1)

这可能不是它,但我看到你首先使用$node作为对象(标题),然后使用make_author_form()方法作为数组(获取语言)。如果$node是一个对象,那么这就解释了为什么您无法检索$node['language']

我不确定我是否完全明白你要做什么,但我认为使用页面参数是个好主意。

function mymodule_form_alter($form_id, &$form) {
  // If $form_id is {node->type}_node_form
  // Then, check for the first argument in the URL and hide/show Preview accordingly
}

答案 1 :(得分:1)

原来是make_author_form函数第4行的基本编程错误。我自己将$ node变量归零。