3

I have a content type with the title and name fields. How can I the form for adding a node as block, in the front page?

I am using Drupal 7.

function mail_block_info() {
  $blocks = array();
  $blocks['mail_block'] = array(
    'info' => t('backend info left block'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}


function mail_block_view($block_name = '') {
  if ($block_name == 'mail_block') {
    $match = TRUE;
    $types = array('mailin_list_node_form' => 1);
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      $node = node_load(array('nid' => $nid));
      $type = $node->type;
      if (isset($types[$type])) {
        $match = FALSE;
      }
    }

    $contact_node = node_load($node);

    drupal_get_form($node);
    $block = array(
      'subject' => t(''),
      'content' => $content,
    );

    return $block;
  }
}
3
  • Do you mean you want the teaser of the node to be displayed? Or the full node? Or do you mean the node add form?
    – Clive
    Mar 15 '12 at 13:50
  • i mean node addform Mar 15 '12 at 13:51
  • As side note, it is useless to pass an empty string to t(): An empty string is an empty string in any language. There is no need to translate an empty string; just use '', instead of t('').
    – apaderno
    Mar 15 '12 at 15:39
1

Try this:

function mail_block_info() {
  $blocks = array();
  $blocks['mail_block'] = array(
    'info' => t('backend info left block'),
    'cache' => DRUPAL_NO_CACHE,
  );

  return $blocks;
}


function mail_block_view($block_name = '') {
  global $user;

  if ($block_name == 'mail_block') {
    // This is required if you want to show this block even if user profile page.
    module_load_include('inc', 'node', 'node.pages');

    // Create a node for the content type.
    $node = new stdClass();
    $node->uid = $user->uid;
    $node->name = $user->name;
    $node->type = 'CONTENT_TYPE_NAME';
    $node->language = LANGUAGE_NONE;
    // Create the form.
    $form = drupal_get_form('CONTENT_TYPE_NAME_node_form', $node);

    $block = array(
      'subject' => t(''),
      'content' => drupal_render($form),
    );

    return $block;
  }
}
2

Try node_add():

$content = node_add($type);

or:

$content = drupal_get_form($type . '_node_form', $node);

Also, please keep in mind that node_load() changed in D7. You should use it like this:

node_load($nid);
4
  • you mean node_load_multiple() Mar 15 '12 at 14:32
  • 1
    @MohamedIbrahim I'm pretty sure he means node_load...your code is: node_load($node), which might have been valid in Drupal 6 but not in Drupal 7
    – Clive
    Mar 15 '12 at 14:36
  • 1
    @Clive In Drupal 6, it would work if $node is an array; in Drupal 7, the first argument must be the node ID, as you said.
    – apaderno
    Mar 15 '12 at 15:44
  • Thanks @kiamlaluno I've forgotten most of D6 to be honest ;)
    – Clive
    Mar 15 '12 at 15:53
1

There are some errors in your code.

    $types = array('mailin_list_node_form' => 1);
    if (arg(0) == 'node' && is_numeric(arg(1))) {
      $node = node_load(array('nid' => $nid));
      $type = $node->type;
      if (isset($types[$type])) {
        $match = FALSE;
      }
    }

In Drupal 7, the first argument of node_load() is the node ID, not an array. The full code snippet is unnecessary complex. All that code can be replaced with a call to menu_get_object(), and to check the content type of a node you just compare $node->type with a string.

    $contact_node = node_load($node);

You are passing to node_load() an object you already obtained from node_load(). First, node_load() doesn't accept an object, in Drupal 7 (nor would it accept a node as first argument, in Drupal 6); secondly, it doesn't make sense to call node_load() when you have already obtained a node object from that function.
You initialize $contact_node, but that variable is not used from the rest of your code.

    drupal_get_form($node);

The parameters passed to drupal_get_form() are wrong; the first argument passed is the form ID, and you didn't pass that argument to the function. The result returned from the function is not saved; probably you were supposed to save it in the $content variable.

I would use code similar to the following one.

function mail_block_view($delta = '') {
  if ($delta == 'mail_block' && ($node = menu_get_object()) && $node->type == 'YOUR CONTENT TYPE') {
    $block = array(
      'subject' => '',
      'content' => drupal_get_form('YOUR CONTENT TYPE FORM ID', $node),
    );

    return $block;
  }
}

Replace 'YOUR CONTENT TYPE' with the ID of the content type, and 'YOUR CONTENT TYPE FORM ID' with the form ID for the form, which is normally similar to <your_content_type>_node_form, where <your_content_type> is the string you used for 'YOUR CONTENT TYPE FORM ID'.

To notice that it is not necessary to pass an empty string to t() because an empty string is still an empty string in all the languages; there is no need to translate it.

1

There is a module Form Block

Enables the presentation of user registration, site wide contact, or node creation forms in blocks. This is particularly useful for including forms on panels.

As its dev in D7 make sure you read the Issues for Form Block

0
function mail_block_view($block_name = '') {
  if ($block_name == 'mail_block') {
    $form = drupal_get_form('mailin_list_node_form');
    $block = array(
        'subject' => t(''),
        'content' => drupal_render($form),
    );
    return $block;
  }
}
8
  • I have used this code but it return me an error: "Undefined variable: node in mail_block_view()." Mar 15 '12 at 14:22
  • i think that the name of content type is wrong Mar 15 '12 at 14:31
  • are you sure your content type's machine name is called "mailin_list"? Not "mailing_list"? (notice the g after mailin). I got it from your original question.
    – Beebee
    Mar 15 '12 at 14:32
  • yes iam sure its called "mailin_list" , Mar 15 '12 at 14:41
  • 1
    The problem with the code is that the call to drupal_get_form('mailin_list_node_form') is missing an argument that is necessary: the node object. Without that, Drupal doesn't know for which node the form is being shown.
    – apaderno
    Mar 15 '12 at 15:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.