Programmatically add, validate, and theme a node

()

How to have a block display a form created with forms api, and how to theme it:

// add this to the block:
// «?php echo newsletter_signup() ?»

// add this code in (reddirt).module:

function newsletter_signup() {
 // Get our custom made form which will be added through drupal_execute
  $form = drupal_get_form('newsletter_signup_form');
  return $form;
}

function newsletter_signup_form_validate($form, &$form_state) {
  $email = $form_state['values']['email'];
  $birthdate = $form_state['values']['birthdate'];
  
  if (!valid_email_address($email)) {
    form_set_error('email', t('Please enter a valid email address.'));
  }
}

function signup_form_submit($form, &$form_state) {
  _save_signup($form_state);
  
  // this clears the status message 'node has been created'.
  drupal_get_messages('status');
 
  $form_state['redirect'] = 'confirm';
}

function _save_signup($form_state) {
  $values = array();
  module_load_include('inc', 'node', 'node.pages');  // new for Drupal 6
  
  $nodeTmp = array('type' => 'signup');
  // I have auto title turned on
  $values['values']['op'] = t('Save');  // Dr6 seems to be a required value
  
  // the username of the node's author, must be a valid user with 
  // access to create this node.
  // Otherwise, see (http://drupal.org/node/178506#comment-726479)
  // above about using user_load() to create nodes as any user, even 
  // when not logged in
  global $user;
  
  $values['values']['name'] = $user->name;
  $values['values']['uid'] = $user->uid;
  
  $values['values']['field_nlemail'][0]['email'] = $form_state['values']['email'];
  $values['values']['field_nlname'][0]['value'] = $form_state['values']['email'];
  $values['values']['field_nlbdate'][0]['value'] = $form_state['values']['birthdate'];
  
  $errs = drupal_execute('signup_node_form', $values, (object) $nodeTmp);
  
  // if there were any validation errors, drupal_execute will return them,
  // then you have a chance to do something
  if (count($errs)) {
    print_r($errs); //todo
    // optional code here
  }
}

function signup_form($form_state) {
    $form['email'] = array(
    '#type' => 'textfield',
    '#title' => 'Email Address ',
    '#required' => true,
    //'#size' => 25, /* this is overridden in css */
    '#prefix' => '', 
    '#suffix' => '',
  );
  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => 'Name (optional)',
    '#size' => 10,
    '#prefix' => '', 
    '#suffix' => '',
  );
  $form['birthdate'] = array(
    '#type' => 'date_popup',
    '#title' => 'Birthday',
    //'#description' => 'please use format mm/dd/yy',
    //'#required' => true,
    '#date_type' => DATE_DATETIME,
 	  '#date_timezone' => date_default_timezone_name(),
 	  '#date_format' => 'm/d/Y',
 	  '#date_increment' => 1,
	  '#date_year_range' => '-60:-14',
    '#prefix' => '', 
    '#suffix' => '',
  );
  $form['submit'] = array(
    '#type' => 'image_button',
    '#title' => t('Sign Up'),
    '#src' => 'files/images/btn_signup.png',
    '#prefix' => '', 
    '#suffix' => '',
  );
  
  return $form;
}

// this is in template.php

function mytheme_theme() {
  return array(
    'signup_node_form' => array(
      'arguments' => array('form' => NULL)),
    'signup_form' => array( 
      'arguments' => array('form' => NULL)),
  );
}

// theme a built in content type form
function mytheme_signup_node_form($form) {
  $output =drupal_render($form);
  return $output;
}

// theme my forms api form
function mytheme_signup_form($form) {
  $output =drupal_render($form);
  return $output;
}


Post new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
7 + 13 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.

wordpress blog stats