1

I'm trying to create my first module which essentially is a visitor sign-in system where each visitor is stored as a custom content type.

The user will be a visitor to our building who's able to input their name and who they are here to see (autocomplete field based on taxonomy terms). My custom module will then add a timestamp to the node, as well as email the person they are here to see.

Code is below:

function visitor_email_staff_form_alter(&$form, &$form_state, $form_id){ if ($form_id == 'visitor_sign_in_node_form'){ $form['actions']['submit']['#submit'][0] = 'visitor_email_staff_my_custom_submit_handler'; } } function visitor_email_staff_my_custom_submit_handler($form, &$form_state){ $guestName = $form['field_visitors_name']['und'][0]['value']['#value']; $staffName = $form['field_tags']['und']['#value']; $visitorTitle = $guestName.' - '.time(); $staffTaxonomy = taxonomy_get_term_by_name($staffName); foreach ($staffTaxonomy as $email) { $staffEmail = $email->description; // email address } $form_state = array(); $form_state['values']['title'] = $visitorTitle; $form_state['values']['field_visitors_name'] = $guestName; $form_state['values']['field_tags'] = $staffName; $form_state['values']['field_timestamp_in'] = time(); // timestamp $form_state['values']['field_timestamp_out'] = ''; $form_state['values']['op'] = t('Save'); drupal_form_submit('visitor_sign_in_node_form',$form_state); } 

The major issue I'm having at the moment is getting the data to save and submit... I keep getting the below errors:

Warning: Missing argument 3 for node_form() in node_form() (line 107 of C:\www\xxx\modules\node\node.pages.inc). Warning: Creating default object from empty value in node_form() (line 115 of C:\www\xxx\modules\node\node.pages.inc). Notice: Undefined property: stdClass::$type in node_object_prepare() (line 981 of C:\www\xxx\modules\node\node.module). Notice: Undefined property: stdClass::$type in _node_extract_type() (line 379 of C:\www\xxx\modules\node\node.module). Notice: Undefined property: stdClass::$type in comment_node_prepare() (line 1298 of C:\www\xxx\modules\comment\comment.module). Notice: Undefined property: stdClass::$type in menu_node_prepare() (line 568 of C:\www\xxx\modules\menu\menu.module). Notice: Undefined property: stdClass::$type in _node_extract_type() (line 379 of C:\www\xxx\modules\node\node.module). Notice: Undefined property: stdClass::$type in node_form() (line 294 of C:\www\xxx\modules\node\node.pages.inc). Notice: Undefined property: stdClass::$type in node_form() (line 300 of C:\www\xxx\modules\node\node.pages.inc). Notice: Undefined property: stdClass::$type in node_form() (line 321 of C:\www\xxx\modules\node\node.pages.inc). EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() (line 7922 of C:\www\xxx\includes\common.inc). 

Am I going about this the wrong way? Should I be using something else instead of drupal_form_submit()?

3
  • 2
    As a first clue don't use $form['actions']['submit']['#submit'][0] use $form['actions']['submit']['#submit'][] instead. Commented Nov 7, 2016 at 16:44
  • Can I ask why the latter is better? Commented Nov 9, 2016 at 10:29
  • Because you don't want to overwrite the default submit handler of the form, you want to add one more? Read this for more details. Commented Nov 9, 2016 at 11:48

1 Answer 1

0

Your close, however, I think there are two changes you'll need to make.

  1. You'll need to add $form_state['bundle'] = "visitor_sign_in" to your $form_state array()
  2. You'll need to pass an (object) $node as the third parameter to drupal_form_submit.

The changes might look something like this:

function visitor_email_staff_form_alter(&$form, &$form_state, $form_id){ if ($form_id == 'visitor_sign_in_node_form'){ $form['actions']['submit']['#submit'][0] = 'visitor_email_staff_my_custom_submit_handler'; } } function visitor_email_staff_my_custom_submit_handler($form, &$form_state){ $guestName = $form['field_visitors_name']['und'][0]['value']['#value']; $staffName = $form['field_tags']['und']['#value']; $visitorTitle = $guestName.' - '.time(); $staffTaxonomy = taxonomy_get_term_by_name($staffName); foreach ($staffTaxonomy as $email) { $staffEmail = $email->description; // email address } $form_state = array(); $form_state['values']['bundle'] = "visitor_sign_in"; $form_state['values']['title'] = $visitorTitle; $form_state['values']['field_visitors_name'] = $guestName; $form_state['values']['field_tags'] = $staffName; $form_state['values']['field_timestamp_in'] = time(); // timestamp $form_state['values']['field_timestamp_out'] = ''; $form_state['values']['op'] = t('Save'); drupal_form_submit('visitor_sign_in_node_form', $form_state, (object)$node); 

}

1
  • Thank you for your help! However, this now throws a new error (in addition to those above): Notice: Undefined variable: node in visitor_email_staff_my_custom_submit_handler() (line 69 of C:\www\xxx\sites\all\modules\zzz\visitor_email_staff\visitor_email_staff.module). Commented Nov 9, 2016 at 10:33

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.