In the process of getting Drupal up and running I have come across some usability quirks that I have slowly been correcting. This is part 1 of an N part series on how I view Drupal should be corrected to be more usuable.
(I say slowly because I have never really done any PHP coding and I have been using trial and error techniques as I don't have an internet connection and am without my usual "group conciousness". If you are coder of any rank try doing your usual work without going to Google or a news group for 4 weeks. It's enough to drive you mad. Luckily I live in Whitehorse now and no one notices if you are mad...)
Before we go on I should note this is a fairly plain 4.6.3 install.
I wanted to add a nice contact form (visible in the top navigation bar above) and so I grabbed one of the more complete modules "feedback" from the Drupal modules download section.
The module was quickly installed and configured but then I realized that it was missing some features I though necessary:
- a rudimentary spam filter (prevent empty emails from peppering my inbox)
- a more intuitive way of notifying a user of form entry errors (a module and system change)
- the removal of the feedback link the module put in the user navigation box (unrequired duplication, I already have a link to it in my top navigation bar)
Thus over the past 4 days (on and off as I was sick) I have cobbled together all these changes. In the process I have discovered a great amount about how Drupal works but also that there are some places that the code is just a bit unfinished.
The first fix was an easy one, I simply copied the configuration code that sets the minimum post length to any node and pasted it into the feedback.module.
$output .= form_select(t('Minimum number of words'), 'feedback_minimum_word_count', variable_get('feedback_minimum_word_count', 0), drupal_map_assoc(array(0, 20, 40, 60, 100, 200)), t('The minimum number of words the message body must contain before it will be sent.'));
I then added the same code that counts words in node.module to the feedback.module at the point it is validating data. Easy.
if (count(explode(' ', $body)) < variable_get('feedback_minimum_word_count', 0)) {
$error_array["form_field_body"] = t('The body of your message is too short. You need at least %words words.', array('%words' => variable_get('feedback_minimum_word_count', 0)));
}
Once these two changes were made I had my simple word count spam filter. A significant to do on this would be to plug it into the spam.module and have spam messages never even reach me in the first place.
The second fix was simple in construction: I just wanted the error messages that appear at the top of the page to be associated with the actual field that is in error. When the user enters invalid data on a form they should be shown exactly where the error is and what it is. It was a combination of simple fixes in the theme.inc, common.inc and feedback.moduls files but required a lot of grep'ing and print statements.
The hook for what I wanted to do already existed in theme.inc but the error text is not printed to the form. Easy. Add it before the form element is drawn but after the title is printed.
if ($error)
{
$output .= " $error\n";
}
An awesome side effect of this change is that all Drupal forms now properly put error messages on the form line that the error occured at. Highly recommended. One thing to note is that because the errors are now associated with the individual form elements it is up to the module writer to handle/display any errors that occur outside of field processing.
I then refactored the feedback.module to use a more informative error messaging technique. No rocket science here, just shove all the errors in a named array which allows us to put the appropriate error message on the appropriate line. Once validation is complete either redraw the form with errors highlighted or show the "your message is sent" message.
The final change, removing the duplicated link, was actually most difficult but in fine fashion resulted in a single line change. I am pretty sure a combination of caching, not saving changes, and impatience made this harder than necessary.
The actual code change to remove the "feedback" link from the user navigation box is to change the type of the menu to be type MENU_HIDE and comment out the $title variable.
// $title = variable_get("feedback_nav_link", "feedback");
// and
if ($may_cache) {
$items[] = array(
'path' => 'feedback',
'callback' => 'feedback_page',
'title' => '',
'access' => $access,
'weight' => 0,
'type' => MENU_HIDE);
I will have to explore more about how menus work once I get back online permanently but in the meanwhile I will bow before the voodoo required.
I have attached the modified theme.inc, common.inc, and feedback.module files to this post. I don't guarantee that they will work with your setup nor do I officially support them. Happy Coding!
| Attachment | Size |
|---|---|
| feedback.module | 16.18 KB |
| common.inc | 59.92 KB |
| theme.inc | 26.39 KB |

Comments
there are much easier ways to do things. The menu can be configured with the menu module. The form can be replaced with a more simple implementation using the forms and survey modules in conjuction (as it now does). Essentially, the work is not needed but the error messages on the form part is a nice touch in my opinion.