欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

hook_form_alter and select lists

shiping1 的头像

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.

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

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 during hook_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

普通分类: