欢迎各位兄弟 发布技术文章
这里的技术是共享的
function hook_form_alter(&&form, &$form_state, $form_id)
{ //这里hook 是模块名
}
//这个是向blog节点表单增加一个subtitler的字段
//这个方法是在模块的module 文件里
function hello_drupal_form_alter(&&form, &$form_state, $form_id)
{
//var_dump($form_id);在此处使用 var_dump 可查看当前页面调用的表单ID
switch($form_id)
{
case 'blog_node_form':
$form['subtitle'] = array(
'#type'=>'textfield',
'#title'=>'Sub Title'
);
break;
}
}
//hellodrupal 这是模块名 覆写菜单
function hellodrupal_form_alter(&$form, &$form_state, $form_id) {
// 先判断form id,只修改某form
if ($form_id == 'event_wzdl_node_form') {
//event_wzdl_node是节点类型名
// 调试时候,打印$form出来看下
//print '<pre>'. check_plain(print_r($form, 1)) .'</pre>';
// 修改form跳转路径
$form['#redirect'] = url('xxoo');
}
}
1)创建一个字段field_zhuanjian_chuli 使它为 text 文本字段
设置隐藏字段的方法
2)在模块中使用
/**
* Implementation of hook_form_alter().
*/
function execself_form_alter(&$form, &$form_state, $form_id) {
if (isset($form['type']) && isset($form['#node'])) {
### Make a CCK field becoming a hidden type field.
// ### Use this check to match node edit form for a particular content type.
if ($form_id === 'event_wzdl_node_form') {
$form['field_zhuanjian_chuli'][0]['#default_value']['value'] = '0';
$form['#after_build'] = array('_test_set_cck_field_to_hidden');
}
}
}
/**
*
* @param
* @return
*/
function _test_set_cck_field_to_hidden($form, &$form_state) {
$form['field_zhuanjian_chuli'][0]['value']['#type'] = 'hidden';
$form['field_zhuanjian_chuli'][0]['#value']['value'] = '0';
return $form;
}
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'my_cck_type_node_form') {
// this works
$form['title']['#disabled'] = true;
$form['title']['#attributes']['class'] = 'my_class';
// this doesn't
$form['my_cck_field']['#disabled'] = true;
$form['my_cck_field']['#attributes']['class'] = 'my_class';
}
}
//hellodrupal 这是模块名
function hellodrupal_form_alter(&$form, &$form_state, $form_id) {
// 先判断form id,只修改某form
if ($form_id == 'event_wzdl_node_form') {
//event_wzdl_node是节点类型名
// 调试时候,打印$form出来看下
//print '<pre>'. check_plain(print_r($form, 1)) .'</pre>';
// 修改form跳转路径
$form['#redirect'] = url('xxoo');
}
}
重写 node 方法
<?php
function nodeself_form_submit($form, &$form_state) {
global $user;
$node = node_form_submit_build_node($form, $form_state);
$insert = empty($node->nid);
node_save($node);
$node_link = l(t('view'), 'node/'. $node->nid);
$watchdog_args = array('@type' => $node->type, '%title' => $node->title);
$t_args = array('@type' => node_get_types('name', $node), '%title' => $node->title);
if ($insert) {
watchdog('content', '@type: added %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
drupal_set_message(t('@type %title has been created.', $t_args));
}
else {
watchdog('content', '@type: updated %title.', $watchdog_args, WATCHDOG_NOTICE, $node_link);
drupal_set_message(t('@type %title has been updated.', $t_args));
}
if ($node->nid) {
unset($form_state['rebuild']);
$form_state['nid'] = $node->nid;
$form_state['redirect'] = 'node/'. $node->nid;
}
else {
// In the unlikely case something went wrong on save, the node will be
// rebuilt and node form redisplayed the same way as in preview.
drupal_set_message(t('The post could not be saved.'), 'error');
}
}
/**
* Build a node by processing submitted form values and prepare for a form rebuild.
*/
function nodeself_form_submit_build_node($form, &$form_state) {
// Unset any button-level handlers, execute all the form-level submit
// functions to process the form values into an updated node.
unset($form_state['submit_handlers']);
form_execute_handlers('submit', $form, $form_state);
$node = node_submit($form_state['values']);
$form_state['node'] = (array)$node;
$form_state['rebuild'] = TRUE;
return $node;
}
/**
* Generate the node add/edit form array.
*/
function nodeself_form(&$form_state, $node) {
global $user;
if (isset($form_state['node'])) {
$node = $form_state['node'] + (array)$node;
}
if (isset($form_state['node_preview'])) {
$form['#prefix'] = $form_state['node_preview'];
}
$node = (object)$node;
foreach (array('body', 'title', 'format') as $key) {
if (!isset($node->$key)) {
$node->$key = NULL;
}
}
if (!isset($form_state['node_preview'])) {
node_object_prepare($node);
}
else {
$node->build_mode = NODE_BUILD_PREVIEW;
}
// Set the id of the top-level form tag
$form['#id'] = 'node-form';
// Basic node information.
// These elements are just values so they are not even sent to the client.
foreach (array('nid', 'vid', 'uid', 'created', 'type', 'language') as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => isset($node->$key) ? $node->$key : NULL,
);
}
// Changed must be sent to the client, for later overwrite error checking.
$form['changed'] = array(
'#type' => 'hidden',
'#default_value' => isset($node->changed) ? $node->changed : NULL,
);
// Get the node-specific bits.
if ($extra = node_invoke($node, 'form', $form_state)) {
$form = array_merge_recursive($form, $extra);
}
if (!isset($form['title']['#weight'])) {
$form['title']['#weight'] = -5;
}
$form['#node'] = $node;
// Add a log field if the "Create new revision" option is checked, or if the
// current user has the ability to check that option.
if (!empty($node->revision) || user_access('administer nodes')) {
$form['revision_information'] = array(
'#type' => 'fieldset',
'#title' => t('Revision information'),
'#collapsible' => TRUE,
// Collapsed by default when "Create new revision" is unchecked
'#collapsed' => !$node->revision,
'#weight' => 20,
);
$form['revision_information']['revision'] = array(
'#access' => user_access('administer nodes'),
'#type' => 'checkbox',
'#title' => t('Create new revision'),
'#default_value' => $node->revision,
);
$form['revision_information']['log'] = array(
'#type' => 'textarea',
'#title' => t('Log message'),
'#default_value' => (isset($node->log) ? $node->log : ''),
'#rows' => 2,
'#description' => t('An explanation of the additions or updates being made to help other authors understand your motivations.'),
);
}
// Node author information for administrators
$form['author'] = array(
'#type' => 'fieldset',
'#access' => user_access('administer nodes'),
'#title' => t('Authoring information'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 20,
);
$form['author']['name'] = array(
'#type' => 'textfield',
'#title' => t('Authored by'),
'#maxlength' => 60,
'#autocomplete_path' => 'user/autocomplete',
'#default_value' => $node->name ? $node->name : '',
'#weight' => -1,
'#description' => t('Leave blank for %anonymous.', array('%anonymous' => variable_get('anonymous', t('Anonymous')))),
);
$form['author']['date'] = array(
'#type' => 'textfield',
'#title' => t('Authored on'),
'#maxlength' => 25,
'#description' => t('Format: %time. Leave blank to use the time of form submission.', array('%time' => !empty($node->date) ? $node->date : format_date($node->created, 'custom', 'Y-m-d H:i:s O'))),
);
if (isset($node->date)) {
$form['author']['date']['#default_value'] = $node->date;
}
// Node options for administrators
$form['options'] = array(
'#type' => 'fieldset',
'#access' => user_access('administer nodes'),
'#title' => t('Publishing options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 25,
);
$form['options']['status'] = array(
'#type' => 'checkbox',
'#title' => t('Published'),
'#default_value' => $node->status,
);
$form['options']['promote'] = array(
'#type' => 'checkbox',
'#title' => t('Promoted to front page'),
'#default_value' => $node->promote,
);
$form['options']['sticky'] = array(
'#type' => 'checkbox',
'#title' => t('Sticky at top of lists'),
'#default_value' => $node->sticky,
);
// These values are used when the user has no administrator access.
foreach (array('uid', 'created') as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => $node->$key,
);
}
// Add the buttons.
$form['buttons'] = array();
$form['buttons']['submit'] = array(
'#type' => 'submit',
'#access' => !variable_get('node_preview', 0) || (!form_get_errors() && isset($form_state['node_preview'])),
'#value' => t('Save'),
'#weight' => 5,
'#submit' => array('node_form_submit'),
);
$form['buttons']['preview'] = array(
'#type' => 'submit',
'#value' => t('Preview'),
'#weight' => 10,
'#submit' => array('node_form_build_preview'),
);
if (!empty($node->nid) && node_access('delete', $node)) {
$form['buttons']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 15,
'#submit' => array('node_form_delete_submit'),
);
}
$form['#validate'][] = 'node_form_validate';
$form['#theme'] = array($node->type .'_node_form', 'node_form');
return $form;
}
/**
* Present a node submission form.
*
* @ingroup themeable
*/
/*function theme_node_form($form) {
$output = "\n<div class=\"node-form\">\n";
// Admin form fields and submit buttons must be rendered first, because
// they need to go to the bottom of the form, and so should not be part of
// the catch-all call to drupal_render().
$admin = '';
if (isset($form['author'])) {
$admin .= " <div class=\"authored\">\n";
$admin .= drupal_render($form['author']);
$admin .= " </div>\n";
}
if (isset($form['options'])) {
$admin .= " <div class=\"options\">\n";
$admin .= drupal_render($form['options']);
$admin .= " </div>\n";
}
$buttons = drupal_render($form['buttons']);
// Everything else gets rendered here, and is displayed before the admin form
// field and the submit buttons.
$output .= " <div class=\"standard\">\n";
$output .= drupal_render($form);
$output .= " </div>\n";
if (!empty($admin)) {
$output .= " <div class=\"admin\">\n";
$output .= $admin;
$output .= " </div>\n";
}
$output .= $buttons;
$output .= "</div>\n";
return $output;
}
*/
/**
* Button sumit function: handle the 'Delete' button on the node form.
*/
function nodeself_form_delete_submit($form, &$form_state) {
$destination = '';
if (isset($_REQUEST['destination'])) {
$destination = drupal_get_destination();
unset($_REQUEST['destination']);
}
$node = $form['#node'];
$form_state['redirect'] = array('node/'. $node->nid .'/delete', $destination);
}
function nodeself_form_build_preview($form, &$form_state) {
$node = node_form_submit_build_node($form, $form_state);
$form_state['node_preview'] = node_preview($node);
}
function nodeself_form_validate($form, &$form_state) {
node_validate($form_state['values'], $form);
}
function nodeself_forms() {
$forms = array();
if ($types = node_get_types()) {
foreach (array_keys($types) as $type) {
$forms[$type .'_node_form']['callback'] = 'node_form';
}
}
return $forms;
}
/**
* Implementation of hook_form_alter().
*/
function nodeself_form_alter(&$form, $form_state, $form_id) {
// Advanced node search form
if ($form_id == 'search_form' && $form['module']['#value'] == 'node' && user_access('use advanced search')) {
// Keyword boxes:
$form['advanced'] = array(
'#type' => 'fieldset',
'#title' => t('Advanced search'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#attributes' => array('class' => 'search-advanced'),
);
$form['advanced']['keywords'] = array(
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
);
$form['advanced']['keywords']['or'] = array(
'#type' => 'textfield',
'#title' => t('Containing any of the words'),
'#size' => 30,
'#maxlength' => 255,
);
$form['advanced']['keywords']['phrase'] = array(
'#type' => 'textfield',
'#title' => t('Containing the phrase'),
'#size' => 30,
'#maxlength' => 255,
);
$form['advanced']['keywords']['negative'] = array(
'#type' => 'textfield',
'#title' => t('Containing none of the words'),
'#size' => 30,
'#maxlength' => 255,
);
// Taxonomy box:
if ($taxonomy = module_invoke('taxonomy', 'form_all', 1)) {
$form['advanced']['category'] = array(
'#type' => 'select',
'#title' => t('Only in the category(s)'),
'#prefix' => '<div class="criterion">',
'#size' => 10,
'#suffix' => '</div>',
'#options' => $taxonomy,
'#multiple' => TRUE,
);
}
// Node types:
$types = array_map('check_plain', node_get_types('names'));
$form['advanced']['type'] = array(
'#type' => 'checkboxes',
'#title' => t('Only of the type(s)'),
'#prefix' => '<div class="criterion">',
'#suffix' => '</div>',
'#options' => $types,
);
$form['advanced']['submit'] = array(
'#type' => 'submit',
'#value' => t('Advanced search'),
'#prefix' => '<div class="action">',
'#suffix' => '</div>',
);
$form['#validate'][] = 'node_search_validate';
}
}
//还有一种方法
hook_form_FROM_ID_alter(&$form, &$form_state)
把 FROM_ID 换成 要修改的 表单的ID
function hello_drupal_form_blog_node_form_alter(&$form, &$form_state)
{
$form['subtitle'] = array(
'#type'=>'textfield',
'#title'=>'Sub Title'
);
}
隐藏字段的方法
function mymodule_form_alter(&$form, $form_state, $form_id) {
if ($form_id == 'my_cck_type_node_form') {
// this works
$form['title']['#disabled'] = true;
$form['title']['#attributes']['class'] = 'my_class';
// this doesn't
$form['my_cck_field']['#disabled'] = true;
$form['my_cck_field']['#attributes']['class'] = 'my_class';
}
}
//通过jquery 方法 让日期 去掉 autocomplete
function execself_form_alter(&$form, &$form_state, $form_id) {
drupal_add_js('$("#edit-field-possible-dates-value-value-datepicker-popup-0").attr("autocomplete","off");', 'inline', 'footer');
drupal_add_js('$("#edit-field-possible-dates-value2-value-datepicker-popup-0").attr("autocomplete","off");', 'inline', 'footer');
}
<?php
//修改用户登录模块
//修改表单 应该是没有 return $form
function shipingzhongcustom_form_alter(&$form, &$form_state, $form_id) {
if ('user_login_block' == $form_id) {
//修改表单 下面这个action 也应该没有吧
// $form = array(
//'#action' => url($_GET['q'], array('query' => drupal_get_destination())),
// '#id' => 'user-login-form',
// '#validate' => user_login_default_validators(),
// '#submit' => array('user_login_submit'),
//);
$form['name'] = array(
'#type' => 'textfield',
'#title' => t('会员登录'),
'#maxlength' => USERNAME_MAX_LENGTH,
'#size' => 15,
'#attributes'=>array('class'=>'text1 l')
);
$form['pass'] = array(
'#type' => 'password',
'#title' => '<img alt="" src="/sites/all/themes/shipingzhong/images/i2_03.jpg">',
'#maxlength' => 60,
'#size' => 15,
'#attributes'=>array('class'=>'text1 l')
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Log in'),
'#attributes'=>array('class'=>'btn ht l')
);
$items = array();
$form['links'] = array('#value' => theme('item_list', $items));
}
if('user_login' == $form_id) {
$form['name']['#size'] = 40;
$form['pass']['#size'] = 40;
}
}
function shipingzhongcustom_perm() {
//return array('access test001 content');
return array('send lianxiwomen','edit own info');
}
//修改 menu hook 给用户编辑自己的信息 的权限
function shipingzhongcustom_menu_alter(&$items) {
// Example - disable the page at node/add
//var_dump($items);var_dump("DDDDDDDd");
$items['user/%user_category/edit']['access callback']='user_access';
$items['user/%user_category/edit']['access arguments']=array('edit own info');
//$items['user/%user/edit']['access callback'] = 'user_access';
// $items['user/%user/edit']['access arguments'] = array('edit own info');
}
//function test_is_admin()
//{
// global $user;
// //return false;
// return user_access('edit own info') && user_access('访问管理页面');
//
//
//}
//联系我们表单
function shipingzhongcustom_menu() {
$items = array();
$items['lianxi'] = array(
'title'=>t('联系我们'),
'page callback'=>'exec_lianxi_form',
'page arguments' => array('page callback arguments'),
'access arguments'=>array('send lianxiwomen')
);
return $items;
}
//执行联系我们表单
function exec_lianxi_form(){
return drupal_get_form('lianxi_form');
}
function lianxi_form()//lianxi_form 就是 $form_id
{
$form['#attributes'] = array('class' => 'cont');
//markup就是普通的文本
$form['lianxiwomen']['formdesc'] = array(
'#type'=>'markup',
'#value'=>t('<p class="zi2"><a class="zi3">如果您有任何建议或需要与我们合作请您填写完以下的表单以方便我们与您取得联系。</a></p>'),
);
// 文本框 name是文本框的名称
$form['lianxiwomen']['xinming'] = array(
'#type' =>'textfield',//drupal 规定文本框用textfield来表示
'#title'=>t('<h3>姓名:</h3>'),//标题 显示在文本框前面
'#attributes'=>array('class' => 'name'),
// '#default_value'=>'hello world',
// '#description'=>t('Please input your name')//文本框下面的描述
'#required' => TRUE
);
$form['lianxiwomen']['sex'] = array(
'#type' => 'radios',
'#title' =>t('<h3>性别:</h3>'),
'#default_value' => 1,
'#options' => array(1 => '男',2 => '女'),
);
$form['lianxiwomen']['tel'] = array(
'#type'=>'textfield',
'#title'=>t('<h3>手机号码:</h3>'),
'#attributes'=>array('class' => 'name'),
'#required' => TRUE
);
$form['lianxiwomen']['address'] = array(
'#type'=>'textfield',
'#title'=>t('<h3>家庭地址:</h3>'),
'#attributes'=>array('class' => 'name'),
'#required' => TRUE
);
$form['lianxiwomen']['leibei'] = array(
'#type'=>'select',
'#title'=>t('<h3>类别</h3>'),
'#attributes'=>array('class' => 'leibie'),
//下面是设置默认值
'#default_value' => 'tongzijunleibei',
'#options' => array('tongzijunleibei'=>'童子军', 'jiaoyupeixunleibei'=>'教育培训','huodongleibei'=>'活动'),
);
//提交按钮 //
$form['lianxiwomen']['submit'] = array('#type'=>'submit','#value'=>t('发送'));
$form['lianxiwomen']['submit']['#attributes']=array('class' => 'button');
return $form;
}
//表单函数_validate 验证类似于钩子函数的回调函数
function lianxi_form_validate(&$form, &$form_state)
{
/**
* 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
*/
// form_set_error('',$form_values['name']);
// if($form_state['values']['xinming'] == '')
// {
// form_set_error('xinming',t('必须填写姓名'));
// }
// if($form_state['values']['tel'] == '')
// {
// form_set_error('tel',t('必须填写手机号码'));
// }
if($form_state['values']['tel'] != '' && !preg_match("/^1[3589][0-9]{9}$/",$form_state['values']['tel']))
{
form_set_error('tel',t('手机号码格式不正确'));
}
// if($form_state['values']['address'] == '')
// {
// form_set_error('tel',t('必须填写家庭地址'));
// }
}
//表单函数_submit 提交函数
function lianxi_form_submit(&$form, &$form_state)
{
/**
* 用户提交的所有数据在 $form_state['values'] 之中,相当于 $_POST
*/
$nodeLianxi = (object)$nodeLianxi;
$nodeLianxi->uid = 0;
$nodeLianxi->type = 'lianxiwomen';
$nodeLianxi->field_xinming[0]['value'] = $form_state['values']['xinming'];
$nodeLianxi->field_sex[0]['value'] = $form_state['values']['sex'];
$nodeLianxi->field_tel[0]['value'] = $form_state['values']['tel'];
$nodeLianxi->field_address[0]['value'] = $form_state['values']['address'];
$nodeLianxi->field_leibei[0]['value'] = $form_state['values']['leibei'];
node_save($nodeLianxi);
drupal_set_message(t('报名成功'));
//drupal_add_js('alert("报名成功!")', 'inline', 'footer');
//drupal_goto();
}