尝试添加这些调试值以找到问题的根源(当然不是在实时服务器上......仅在开发环境中):
function hiderating_form_alter(&$form, &$form_state, $form_id) {
global $user;
if ($form_id == "comment_node_stuff_form") {
// I'm not sure why the examples make use of "die" because that will stop the entire
// site from continuing any further.
$flag = flag_get_flag('commented');
// ** Begin debug ** //
// Notify us that we've at least got the right form.
drupal_set_message(t('DEBUG: form id "comment_node_stuff_form" found. Debugging...'), 'warning');
// Warn us if $flag is giving us issues
if (!$flag) {
drupal_set_message(t('DEBUG: No "commented" flag defined.'), 'warning');
}
// Check form components
if (!isset($form['#node']) || !$form['#node']) {
drupal_set_message(t('DEBUG: Something is wrong with or missing from $form["#node"]'), 'warning');
}
// Check more form components
if (!isset($form['#node']->uid)) {
drupal_set_message(t('DEBUG: $form["#node"]->uid is not set.'), 'warning');
}
// Again
if (!isset($form['field_stars']) || empty($form['field_stars'])) {
drupal_set_message(t('DEBUG: <em>$form["field_stars"]</em> is not set or is empty/false.'), 'warning');
}
// Check flag stuff
if (!$flag->is_flagged($form['#node']->nid, $user->uid)) {
drupal_set_message(t('DEBUG: The code <em>$flag->is_flagged($form["#node"]->nid, $user->uid)</em> is returning FALSE.'), 'warning');
}
// Again...
if (!$flag->is_flagged($form['#node']->uid, $user->uid)) {
drupal_set_message(t('DEBUG: The code <em>$flag->is_flagged($form["#node"]->uid, $user->uid)</em> is returning FALSE.'), 'warning');
}
// ** End debug ** //
// Changing this to nid instead of uid
if ($flag->is_flagged($form['#node']->nid, $user->uid)) {
unset($form['field_stars']);
}
// One more debug item:
drupal_set_message('DEBUG: final form structure:<br/><pre>' . check_plain(print_r($form, 1)) . '</pre>', 'warning');
}
}