3
votes

I am completely new to Drupal am trying to create a single page that contains 2 forms (that come from a custom module). I also want this page to be a dedicated file in my theme directory.

In the custom module called "vocabt" I have vocabt.module that contains 3 needed functions:

function vocabt_menu() {

    $items['scores/admin'] = array(
    'title' => 'Check Your Student\'s Scores',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('vocabt_admin_login_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
);

    $items['scores/student'] = array(
    'title' => 'Check My Scores',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('vocabt_student_login_form'),
    'access arguments' => array('access content'),
    'type' => MENU_NORMAL_ITEM,
);

}



function vocabt_admin_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
    $values = $form_state['values'];
} else {
    $values = array();
}

$schools = vocabt_get_school_options();
$form['school'] = array(
    '#type' => 'select',
    '#title' => 'Select Your School',
    '#required' => TRUE,
    '#options' => $schools,
    '#default_value' => !empty($values['school']) ? $values['school'] : '',
    '#empty_option' => 'Select',
);
$form['username'] = array(
    '#type' => 'textfield',
    '#title' => 'User Name',
    '#required' => TRUE,
);
$form['password'] = array(
    '#type' => 'password',
    '#title' => 'Password',
    '#required' => TRUE,
);

$form['actions'] = array();
$form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Log In',
    '#attributes' => array('class' => array('button blue')),
);
return $form;
}




 function vocabt_student_login_form($form, &$form_state) {
if(!empty($form_state['values'])) {
    $values = $form_state['values'];
} elseif(isset($_GET['id']) && $info = vocabt_decode_id($_GET['id'])) {
    $values = $info;
} else {
    $values = array();
}

$form['left'] = array(
    '#prefix' => '<div class="lookup">',
    '#suffix' => '</div>',
);

$form['right'] = array(
    '#prefix' => '<div class="results">',
    '#suffix' => '</div>',
);

$schools = vocabt_get_school_options();
$form['left']['school'] = array(
    '#type' => 'select',
    '#title' => 'Select Your School',
    '#required' => TRUE,
    '#options' => $schools,
    '#default_value' => !empty($values['school']) ? $values['school'] : '',
    '#empty_value' => 'Select',
);
$form['left']['id'] = array(
    '#type' => 'textfield',
    '#title' => 'Enter your student ID',
    '#required' => TRUE,
    '#default_value' => !empty($values['id']) ? $values['id'] : '',
);
$form['left']['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Check',
    '#attributes' => array('class' => array('button blue')),
);

if(!empty($form_state['storage']['error'])) {
    $results = '<div class="blackbox">';
    $results .= '<div style="padding: 20px 0px; text-align: center;">'.$form_state['storage']['error'].'</div>';
    $results .= '</div>';
} elseif(empty($values['id'])) {
    $results = '<div class="blackbox">';
    $results .= '<div class="instructions">Your score will appear here once you’ve entered your information on the left.</div>';
    $results .= '</div>';
} else {
    $results = vocabt_get_student_results($values['school'], $values['id']);
}

$form['right']['results'] = array('#markup' => $results);

return $form;
}

My question is I want to hit a URL such as "/scores/admin" that NOT ONLY contains the form within the function of "vocabt_admin_login_form()" but ALSO contains the form within the above function "vocabt_student_login_form()" ....how can I do this?

I also want both forms to appear in page that uses a dedicated PHP file (I can already create custom pages using dedicated PHP files via creating a new page in the CMS), but I do not know how to tie this dedicated PHP file with the 2 forms above. Please let me know! Thank you

2
Although it doesn't directly answer your question, Drupal also has a module called webform (drupal.org/project/webform). You can add all kinds of contents to the page where you create the form using the "markup field.Meetai.com

2 Answers

8
votes

It's not clear what you mean by "Dedicated PHP files". In Drupal, you define menu router items using hook_menu(), and then return their data in the specific page callback function. This function can be in any .module file or any included file in normal PHP scope.

Add a new item in your hook_menu() implementation like this:

$items['scores/check'] = array(
  'title' => 'Check Scores',
  'page callback' => 'vocabt_scores_page', // Note this is the PAGE CALLBACK!
  'access arguments' => array('access content'),
  'type' => MENU_NORMAL_ITEM,
) 

Now, lets create the page callback function, vocabt_scores_page, to return a render array so Drupal will build both forms in this page - not just the specified one.

function vocabt_scores_page() {
  $output = array();
  $output['admin'] = drupal_get_form('vocabt_admin_login_form');
  $output['student'] = drupal_get_form('vocabt_student_login_form');
  return $output;
}

Make sure you clear your site's caches before testing the code. Once saved, go to scores/check page and you will see both forms in one page!

You can move your page callback functions to a different file from the .module file. To do so, add 'files' => 'vocabt.pages.inc' to the menu item and move the functions to a new file named vocabt.pages.inc. Drupal will assume the file is in same folder as the .modulefile. This file name can be anything but we mostly use module.pages.inc, module.admin.inc, likewise.

2
votes

You could call a template page using the hook_theme function. You could place your html contents in this page.And just call the theme('page_name', $link);.Place your newly created theme file in your corresponding module.

function modulename_theme() {
    return array(
        'page_name' => array(
          'template' => 'page_name',
          'arguments' => array('link' => NULL),
        ),
      );
    }

You could find more information here

http://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_theme/7

Hope this helps you... :)