How to embed node add form in a block?

I have tried the following but it does not work. "free_listing2_node_form" is the form_id of the node add form I want to embed in this block.

If the approach below is right, I suspect problem in this statement "$block['content'] = drupal_get_form('free_listing2_node_form');"

Any help / direction is much appreciated!


<?php

 
function freelisting2_block_info() {
  $blocks['neil_recent'] = array(
    'info' => t('neil_Recent content'),
  );

  return $blocks;
}

function freelisting2_block_view($delta = '') {
  $block = array();

  switch ($delta) {
    case 'neil_recent':
      if (user_access('access content')) {
        $block['subject'] = t('Recent content');
		$block['content'] = drupal_get_form('free_listing2_node_form');
      }
      break;
  }
  return $block;
}


?>

Comments

nevets’s picture

I use the Form block module for such tasks

nilashis’s picture

@nevets - Have you used Form Block for drupal 7 too? Looks like a dev release - does it work well for you with D7?

nevets’s picture

It has worked for me in Drupal 7.

blairski’s picture

This is an old thread, but just in case it helps someone..

In the above code snippet, if you change drupal_get_form with node_add, it should work.

So:

$block['content'] = drupal_get_form('free_listing2_node_form');

Becomes:

$block['content'] = node_add('free_listing2_node_form');

nevets’s picture

node_add() takes a content type (not a form) as an argument and additionally it sets the page title which is probably not desired when using the form in a block.

himanshupathak3’s picture

Below is a demo function which returns the node type form.

/**
 * @param string $type
 * @return string
 */
function _custom_work_get_node_form($type = 'page') {
  global $user;
  module_load_include('inc', 'node', 'node.pages');
  $node = (object) array(
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => $type,
    'language' => LANGUAGE_NONE
  );
  $form = drupal_get_form($type . '_node_form', $node);
  return $form;
}

Himanshu