How to display node/add/sometype form on another page?

核能气质少年 提交于 2019-11-28 04:21:54

To get a node edit form, you need to include node.pages.inc.

<?php
  // required for Drupal 6
  module_load_include('inc', 'node', 'node.pages'); 
  // which nodeform you want
  $node_type = 'YOURNODETYPE';
  $form_id = $node_type . '_node_form';
  // maybe add current users info
  global $user;
  // create a blank node
  $node = array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $node_type,
  );
  // Invoke hook_nodapi and hook_node
  node_object_prepare($node);
  // Or you can also use an exiting node, for example
  // $node = node_load(123);
  // and the display the form:
  $output = drupal_get_form($form_id, $node);
?>
module_load_include('inc', 'node', 'node.pages');

$form = node_add('nodetype');
$output = drupal_render($form);

If your node form has a file upload widget, you should add the following lines to the menu array:

'file path' => drupal_get_path('module', 'node'),
'file' => 'node.pages.inc',
Kendrick
// Drupal 7    
// Embed node creation form on a custom page inside module.
module_load_include('inc', 'node', 'node.pages');
$form = node_add('node_machine_name'); 
return drupal_render($form);
Thomas4019

The Module Form Block is the easiest way to embed a node form on a page. Then I would use views with a block display and an argument to show a tabular listing of these related nodes.

Although the Drupal 7 comment module is built on fields it really isn't quite flexible enough for non comment like things. If you want your sub-type to have a title and body then comments is probably the way to go. If you only want custom fields then a node is the way to go and possibly using something like Automatic Nodetitles.

Update for 2014: If adding a block without coding is preferred by anyone you may wish to check out Advanced Form Block module, which adds a few features to your standard blocks (you can add as many as you like, make them all submit via AJAX and even select which fields you want). Unlike the Form Block module it is still actively maintained for Drupal 7.

In Drupal 7 the blank node needs to be created as an object (not an array).

  $node->uid = $user->uid;
  $node->name = (isset($user->name) ? $user->name : '');
  $node->type = $node_type;
  $node->language = '';

Thomas's answer looks good to me: Formblocks and perhaps automatic nodetitles. I think you could expand on that with Nodereference URL Widget -- using nodereferences rather than comments, and letting that module do the work of keeping child-nodes connected to their parent.

To get dobeerman's example (the accepted answer) to work in d7, add 'language' => LANGUAGE_NONE and cast the $node array into an object. i.e.:

$node = (object)array(
  'uid' => $user->uid,
  'name' => (isset($user->name) ? $user->name : ''),
  'type' => $node_type,
  'language' => LANGUAGE_NONE
);

I am reproducing the solution which worked for me on my case. it is available as an answer on Drupal.org and it may help others with the same problem I was experiencing.

The answer is available here: https://www.drupal.org/node/1336212#comment-6192404.

I am copying-pasting it below:

in your custom callback OR in a hook_form_alter, call either...

<?php
form_load_include($form_state, 'inc', 'node', 'node.pages');
?>

...OR...

<?php
form_load_include($form_state, 'inc', 'user', 'user.pages');
?>

...depending on whether the core Drupal form you're loading is a node form or a user form.

This is how I solved the problem:

In my hook_menu

$items['add-visiteur'] = array(
  'title' => 'Add',
  'page callback' => 'add_visiteur',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK,
);

and my callback function

function add_visiteur() {
  module_load_include('inc', 'node', 'node.pages'); 
  // which nodeform you want
  $node_type = 'visiteur';
  $form_id = $node_type . '_node_form';
  // maybe add current users info
  global $user;
  // create a blank node
  $node = new stdClass;
  $node->uid = $user->uid;
  $node->name = (isset($user->name) ? $user->name : '');
  $node->type = $node_type;

  // Invoke hook_nodapi and hook_node
  node_object_prepare($node);

  $output = drupal_get_form($form_id, $node);
  return $output;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!