开发者

How do I insert a custom form into a Drupal node view and process the node's data on submission?

开发者 https://www.devze.com 2023-03-19 08:45 出处:网络
We are importing our sales orders from our accounting system into a Drupal 6 site. We setup a content type called orders.

We are importing our sales orders from our accounting system into a Drupal 6 site.

We setup a content type called orders.

I would like to build a shipping module for our shipping provider (no there isnt already one).

I understand how to build "block" modules and "content" modules.

What is the best approach to build a module that would appear at the bottom of our order nodes that presents a form they need to complete (which in turn would process the shipping)

If possib开发者_StackOverflow社区le I would like to do this without creating a different content type


If you want to attach a custom form to every node, first you need (in your module) to:

  • write the function that will build the form (see: Generating Forms). If this function will display node-related data according to the node that's currently being viewed, then you'll probably want to pass the nid or the whole $node as an argument.

Then you have to decide how you want to go about inserting the form into the node page (or however it is that the node is being viewed), and there's a couple of ways to go about it, including:

  1. Implementing hook_nodeapi() and within the "view" operation (i.e $op == 'view') you could get your form and add it to $node->content.
  2. Adding the form as a template variable (see: template_preprocess_node(), you would do a MYMODULE_preprocess_node(&$vars)) so you can put it wherever you want in your custom node.tpl.php.

I guess there are more ways to go about it (having your module generate a block with the form in it, etc.), it depends on what you specifically need.

Whichever you choose, the way you get the markup for your custom form is:

<?php
// something like this for hook_nodeapi() way.
$node->content['body']['value']] .= drupal_get_form('my_custom_form_generating_function', $node);
// or something like this for the hook_preprocess_node() way.
$vars['my_custom_form'] = drupal_get_form('my_custom_form_generating_function', $vars['node']);

If your form-generating function will indeed accept the $node as a parameter, then you'll need to either use PHP's func_get_args() or explicitly declare the $node argument in the signature:

<?php
function my_custom_form_generating_function($form_state, $node) { }

And to be clear:

...to build a module that would appear at the bottom of our...

Drupal modules are not things that "appear". Modules are extensions that provide additional functionality to your Drupal site. They can implement hooks which may or may not end up generating markup that will "appear" on you site.

Regarding "content modules" and "block modules": Even though I've seen the term "node module" to refer to modules that provide new content types, any module can implement any hook, and thus provide blocks, content types, etc.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号