用户登录

Drupal资源链接

http://zhupou.cn
drupal布道者,改成了"老葛的Drupal培训班",专心于培训事业
http://drupal.org
官方网站
http://drupalchina.org
中文的官方网站
http://acquia.com/
本站就是用这个版本构建的

使用hook_form_alter关闭CCK字段的输入无效

一般情况下,我们可以使用hook_form_alter对form进行修改,但对CCK字段进行修改时,却发现找不到CCK的字段。能过google发现有2种方法。

1、修改使用hook_form_alter的模块的weight 值,将这个值变大,实际在就是延后执行,(这种方法好象对于现在的CCK模块无效,可能早期有用。

2、使用一个技巧,

<?php
/**
* @file
* Custom module to set the disabled attribute of CCK fields.
*/
/**
* Implementation of hook_form_alter().
*/
function mysnippet_form_alter(&$form, $form_state, $form_id) {
if (isset($form['type']) && isset($form['#node'])) {
// Use this check to match node edit form for a particular content type.
if ('mytype_node_form' == $form_id) {
$form['#after_build'][] = '_mysnippet_after_build';
}
// Use this check to match node edit form for any content type.
// if ($form['type']['#value'] .'_node_form' == $form_id) {
// $form['#after_build'][] = '_mysnippet_after_build';
// }
}
}
/**
* Custom after_build callback handler.
*/
function _mysnippet_after_build($form, &$form_state) {
// Use this one if the field is placed on top of the form.
_mysnippet_fix_disabled($form['field_myfield']);
// Use this one if the field is placed inside a fieldgroup.
// _mysnippet_fix_disabled($form['group_mygroup']['field_myfield']);
return $form;
}
/**
* Recursively set the disabled attribute of a CCK field
* and all its dependent FAPI elements.
*/
function _mysnippet_fix_disabled(&$elements) {
foreach (element_children($elements) as $key) {
if (isset($elements[$key]) && $elements[$key]) {
// Recurse through all children elements.
_mysnippet_fix_disabled($elements[$key]);
}
}
if (!isset($elements['#attributes'])) {
$elements['#attributes'] = array();
}
$elements['#attributes']['disabled'] = 'disabled';
}
?>