以编程方式更改 CCK 文本字段
我正在尝试将“显示”复选框添加到多个 cck 文本字段。下面的代码确实添加了该字段,但是当我提交表单时,在验证表单时我看不到 $form_state 数组中的值。
我可能做错了什么?
<?php
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function leghist_form_paleghist_node_form_alter(&$form, &$form_state) {
//Add function to manipulate cck node form
$form['#after_build'][] = 'leghist_cck_after_build';
}
/**
* Implementation of hook after_build();
*/
function leghist_cck_after_build ($form, &$form_state) {
//Add display option to titles
foreach (element_children($form['field_lg_pop_names']) as $key) {
$form['field_lg_pop_names'][$key]['display'] = array(
'#type' => 'checkbox',
'#title' => t('Display'),
'#process' => array('leghist_display_process')
);
}
return $form;
}
/**
* Implementation of hook element_process
*/
function leghist_display_process($element) {
dsm('yuk');
}
?>
I'm trying to add a 'Display' check box to multiple cck textfield. The code below does add the field, but when I submit the form I do not see the values in the $form_state array when I'm validating the form.
What could I be doing wrong?
<?php
/**
* Implementation of hook_form_FORM_ID_alter().
*/
function leghist_form_paleghist_node_form_alter(&$form, &$form_state) {
//Add function to manipulate cck node form
$form['#after_build'][] = 'leghist_cck_after_build';
}
/**
* Implementation of hook after_build();
*/
function leghist_cck_after_build ($form, &$form_state) {
//Add display option to titles
foreach (element_children($form['field_lg_pop_names']) as $key) {
$form['field_lg_pop_names'][$key]['display'] = array(
'#type' => 'checkbox',
'#title' => t('Display'),
'#process' => array('leghist_display_process')
);
}
return $form;
}
/**
* Implementation of hook element_process
*/
function leghist_display_process($element) {
dsm('yuk');
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使在阅读了 hook_form_alter() 和 CCK 字段之后,仍然了解在 CCK 上使用 hook_form alter 时要注意的陷阱我仍然被难住了,因为我基本上就是在做他们提供的事情。
我最终决定尝试另一种方法,以下更改对我有用:
Even after reading hook_form_alter() and CCK fields on the pitfalls to watch-out for when using hook_form alter on CCK I was still stumped, because I'm essentially doing exactly what they offer.
I finally decided to try another approach, and the following changes worked for me:
像 #after_build 函数中那样改变形式是一个坏主意。虽然表单元素可能会出现在页面上,但它们不会作为表单的一部分进行处理。
您应该能够通过直接的形式更改和自定义模块的高模块重量来实现您想要的目标。
Altering the form like that in an #after_build function is a bad idea. While the form elements may appear on the page, they will not be processed as part of the form.
You should be able to achieve what you want with a straight form alter and a high module weight for your custom module.