1

Drupal is showing error message for empty comments by default. how can i remove that error message. Note:- (I have not written any custom validation. It's the default validation provided by drupal module.) My changes in comment.module

if (empty($comment->cid) && empty($comment->pid)) { 
if(($form['#node']->type=='iks_videos_youtube_video') || ($form['#node']-               >type=='iks_videos_vimeo_video' || $form['#node']->type=='iks_videos_video')){
if(isset($form['#node']->type) && $form['#node']->type=='iks_videos_youtube_video')
{   $vtype='yt';}
else
{   $vtype='v'; }
$form['#action'] = url('video/' . $form['#node']->video_id[LANGUAGE_NONE][0]['value'] . '/'.$vtype.'/' . strtolower(str_replace(' ', '_', $form['#node']->title)));
  }else{
$form['#action'] = url('comment/reply/' . $comment->nid);}

}

my custom module code

function  iks_videos_form_comment_node_iks_videos_youtube_video_form_alter(&$form, $form_estate) {
if (isset($form['actions']['preview']))
unset($form['actions']['preview']);
$form['subject']['#access'] = FALSE;
unset($form['author']['mail']['#description']);
$form['#submit'][] = '_iks_videos_comment_form_submit';
    }

alter form

function _iks_videos_comment_form_submit($form, &$form_estate) {

if(($form['#node']->type) && $form['#node']->type=='iks_videos_youtube_video')
    $vtype='yt';
else 
    $vtype='v';
$form_estate['redirect'][0] = 'video/' . $form['#node']->video_id[LANGUAGE_NONE][0]['value'] . '/'.$vtype.'/' . strtolower(str_replace(' ', '_', $form['#node']->title));

}

7
  • you musn't change any code in Drupal core modules, it's very bad hardcode. You can do all your logic that you added to comment.module in hook_form_alter.
    – milkovsky
    Jul 26, 2013 at 10:32
  • ok @milkovsky let me change the comment.module to default settings.. Actually validation is not my problem. but the validation is showing in another page called /comment/reply. how can i make the validation show in my custom page i mentioned above
    – Ajmal
    Jul 26, 2013 at 10:41
  • you can check add by url of page. e.g. if(arg(0)=='node'){...}
    – milkovsky
    Jul 26, 2013 at 11:48
  • i tried. it is redirecting to my custom page. but the error message is not displaying there. it is displaying is some other page which i navigate next
    – Ajmal
    Jul 26, 2013 at 12:02
  • change #action in form alter
    – milkovsky
    Jul 26, 2013 at 12:04

1 Answer 1

1

You can see error message because comment_body is required for comment entity. You can delete #required parameter for comment_body:

function my_module_form_alter(&$form, &$form_state, $form_id) {
  if(strpos($form_id, 'comment_node_erpal_task_form') !== FALSE) {
    $form['comment_body'][LANGUAGE_NONE][0]['#required'] = FALSE;
  }
}

But just know that you'll have php notice then. Because posting comment without the body is not right:

Notice: Undefined offset: 0 in comment_submit() (line 2197 of W:\home\erpal.local\www\modules\comment\comment.module).

Make sure yhat you added custom validation for body field.

You you can handle validation callbacks for the whole form in $form['#validate'] array. Also you can handle validation callbacks for each form element in #element_validate array. E.g. for comment_body:

$form['comment_body'][LANGUAGE_NONE]['#element_validate'][] = '_my_module_comment_body_validate';

Update

You musn't change any code in Drupal core modules, it's very bad hardcode. You can do all your logic that you added to comment.module in hook_form_alter.

function my_module_form_alter(&$form, &$form_state, $form_id) {
  if(strpos($form_id, 'comment_node_erpal_task_form') !== FALSE) {
    $form['comment_body'][LANGUAGE_NONE][0]['#required'] = FALSE;

    if(isset($form['#node']->type) && $form['#node']->type=='iks_videos_youtube_video') {
      $vtype='yt';
    }
    else {
      $vtype='v'; 
    }

    $url = url('video/' . $form['#node']->video_id[LANGUAGE_NONE][0]['value'] . '/'.$vtype.'/' . strtolower(str_replace(' ', '_', $form['#node']->title))); 
    $form['#action'] =  $url;  
  }
}
3
  • drupal.org/node/1609256 please go through the issue here. I'm asking the solution for this
    – Ajmal
    Jul 26, 2013 at 11:56
  • the issue it's for drupal 8
    – milkovsky
    Jul 26, 2013 at 12:02
  • @milkovsky It looks like the issue was created for Drupal 7, then assigned to Drupal 8, and a patch will be backported. This is how most core bugs in 7 are handled right now.
    – mpdonadio
    Aug 7, 2013 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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