【问题标题】:Drupal 7 custom form moduleDrupal 7 自定义表单模块
【发布时间】:2015-03-24 07:42:08
【问题描述】:

我使用自定义模块的表单没有出现在我的联系我们页面上。我创建了我的.module 文件和.info 文件,然后也激活了该模块。以下是我在.module 文件中包含的代码

function custom_form_module_form($form,&$form_state) {
$form['name'] = array(
'#type' => 'textfield',
);
$form['company'] = array(
'#type' => 'textfield',
);
$form['phone'] = array(
'#type' => 'textfield',
);
$form['email'] = array(
'#type' => 'textfield',
);
$form['message'] = array(
'#type' => 'textfield',
);
return $form; 
}

在我的 template.php 文件中

function custom_form_module_form_theme($existing, $type, $theme, $path) {
$items['custom_form_module_form'] = array(
'render element' => 'form',
'template' => 'page--contact-us',
'path' => drupal_get_path('theme', 'escorts').'/templates');
return $items;
}

在我的 page--contact-us.tpl.php 中,我使用以下行来调用表单的单独字段,但它不起作用。

<?php echo drupal_render($form['name']); ?>

“这里custom_form_module=my module namepage--contact-us.tpl.php=my template fileescorts=my theme name

【问题讨论】:

  • 问题是……!
  • 表单没有出现在我的联系我们页面@muhammad,我试图在这里获取“名称”字段

标签: drupal-7 form-api


【解决方案1】:

要打印联系页面中的表格,您不能使用template.php

简单地说:

$my_form = drupal_get_form('custom_form_module_form');
print drupal_render($my_form);

更新

template.php 更改表单标记

function theme_my_form_1($form) {
    $vars['element'] = $form;

    $markup = '';
    $markup .= '<div id="form-name-wrapper">'
        . drupal_render($form['name'])
        . '</div>';
    // continue altering the markup

    $vars['element']['#children'] = $output;
    return theme_form($vars);
}

然后,在您的.tpl.php 文件中:

$my_form = drupal_get_form('custom_form_module_form');
print theme_my_form_1($my_form);

【讨论】:

  • Thanku @Muhammad,但我想要单独的字段以便分别对它们进行主题化。我的代码正确吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
相关资源
最近更新 更多