I have an error Notice: Trying to get property of non-object in function MY_MODULE_form_comment_form_alter() This error is related to the line if ($node->nid == $comment->nid) and I think to $comment->nid
I suppose that it is needed to describe $comment->nid as an object like for $node->nid an expression $node = menu_get_object() was used. I was searching for this similar program code and with no success. I have to use $form['#attributes']['class'][] = 'new-comment-form'; and that is why hook_form_alter is used. Maybe there is another solution to this problem?
The code is:
function MY_MODULE_form_comment_form_alter(&$form, &$form_state, &$comment_count, &$comment) {
if (arg(0) == 'node' && is_numeric(arg(1))) {
$node = node_load(arg(1));
if (isset($node->uid, $GLOBALS['user']->uid) && $GLOBALS['user']->uid == $node->uid && $GLOBALS['user']->uid > 0) {
$form['#attached']['css'] = array(
drupal_get_path('module', 'MY_MODULE') . '/MY_MODULE.css.');
}
}
$node = menu_get_object();
if ($form['#node']->comment_count == 0 ){
// hide a field
drupal_add_css(drupal_get_path('module', 'MY_MODULE') . '/MY_MODULE2.css.');
}
if ($node->nid == $comment->nid) {
$form['#attributes']['class'][] = 'new-comment-form';
}
}
$form
array - the comment object is in thereprint_r($form);
dpm($form);
- it'll appear on-screen in the messages area