使用 Drupal Form Hooks 进行表单自定义修改

Drupal使用或者开发过程中最常用到的Hooks(钩子)莫过于hook_form_alter,你所常见的Drupal网站中的内容创建,联系表单,Menu菜单,用户注册等等都会用到表单的钩子。

Drupal Form Hooks
hook_form_alter 中的hook直接替换为你的模块名称。

 代码如下 复制代码

/**
* Implements hook_form_alter().
*/
function custom_form_alter(&$form, &$form_state, $form_id) {
switch($form_id) {
case ‘user_profile_form’:
//doing something
break;
}
}

hook_form_FORM_ID_alter 是 hook_form_alter的一个变种,直接对某一个具体的表单进行修改

 代码如下 复制代码

/**
* Implements hook_form_FORM_ID_alter().
*/
function custom_form_user_profile_form_alter(&$form, &$form_state) {
if ($form['#user_category'] == ‘settings’) {
if (variable_get(‘configurable_timezones’, 1)) {
system_user_timezone($form, $form_state);
}
return $form;
}
}

通过以上2个Hooks就可以轻松给Drupal 添加自定义的表单元素。

每一个form都可以自定义theme前段元素,render的elements 都会通过variables传递给主题。

 代码如下 复制代码

/**
* Implements hook_theme().
*/
function custom_theme() {
return array(
‘user_profile_form’ => array(
‘render element’ => ‘form’,
),
);
}

自定义form的element样式。

 代码如下 复制代码

function theme_user_profile_form($variables) {
$form = $variables['form'];

$output = drupal_render($form['info']);

$header = array(t(‘Factor’), t(‘Weight’));
foreach (element_children($form['factors']) as $key) {
$row = array();
$row[] = $form['factors'][$key]['#title'];
$form['factors'][$key]['#title_display'] = ‘invisible’;
$row[] = drupal_render($form['factors'][$key]);
$rows[] = $row;
}
$output .= theme(‘table’, array(‘header’ => $header, ‘rows’ => $rows));

$output .= drupal_render_children($form);
return $output;
}

通过 hook_preprocess_FORM_ID 在theme form element之前修改$variables

 代码如下 复制代码

function custom_preprocess_user_profile_form(&$variables) {
if ($variables['form’][‘actions]) {
//change the button name
}
// add new variable to theme form
}

自定义form的html元素,可以将form的theme定义一个template,注意这样会降低drupal的性能,但是换来的好处是可以自定义html。

 代码如下 复制代码

/**
* Implements hook_theme().
*/
function lixiphp_theme($existing, $type, $theme, $path){
return array(
‘user_profile_form’ => array(
‘render element’=>’form’,
‘template’ =>’templates/form/user-profile’,
),
);
}

创建user-profile.tpl.php文件在templates/form目录下。

 代码如下 复制代码

<?php
print drupal_render($form['form_id']);
print drupal_render($form['form_build_id']);
print drupal_render($form['form_token']);
?>
<li class=“rows”>
<?php print drupal_render($form['actions']); ?>
</li>

本文讲究的form自定义方法实用于Drupal6,Drupal7和Drupal8。

时间: 2024-08-31 20:16:14

使用 Drupal Form Hooks 进行表单自定义修改的相关文章

javascript获取form里的表单元素的示例代码

 本篇文章主要是对javascript获取form里的表单元素的示例代码进行了介绍,需要的朋友可以过来参考下,希望对大家有所帮助 //获取form对象   var form=document.getElementById('my_form');   //用户名input对象 user_name是对象的name属性   var userName=form.user_name;   //用户名清空   userName.value='';   //用户密码input对象 password是对象的na

Jquery.Form 异步提交表单的简单实例

 这篇文章主要介绍了Jquery.Form 异步提交表单的简单实例.需要的朋友可以过来参考下,希望对大家有所帮助 http://www.vaikan.com/docs/jquery.form.plugin/jquery.form.plugin.html#   1. 在你的页面里写一个表单.一个普通的表单,不需要任何特殊的标记:    代码如下: <form id="myForm" method="post" action="/Home/AjaxFor

form action的表单提交问题

问题描述 form action的表单提交问题 var posthref=......./a.jspa $("#addForm").attr("action",posthref).submit(); `` 这段程序是将表单addform提交到posthref所指的路径的意思吗? 为什么会提交到一个jspa的里面?jspa是做什么用的?我再工程中也没有找到a.jspa...后面是如何将表单数据再传到数据库的呢? 解决方案 就是提交到后台的路径啊,你找到后台文件看一下

工作流引擎与表单自定义

问题描述 工作流引擎与表单自定义 客户有这样一个需求:可以自己定义一个表单的样式,包括布局.字段等..另外表单生成后,提交的流程也可以自定义,并且是图形化拖拉拽的方式..在JAVA领域有这方面的开源组件可以借鉴吗?商业也可以.求推荐.谢谢! 解决方案 http://blog.csdn.net/mosquitofree/article/details/6563452 解决方案二: 可以使用ccflow试试 解决方案三: 拥有可视化的流程设计器,可以实现公文表单.自定义表单.自由表单等多种表单,具有

Angular2表单自定义验证器的实现_AngularJS

本文主要给大家介绍如何判断验证器的结果.在这里,我们就来看看怎样实现一个自定义的验证器. 目标 我们要实现一个验证手机号的验证器,使用的实例还是基于之前的文章里面的实例,也就是用户信息输入的表单页面.我们在手机号的元素上添加一个验证手机号的验证器.然后,如果手机号验证失败,就显示一个错误,页面如下: 这部分教程的代码可以从github获取: git clone https://github.com/Mavlarn/angular2-forms-tutorial 如果要运行,进入项目目录,运行下面

JS判断form内所有表单是否为空的简单实例_javascript技巧

如下所示: function checkForm(){ var input_cart=document.getElementsByTagName_r("INPUT"); for(var i=0; i<input_cart.length; i++) { if(input_cart[i].value==""||input_cart[i].value==null) { alert("信息不能为空!"); input_cart[i].focus()

jqm列表页传参到表单页修改后再传回来求一个大概的代码流程

问题描述 jqm列表页传参到表单页修改后再传回来求一个大概的代码流程 求一个大概的代码流程,用什么语言写逻辑比较清晰,用的是List集合来模拟数据库 列表页用的是Post传的数据,后台是WebMethod

基于Form Effect 的表单美化插件介绍

Form Effect - 表单美化插件          1)Formly这个基于jQuery的表单美化插件,并带有表单校验功能. http://thrivingkings.com/formly/                 2)jQuery Tags Input这个jQuery插件能够将一个简单的文本输入转换成一个漂亮的Tag列表. http://xoxco.com/clickable/jquery-tags-input 演示地址: http://xoxco.com/clickable/

asp.net 将&amp;amp;lt;form&amp;amp;gt;表单信息传到数据库

问题描述 .aspx页面中的<form>表单信息如下:<formid="form1"name="form1"method="post"runat="server"><tablestyle="border-spacing:8px;"><captionstyle="font-size:30px;font-family:KaiTi">案件详情记录