Search code examples
drupaldrupal-6

Adding a new message field in comment form using form_alter, want to know if it's correctly done


I want to know if it is done correctly and want suggestions of improvement.

I've just now added this warning in the comment form:

→Comment Writing Instructions:
Comments written in incorrect English, improper grammar, using short forms, using Hindi words etc - those comments will be deleted.

Please scroll down in this web page implementation here.

Here is the form_alter module code:

<?php

function change_comment_form_form_alter(&$form,&$form_state,$form_id)
{

if( $form_id==="comment_form" 
)
  {

  $form['warning'] = array(
  '#type' => 'item',
  '#title' => t('&rarr;Comment Writing Instructions'),
  '#value' => 'Comments written in incorrect English, improper grammar, using short forms, using Hindi words etc - those comments will be deleted.',

  '#weight' => -15,

);


  }

}

Solution

  • It is the correct way to alter form.

    However, it will be more user-friendly if text is shown just before or after comment textarea. You should hide "input format" fieldset as well.

    In your code, you should surround all written sentences in t() as long as you can translate your sentence. Example of rewritting of your code :

     '#title' => '&rarr;' . t('Comment Writing Instructions'),
     '#value' => t('Comments written in incorrect English, improper grammar, using short forms, using Hindi words etc - those comments will be deleted.'),
    

    EDIT: Drupal FAPI or Forms API has a very well documented page here : http://api.drupal.org/api/drupal/developer--topics--forms_api_reference.html/6

    You should have a look at all #type availables.