开发者

How to add warning text in Drupal comment form

I want a warning message displ开发者_开发问答ayed in the comment form when people try to add comments:

"Please write comments in correct grammatical English, otherwise they will not published"

How can I do it?


Here is how you can do it by using hook_form_alter in your own module:

function mymodule_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
    case "comment_form":
      $form['#prefix'] .= "<div><p>Show some text before the comment form.</p></div>";
    break;
  }
}


You can alter the comment form so that your guidelines are added to it. There are a handful of ways to alter forms in Drupal. You can do it in your theme's template.php file (which I prefer for simple changes) or in a custom module. This article describes both methods, in Drupal 5 and 6, however not for the form you're interested in. However, the method used is the same that leads to the solution below. This is how you can make the change via template.php:

The following PHP code can be added to your theme's template.php file:

function YOURTHEME_theme() {
  return array(
    'comment_form' => array(
      'arguments' => array('form' => NULL),
    ),
  );
}

function YOURTHEME_comment_form($form) {
  $output = '';
  $output .= '<div class="comment-help">' . t('Please write comments in correct grammatical English, otherwise they will not published.') . '</div>';
  $output .=  drupal_render($form);
  return $output;
}

Replace YOURTHEME with the name of your theme. If you already have a YOURTHEME_theme function you will need to add the 'comment_form' key to the array it is already returning. I doubt you do, but it's worth mentioning just in case.

A note: you should not be editing any of the themes in /themes, but you should have made a new theme or copied and renamed any of those themes into /sites/default/themes or /sites/all/themes.

The above code is based on code from this page.


Once you are inside a hook_form_alter function you can use the Development module (http://drupal.org/project/devel) dpm() function in place of var_dump to help view and isolate which properties to change in the big form arrays. I find this is a must-have when trying to figure out changes to an existing form. It puts all the elements of the form array into clickable rows.


In Drupal 7 go to

admin/structure/types/manage/mycontenttype/comment/fields/comment_body

There you can add your text. It will be shown below the field as usual. If you want the warning displayed above the field, you'd have to go the form_alter way.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜