drupal_get_form 没有传递节点数组

发布于 2024-09-01 11:54:08 字数 4383 浏览 5 评论 0原文

我无法让 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

燃情 2024-09-08 11:54:08

可能不是这样,但我看到您首先在 make_author_form() 中使用 $node 作为对象(标题),然后作为数组(以获取语言) > 方法。如果 $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
}

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 the make_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.

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
}
一紙繁鸢 2024-09-08 11:54:08

事实证明,这是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文