Need help to display node entry form in a block or page template

nrackleff - July 21, 2009 - 22:52

Hello, I have created a content type that has just one field - the body. I am using the auto node title module to create the title so the user doesn't have to. I need to be able to allow users to create nodes of this type without going to the traditional node entry screen. I would like to put a little form in the right column of my page template to allow users to quickly enter these nodes. It's kind of like a facebook status update, but not something I can use the facebook like status module for.

How does one go about doing this? Do I need to create a module or can I use PHP code in my page template? Is there an example of this somewhere that someone could show me to help me get started?

Thanks for your help,
-Nancy

You might try the Form Block

nevets - July 22, 2009 - 00:35

You might try the Form Block module.

Need more control over the display

nrackleff - July 23, 2009 - 16:25

Thanks for the suggestion. I just did a test, and the Form Block module is nice, but I'm afraid I need more control over the display. For this form, I only want to display the required fields, not the author info, publishing options etc. I believe I am familiar enough with PHP to do this if someone could point me in the right direction or show me an example to get me started. Thanks for your help. -Nancy

Try the form when you are not

nevets - July 23, 2009 - 18:16

Try the form when you are not logged in as user 1. The fields you mentioned are all controlled by permissions and generally not visible unless you grant a particular role that ability.

...

roopletheme - July 23, 2009 - 19:20

As nevets points out, the Form Block module might be all you need. But like you, I've found that I wanted more control over the block display. I've created code similar to what you're looking for on a few sites, so perhaps this will help...

You're going to need a module that is 'smart' about your custom content type. I'll assume you created a content type with a (machine readable) type of 'blurb'. Let's create a module called 'blurb' to give us the block. We'll need a folder called 'blurb' that contains two files: the info file, and the module file. The info file should have the filename 'blurb.info', and it's simple enough:

name = Blurb
description = Blurb Block
core = 6.x

The other file is the module itself, which should have a filename of 'blurb.module'. It will look something like this:

<?php
function blurb_block($op='list',$delta=0,$edit=array()) {
 
$block = array();
  if(
$op == 'list') {
   
$block['0']  = array(
     
'info' => t('Blurb Block'),
    );
    return
$block;
  } else if(
$op == 'view') {           
    switch (
$delta) {
    case
0:
     
$block['subject'] = t('Blurb Block');
     
$block['content'] =  drupal_get_form('blurb_blurbform');
      return
$block;
      break;
    }
  }
}

function
blurb_blurbform($form_state) {
 
$form = array();

 
$form['blurb'] = array(
   
'#title' => t('Your Blurb'),
   
'#type' => 'textfield',
   
'#size' => '25',
  );
 
$form['submit'] = array(
   
'#type' => 'submit',
   
'#value' => t('Submit')
  );

  return
$form;
}

function
blurb_blurbform_submit($form, &$form_state) {
  if (
$form_state['values']['blurb']) {
    global
$user;
   
module_load_include('inc', 'node', 'node.pages');
   
$node = array('type' => 'blurb');
   
$blurb_form_state = array();
   
$blurb_form_state['values']['name'] = $user->name;
   
$blurb_form_state['values']['body'] = $form_state['values']['blurb'];
   
$blurb_form_state['values']['op'] = t('Save');
   
drupal_execute('blurb_node_form', $blurb_form_state, (object)$node);
  }
}
?>

The blurb_block function lets Drupal know about our block. The blurb_blurbform function is the actual block that gets displayed. Here's where you would customize the format to your liking. For example, I'm using a textfield instead of a textarea for the node body. The blurb_blurbform_submit function gets called whenever the user clicks the submit button, and it creates our new blurb node using the body value entered by the user.

Typically, I would include a field for the title in blurb_blurbform function, which I would assign to the node title in the blurb_blurbform_submit function. But I've omitted it here because of your mentioning the auto-node-title thing.

If your content type is something other than 'blurb', you'll need to change a couple of lines of code. For example, if the content type is 'shout', then change the line:

    $node = array('type' => 'blurb');

to be:

    $node = array('type' => 'shout');

and change the line:

    drupal_execute('blurb_node_form', $blurb_form_state, (object)$node);

to read:

    drupal_execute('shout_node_form', $blurb_form_state, (object)$node);

Copy the blurb folder to your sites/all/modules folder, enable the module, and then go to your blocks page and assign the blurb block to a region.

Homework:
Writing a Block Module
The Forms API
Programatically creating nodes

This looks great

nrackleff - July 23, 2009 - 19:52

Thanks so much. This is very thorough and I believe I can do this. I have been reading the Pro Drupal Development book and understand the basics of creating modules. I will be sure to read your "homework" suggestions too. I'll start work on this item next week. I think my only stumbling block might be that I also have to include the options from the user relationships module that allows the person filling out the form to specify whether to post it to their friends and/or followers. I should easily be able to create the checkboxes, but I'll probably have to crack open the user relationships node access module code to see how that data gets added when submitting the form.

Thanks again to both nevets and roopletheme for all your help. I've only been working with Drupal for a few months and I am very impressed with the community and everyone's willingness to help. -Nancy

Need help

binoykl - October 16, 2009 - 01:18

I have similar scenario. http://drupal.org/node/604490

How do I get the results of the following query into the form?

function taxonomy_get_term($tid) {
  static $terms = array();

  if (!isset($terms[$tid])) {
    $terms[$tid] = db_fetch_object(db_query('SELECT * FROM {term_data} WHERE tid = 7', $tid));
  }

  return $terms[$tid];
}

The form

function blurb_blurbform_submit($form, &$form_state) {
  if ($form_state['values']['blurb']) {
    global $user;
    module_load_include('inc', 'node', 'node.pages');
    $node = array('type' => 'questions');
    $blurb_form_state = array();
    $blurb_form_state['values']['name'] = $user->name;
    $blurb_form_state['values']['title'] = $form_state['values']['title'];
    $blurb_form_state['values']['op'] = t('Save');
    drupal_execute('questions_node_form', $blurb_form_state, (object)$node);
  }
}

 
 

Drupal is a registered trademark of Dries Buytaert.