Recently a client needed to have a mini form for creating specific types of nodes on the fly from anywhere on the site. So, instead of the user clicking "Create content" then "foo" then filling in the form, then clicking submit, a form for that type of content will be shown on most pages in a block in the side bar. So, they can create that type of node quickly.
There are two ways to do this:
- Call drupal_get_form('nodetype_node_form'), then use hook_form_alter() to unset the parts you don't need.
- Create your own form, then do a drupal_execute() using the data from your form.
Calling the node type node form
For this, assuming you have a content type called foo, then you do the following:
function custom_foo_node_form() { global $user; $node = array( 'uid' => $user->uid, 'name' => $user->name, 'type' => 'foo', ); return drupal_get_form('foo_node_form', $node); } function custom_form_alter($form_id, &$form) { if ($form_id == 'foo_node_form') { if ($_GET['q'] != 'node/add/foo') { $values = array( 'menu', 'attachments', 'preview', 'options', 'author', 'log', 'body_filter', 'path', ); foreach($values as $k) { if (isset($form[$k])) { unset($form[$k]); } } } } }
You then place the following in a block with type PHP or anywhere in the theme's .tpl.php you wish.
<?php print custom_foo_node_form() ?>
Using your own form with drupal_execute()
However, in order to keep the mini form clean and not too busy, we opted for the second option.
For this, the code will look like this:
function custom_foo() { return drupal_get_form('custom_foo_form'); } function custom_foo_form() { $form['title'] = array( '#type' => 'textfield', '#title' => 'foo', ); $form['taxonomy'] = taxonomy_form(1); $form['submit'] = array( '#type' => 'submit', '#value' => 'Foo', ); return $form; } function custom_foo_form_submit($form_id, $form) { global $user; $form_id = 'foo_node_form'; $node = array( 'uid' => $user->uid, 'name' => $user->name, 'type' => 'foo', ); $form_values = array( 'title' => $form['title'], 'name' => $user->name, 'taxonomy' => $form['taxonomy'], ); watchdog('debug', 'Saving foo from mini node form'); drupal_execute($form_id, $form_values, $node); }
Note that there is no body for this type of node, as it is a CCK type with an optional body. Only the title is relevant. But of course you can include a body if you so wish.
Then, in a block of type PHP or anywhere in your tpl.php files, you place the following call:
<?php print custom_foo() ?>
Thanks to Karoly Negyesi (chx) for his advice on the options available.
Comments
Khalid
Programmatic CCK creation
Mon, 2007/07/23 - 22:19Josh Koenig also points out that creating CCK nodes without a user interface at all (e.g. from an install profile) is now possible.
--
2bits -- Drupal and Backdrop CMS consulting
Visitor (not verified)
There is also shortform module
Tue, 2007/07/24 - 00:33If the core set of fields is basically the same this is a great way of shortening them so that the form can be displayed in the sidebar:
http://drupal.org/project/shortform
Alternatively you can just use it as a helper function in your theme - i.e. unset the fields you want, and then pass the result through shortform.
Visitor (not verified)
Can't find any release of
Tue, 2007/07/24 - 04:45Can't find any release of shortform module.
As I see, the shortform module isn't released jet :-(
Visitor (not verified)
Very nice
Tue, 2007/07/24 - 07:08Excellent!
How big a step is this (and programmatic creation of nodes) toward inline creation of nodes (say I'm creating a user profile node and I can create my organization profile node at the same time if it doesn't exist already)?
And it seems to get Drupal closer to my other dream: registering (or logging into) a site simultaneous with posting content for the first time...
Here's hoping, though of course this is good in itself.
Visitor (not verified)
Thank you for this
Thu, 2007/11/01 - 11:09Thank you for this article!
Seems in second way taxonomy select doesn't work properly - always submits first term from dropdown list. How can I fix it?
Visitor (not verified)
Taxonomy
Wed, 2008/05/21 - 10:30Yeah the taxonomy part doesn't seem to be working on the part 2 thing also when doing stuff with CCK you need to have this for the fields:
$form_values['field_foo'][0]['value'] = $form['fraga'];
Visitor (not verified)
can not save the taxonomy
Thu, 2010/11/04 - 05:54can not save the taxonomy term which be selected when submit the form, any tips should be thankful
/** add article node/add form **/
function modulename_article() {
return drupal_get_form('modulename_article_form');
}
function modulename_article_form() {
$form['title'] = array(
'#type' => 'textfield',
'#title' => 'article',
'#size' => 30,
);
$form['body'] = array(
'#type' => 'textarea',
'#title' => 'body',
'#size' => 30,
);
$form['taxonomy'] = taxonomy_form(6);
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Post',
);
return $form;
}
function modulename_article_form_submit($form_id, $form) {
global $user;
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
$node = array('type' => 'article');
$form_state['values']['taxonomy'] = $form['values']['taxonomy'][1];
$form_state['values']['title'] = $form['values']['title'];
$form_state['values']['body'] = $form['values']['body'];
$form_state['values']['name'] = $user->name;
$form_state['values']['op'] = t('Save');
drupal_execute('article_node_form', $form_state, (object)$node);
}
Visitor (not verified)
free tagging code
Mon, 2007/12/03 - 08:43great snippets. I tried the free tagging, but don't know the exact code for that. Any help would be appreciated. Thanks.
Visitor (not verified)
Also for normal nodes (not cck)
Fri, 2007/12/07 - 04:17Hi,
Is this snippet also to be used with links_weblink (weblinks-node)?
It would be great to be able to add nodes on taxonomy screen level.
Please respond, or may be the code to use this snippet for the weblinks-node?
Thanks in advance!
greetings,
Martijn
Visitor (not verified)
I did not get. Is there
Thu, 2008/01/10 - 14:52I did not get. Is there sniplet or module?
Pages