Changing help text and number of columns in Drupal's feedback module

See more about:

Feedback is a contributed module for Drupal, maintained by the prolific Daniel Kudwien, aka sun, of unleashed mind. It shows a widget users can click on to provide instant feedback about your site. It offers a floating Ajaxy form that doesn’t get in the user’s way.

You can learn how Ajax works.

The module’s default help message is “If you experience a bug or would like to see an addition on the current page, feel free to leave us a message.” I wanted something different, as you can see if you use the feedback link on this page. Also, the <textarea> is 60 columns wide. I wanted to change it to 40 columns.

I could have hacked the module, but that would have been bad. When Mr. Kudwien updates the module, I would have had to apply the changes again. Easy to lose track of such things.

Instead, I used hook_form_alter(). It lets you make custom changes to forms. I had already created a module called coredogs_forms with some custom changes to other forms. It already had a hook_form_alter() in it. My task was to add to that function.

First, I had to find out the id of the feedback form. Programmers create form ids with defining forms using the Drupal form API (FAPI). The feedback module has this call in the file feedback.module:

drupal_get_form('feedback_form')

The drupal_get_form() function takes a form id as a parameter, as you can see from the function’s API documentation.

This let me start my own code as follows:

if ( $form_id == 'feedback_form' ) {

Now, now to change the help text? The form is defined in feedback.module as follows:

function feedback_form() {
...
$form['help'] = array(
'#prefix' => '<div class="feedback-help">',
'#value' => t('If you experience...'),

I changed the #value, like so:

$form['help']['#value'] = t(
'Find a mistake? Got a suggestion? Please let us know.'
);

What about the number of columns? Here’s more code from feedback.module.

$form['message'] = array(
'#type' => 'textarea', ...

The #cols setting controls the number of columns in a <textarea>. So I added this to my code:

$form['message']['#cols'] = 40;

So here was the final code:

function coredogs_forms_form_alter(&$form, $form_state, $form_id) {
  //Edit feedback form
  if ( $form_id == ‘feedback_form’ ) {
    $form[‘help’][’#value’] = t(
        ‘Find a mistake? Got a suggestion? Please let us know.’
    );
    $form[‘message’][’#cols’] = 40;
    return;
  }
  ...
}

W00f!


Lessons

How to...


Dogs