Search code examples
phpdrupaldrupal-7drupal-modulesdrupal-forms

Create custom form display it in colorbox in each page in drupal 7?


I want to display custom form (which is custom module) in colorbox in each page.

I have created form in drupal 7 which is working fine if i run it by calling it in url but i need to call this module in colorbox.

my custom module(regform) code is

  function regform_init() {
    drupal_set_message('Our module is alive!');
  }
  function regform_menu() {
    $items = array();
    $items['regform'] = array(
      'title' => 'Custom page',
      'page callback' => 'drupal_get_form',
      'page arguments' => array('regform_form'), 
      'access arguments' => array('access content'),
      'access callback' => TRUE
    );
    return $items;
  }
  function regform_form($form, &$form_state) {
    $form['#suffix'] = '<div id="test-ajax"></div>';
     $form['email'] = array(
      '#type' => 'textfield', //you can find a list of available types in the form api
      '#title' => 'Enter Email',
      '#size' => 50,
      '#maxlength' => 50,
      '#required' => TRUE, //make this field required
    );
    $form['submit_button'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
      '#validate' => 'regform_form_validate',
      '#ajax' => array(
        'callback' => 'regform_form_ajax_callback',
        'wrapper' => 'test-ajax'
      ),
      '#submit' => array('regform_form_ajax_callback'),
    );
    return $form;
  }
  function regform_form_ajax_callback($form, &$form_state) { 
    /*Fire database query*/
    /*Validation msg div block call here*/
    return "<div id='test-ajax'></div>";
  }
  function regform_form_validate($form, &$form_state) {
    if (trim($form_state['values']['email']) == ''){
      form_set_error('email', t('Please Enter Email'));
    }
    if(!valid_email_address($form_state['values']['email'])){
      form_set_error('email', t('Enter Valid Email'));
    }
  }
  function regform_form_submit($form, &$form_state) {
      $form_state['rebuild'] = TRUE;
      $form_state['input'] = array();
  }

This form is working fine.

Also I dont have any idea about how to use colorbox in drupal 7.

Any help will be appreciated.

Thanks in Advance.


Solution

  • I suggest to use chaos tools for modal popup form. Its easy to implement and integrate into Drupal 7.

    A great example of Ctools modal form is given at here