2

I have the following content types:

  • event
  • signup

I'm dealing with my_singnup module, for which I wish to:

  1. allow user to add signup content (same as core node/add/signup)
  2. using signup/event/%eventid path (it is simple link expose in every event)
  3. presenting (while adding) some information from event in left side and node/add/signup form on the right side
  4. preventing signup/event without argument to run

The code I have already wrote is the following.

function my_singnup_menu() {
   $items = array();
   $items['signup/event'] = array(
     'title' => 'Sorry, no such event.',
     'page callback' => 'There is no event id in path.',
     'type' => MENU_CALLBACK,
     'access callback' => TRUE // for now
   );

   $items['signup/event/%eventid'] = array(
     'title' => 'Signing up for event',
     'title callback' => 'my_signup_title',
     'title arguments' => array(2),
     'page callback' => 'my_singnup',
     'page arguments' => array(2),
     'type' => MENU_CALLBACK,
     'access callback' => TRUE // for now
   );
   return $items;
}

function my_signup_title($node) {
  if ($node->type == 'event') {
  drupal_set_title(t('Signing up for event '.$node->title));
  }
  if ($node->type <> 'event') {
  drupal_set_title(t('Id in path is not event id.'));
  }
  return t('Signing up for event');
}

function my_signup($node = FALSE) {
   if($node) {
     if($node->type == 'event') {
       module_load_include('inc', 'node', 'node.pages'); //Added
       global $user; //Added
       $new_node = new stdClass; //Added
       $new_node->type = 'signup'; //Added - here was bug
       $new_node->language = LANGUAGE_NONE; //localization
       $new_node->user = $user->uid; //Added
       $new_node->name = (isset($user->name) ? $user->name : ''); //Added
       $form = drupal_get_form('signup_node_form', $new_node); //here was my big trouble
       return $form;
     }
   }
   return drupal_not_found();
}

function my_signup_theme($existing, $type, $theme, $path) {
  return array(
    'signup_node_form' => array(
      'render element' => 'form',
      'template' => 'signup-node-form',
      'path' => drupal_get_path('module', 'my_signup'),
    ),
  );
}

Right now, I see some form under event/signup/999 (thats good), but there is no custom groups and fields like in core default node/add/signup. I was so happy to see it that I forget to check it looks the same. I see only core vertical tabs such as Menu, Book, Version, Adress, Comments, and there is no custom fields and groups in it. It is strange.

1 Answer 1

1

node_form() is in node.pages.inc which isn't included by default. Just add a call like this to the beginning of your my_signup() function:

module_load_include('inc', 'node', 'node.pages');

When you call drupal_get_form() with a specific type (like signup_node_form) the node module internally translates that to node_form(). This happens in node_forms() which is in the node.module file, and as such is always included.

However node.pages.inc is only included when specifically requested by a module/menu callback etc. so to use node_form() you'll need to include the file manually.

You also need to pass a node object to the function with the required content type assigned like the following code:

$new_node = new stdClass;
$new_node->type = 'signup';
$new_node->language = LANGUAGE_NONE;
$form = drupal_get_form('signup_node_form', $new_node);
7
  • So now I have to deal with missing arg, propably user... Mar 15, 2012 at 0:09
  • @GregoryZbinsky I should have mentioned that sorry, I've updated the answer
    – Clive
    Mar 15, 2012 at 0:15
  • Almost, I can see the form, there is only one object property to be set - language. Notice: Undefined property: stdClass::$language w node_form(). And this will be very, very useful answer for all Drupalist Mar 15, 2012 at 0:26
  • I've added the language part to the answer too, forgot about that one :)
    – Clive
    Mar 15, 2012 at 0:41
  • There is one big ooops I've missed, when I was so happy to see signup_node_form: there is no custom groups and fields, only core... gosh! Mar 15, 2012 at 1:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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