欢迎各位兄弟 发布技术文章
这里的技术是共享的
function shipingzhongcustom_form_alter(&$form, &$form_state, $form_id)
{
//下面是添加的时候给总表传默认值
// //var_dump($node);var_dump($form_id);var_dump("QQQQQQqq");
//if($form_id=='jingjiazongbiaotype_node_form' || $form_id=='kefuzongbiaotype_node_form' || $form_id=='seozongbiaotype_node_form' )
//{
// $thirdPara = arg(3);//这是第三个参数
//if(!empty($thirdPara)){
// $form['field_zong_time'][0]['#default_value']['value'] = arg(3);
//}
//}
if($form_id == 'kefuchat_node_form'){
//var_dump($form['field_qq_property']);
$form['#after_build'][] = '_afterFormSelect';//表示它要调用它
}
}
function _afterFormSelect($form, &$form_state) {
$options = array(
"1" => "option1",
"2" => "option2",
);
//dsm($form['field_qq_property']['value']['#options']); //其实这个很重要,因为要调试它
$arrOptions = $form['field_qq_property']['value']['#options'];
unset($arrOptions['400']);
unset($arrOptions['baidushangqiao']);
unset($arrOptions['kuishangtong']);
unset($arrOptions['qiyeqq']);
$form['field_qq_property']['value']['#options'] = $arrOptions;
return $form;
}
//下面是自己亲自做的例子
function shipingzhongcustom_form_alter(&$form, &$form_state, $form_id)
{
//下面是添加的时候给总表传默认值
// //var_dump($node);var_dump($form_id);var_dump("QQQQQQqq");
if($form_id=='jingjiazongbiaotype_node_form' || $form_id=='kefuzongbiaotype_node_form' || $form_id=='seozongbiaotype_node_form' )
{
$thirdPara = arg(3);//这是第三个参数
if(!empty($thirdPara)){
$form['field_zong_time'][0]['#default_value']['value'] = arg(3);
}
}
if($form_id == 'kefuchat_node_form'){
//var_dump($form['field_qq_property']);
$form['#after_build'][] = '_afterFormSelect';
}
if($form_id == 'kefuchat_node_form'){
//var_dump($form['field_qq_property']);
$form['#after_build'][] = '_trimTel';
}
if($form_id == 'kefuchat_node_form'){
$form['#after_build'][] = '_kefuchat_node_form_validation';
}
}
function _trimTel($form, &$form_state){
$form_state['values']['field_tel'][0]['value'] = trim($form_state['values']['field_tel'][0]['value']);
return $form;
}
function _afterFormSelect($form, &$form_state) {
$options = array(
"1" => "option1",
"2" => "option2",
);
//$form['field_qq_property']['und']['#options'] = $options;
//dsm($form['field_qq_property']['value']['#options']);
$arrOptions = $form['field_qq_property']['value']['#options'];
unset($arrOptions['leyu']);
//unset($arrOptions['xingqubaidushangqiao']);
unset($arrOptions['xingqushoujiliuyan']);
//unset($arrOptions['400xinqu']);
//unset($arrOptions['liuyan']);
unset($arrOptions['400']);
unset($arrOptions['baidushangqiao']);
unset($arrOptions['kuishangtong']);
unset($arrOptions['qiyeqq']);
$form['field_qq_property']['value']['#options'] = $arrOptions;
return $form;
}
function _after_build_kefuchat_node_form_validate($form, &$form_state) {
$form['#validate'][] = '_kefuchat_node_form_validation';
}
function _kefuchat_node_form_validation($form, &$form_state) {
$arg0=arg(0);
$arg1=arg(1);
$arg2=arg(2);
$form_state['values']['field_tel'][0]['value'] = trim($form_state['values']['field_tel'][0]['value']);
if($form_state['values']['field_tel'][0]['value'] && ($arg0=='node' && $arg1='add' && $arg2=='kefuchat'))
{
//var_dump($form_state['values']['field_suozaixiaoqu']['value']);
//var_dump($form_state['values']['field_tel'][0]['value']);
$sql = "select n.nid as nid from {node} as n inner join {content_type_kefuchat} as kefu on n.nid=kefu.nid
inner join {content_field_tel} as tel on n.nid=tel.nid where n.type='kefuchat' and tel.field_tel_value='%s'";
$result = db_query($sql,$form_state['values']['field_tel'][0]['value']);
$rowIds = array();
while ($rowObject = db_fetch_object($result))
{
$rowIds[]= $rowObject->nid;
}
$rowObjs = array();
foreach($rowIds as $id)
{
$rowObjs[] = node_load($id);
}
foreach($rowObjs as $key=>$obj)
{
drupal_set_message('以前存在过这个号码','error',false);
$form_state['values']['field_tel'][0]['value'] = $form_state['values']['field_tel'][0]['value']."cf_tel";
// empty($obj->field_suozaixiaoqu[0]['value']) && $obj->field_suozaixiaoqu[0]['value']='wz';
//
// if($obj->field_suozaixiaoqu[0]['value'] == $form_state['values']['field_suozaixiaoqu']['value']){
// form_set_error('field_suozaixiaoqu', "对不起已存在这个电话号码,原来的电话号码<a href='/node/".$obj->nid."/edit'>在这里</a>!");
// }
// else if($obj->field_suozaixiaoqu[0]['value'] != $form_state['values']['field_suozaixiaoqu']['value']){
// drupal_set_message('已在另一个校区存在这个号码','error',false);
// $form_state['values']['field_tel'][0]['value'] = $form_state['values']['field_tel'][0]['value']."cf_tel";
// }
}
// var_dump($form_state['values']['field_suozaixiaoqu']);
// var_dump($rowObjs);
// exit;
}
return $form;
}
I'm having a hard time finding an example of the right way to do this. I want to use hook_form alter to change the options of a select list.
Can anyone point me to an example or give me a hint on how to go about it?
Comments
Starting off I felt like this
Starting off I felt like this was an easy endeavor. But I'm definitely struggling.
I'm seeing stuff about CCK being special. Stuff about "after_build." I wish there was a clear cut example of how to do this.
Argh.
Login or register to post comments
You can try giving the same
You can try giving the same form item and give the form options which you want to change by declalring those options here
Login or register to post comments
First of all, do this to see
First of all, do this to see what's currently available:
<?php
echo '<pre>';
print_r($form);
echo '</pre>';exit();
?>
Certain elements do not yet have their
#options
populated duringhook_form_alter
. In such cases you won't be able to simply change the values from form alter, but will need to use after_build. In your form_alter:<?php
$form['#after_build'] = 'my_function';
?>
And then:
<?php
function my_function($form, &$form_state) {
//modify data here!
echo '<pre>';
print_r($form);
echo '</pre>';exit();
}
?>
---
Yuriy Babenko | Senior Developer
http://yuriybabenko.com
来自 https://drupal.org/node/1068754