首页 > 解决方案 > Drupal 7:自定义模块模板中的自定义表单

问题描述

我正在为 Drupal 7 站点创建一个自定义模块,我需要使用表单挂钩创建一个表单,然后将其包含在自定义模块的模板中。

我在 .module 文件中创建了一个简单的表单:

function my_login_form($form, &$form_state) {
  $form['email_address'] = array(
    '#type' => 'textfield',
    '#attributes' => array('placeholder' => t('Email Address')),
  );
  $form['phone'] = array(
    '#type' => 'password',
    '#attributes' => array('placeholder' => t('Password')),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Login',
  );

  return $form;
}

在我的 hook_menu() 中,我创建了一条路线,并且“页面回调”指向了该函数:

function my_login(){

  return theme('my_login_template');

}

在我的 hook_theme() 函数中,我将它指向 .tpl:

function my_theme(){
  return array(

    'my_login_template' => array(
      'template' => 'login',
    ),
  );
}

如何将 my_login_form 放入 login.tpl.php 文件?

感谢您提供的任何帮助或您可以指出的任何地方。

标签: drupal-7drupal-modules

解决方案


您需要将其传递给主题:

function my_login(){
  $form = drupal_get_form('my_login_form');
  return theme('my_login_template',  array('form' => render($form)));

}

进入你的 tpl :

<?php print $form; ?>

清除缓存主题


推荐阅读