How to display node/add/sometype form on another p

2019-01-16 13:00发布

The whole problem is following:

Lets say we have Items, Items can have Bids, Items can have Questions and Question can have Answer.

When an Item is displayed, all content associated with this Item should also be displayed. Additionally depending on roles, certain forms to make Bids, ask Questions and replay Answers should be display.

How to achieve this? Should I have separate node type for each type? Or should I treat some subtypes like Questions and Answers as comments? Should I use some well-known modules for this?

I am using Drupal 7 and I tried to write a custom module but I didn't get it working properly.

9条回答
一纸荒年 Trace。
2楼-- · 2019-01-16 13:03

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.

查看更多
叛逆
3楼-- · 2019-01-16 13:03

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;
}
查看更多
走好不送
4楼-- · 2019-01-16 13:05
// 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);
查看更多
Melony?
5楼-- · 2019-01-16 13:11

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.

查看更多
放荡不羁爱自由
6楼-- · 2019-01-16 13:13

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
7楼-- · 2019-01-16 13:16
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',
查看更多
登录 后发表回答