我想在Drupal 7中的一个自定义页面中放置2个表单

Ree*_*ees 3 drupal drupal-views drupal-7 drupal-theming drupal-modules

我是Drupal的新手,我试图创建一个包含2个表单(来自自定义模块)的单个页面.我还希望这个页面成为我主题目录中的专用文件.

在名为"vocabt"的自定义模块中,我有vocabt.module,其中包含3个所需的函数:

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;
}
Run Code Online (Sandbox Code Playgroud)

我的问题是我想点击"/ scores/admin"之类的URL,它不仅包含"vocabt_admin_login_form()"函数中的表单,而且还包含上述函数"vocabt_student_login_form()"中的表单....我怎样才能做到这一点?

我还希望两个表单都出现在使用专用PHP文件的页面中(我已经可以通过在CMS中创建新页面使用专用PHP文件创建自定义页面),但我不知道如何将此专用PHP文件与以上2种形式.请告诉我!谢谢

Aye*_*h K 8

目前尚不清楚"专用PHP文件"的含义.在Drupal中,您使用定义菜单路由器项目hook_menu(),然后在特定页面回调函数中返回它们的数据.此函数可以在任何.module文件或普通PHP范围内的任何包含文件中.

在hook_menu()实现中添加一个新项目,如下所示:

$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,
) 
Run Code Online (Sandbox Code Playgroud)

现在,让我们创建页面回调函数vocabt_scores_page来返回一个渲染数组,这样Drupal将在这个页面中构建两个表单 - 而不仅仅是指定的表单.

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;
}
Run Code Online (Sandbox Code Playgroud)

在测试代​​码之前,请确保清除站点的缓存.保存后,转到scores/check页面,您将在一个页面中看到两个表单!

您可以将页面回调函数移动到文件中的其他.module文件.为此,请添加'files' => 'vocabt.pages.inc'到菜单项并将函数移动到名为的新文件vocabt.pages.inc.Drupal将假定该文件与文件位于同一文件夹中.module.此文件名可以是任何东西,但我们大多使用module.pages.inc,module.admin.inc同样.