0

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';
        }

}
  • Inspect the $form array - the comment object is in there – Clive Aug 5 '14 at 15:55
  • Just print it print_r($form); – Michal Przybylowicz Aug 5 '14 at 16:11
  • @gladiatorhl2 Install the Devel module and use dpm($form); - it'll appear on-screen in the messages area – Clive Aug 5 '14 at 16:27
  • OK ... (Array, 39 elements) #entity (Object) stdClass comment (String, 0 characters ) What am I supposed to do with that? – gladiatorhl2 Aug 5 '14 at 16:42
  • I have used the code and those errors disappeared but another error appeared 'Notice: Undefined index: value in function bestreply_form_comment_form_alter()...' Code: if ($form['nid']['value'] == $form['#entity']->comment) { $form['#attributes']['class'][] = 'new-comment-form'; } – gladiatorhl2 Aug 5 '14 at 17:16
1

You have the wrong function signature. It is supposed to be:

hook_form_FORM_ID_alter(&$form, &$form_state, $form_id)

In your function, $comment_count is set as the form_id, and $comment will always be null.

Set it to the signature above, then use $form['nid'] and $form['cid']. To get the comment count on the node, inspect at $form['#node']->comment_count

Here is a replacement code for what you are trying to do:

function MY_MODULE_form_comment_form_alter(&$form, &$form_state, $form_id) {
  if ($form['#node']->comment_count){
    // node has comments
    $form['#attached']['css'] = array(
        drupal_get_path('module', 'MY_MODULE') . '/MY_MODULE.css.', 'file');
  }
  else {
    // no comments on the node
    $form['#attached']['css'] = array(
      drupal_get_path('module', 'MY_MODULE') . '/MY_MODULE2.css.', 'file');
    $form['#attributes']['class'][] = 'new-comment-form';
  }
}
| improve this answer | |
  • I am using this hook_form_FORM_ID_alter as you can see (FORM_ID = comment_form). If I use $form['nid'] == $form['cid'], it does not work because $form['cid'] is not equivalent of $comment->nid. I don't know what to do with that. – gladiatorhl2 Aug 6 '14 at 10:09
  • It is quite surprising that the code works right but only there is this kind of error. – gladiatorhl2 Aug 6 '14 at 10:15
  • You want to add CSS if the user is authenticated and the author, add CSS if there are no comments, and add the class 'new-comment-form' when what exactly? – J. Reynolds Aug 6 '14 at 16:27
  • I would like to add CSS (to hide some fields), if a reply comment form is viewed (not a new comment form). I think that the code $node->nid == $comment->nid is a condition of that. Maybe there is another condition to show that? – gladiatorhl2 Aug 6 '14 at 16:36
  • Actually both $node->nid and $comment->nid are considered to be non-objects. – gladiatorhl2 Aug 6 '14 at 23:25
0

As said in the other answer, the hook signature is wrong, since the correct one is hook_form_FORM_ID_alter(&$form, &$form_state). That is why you get an error about using a property for something that is not an object.

The rest of the code should be corrected too.

  • $form['#node'] contains the node object to which the comment is going to be attached. The following code can be simplified.

    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.');
      }
    }
    

    I would rewrite it as follows.

    if (!empty($form['#node']->uid) && !empty(GLOBALS['user']->uid) && $form['#node']->uid == GLOBALS['user']->uid) {
      // The node has been written from the currently logged-in user.
    }
    
  • You should not assume the comment edit form is shown in the node page. As long as you are changing the comment edit form, the page where the form is shown should not matter.

  • The following code doesn't have sense. Even supposing your intention is to change the comment edit form only when it is shown in the node page, generally speaking, a comment is shown in the same page the commented node is shown.

    if ($node->nid == $comment->nid) {
      // Omissis.
    }
    

    That condition would always be true.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.