Catching Duplicates With hook_form_alter()

(Submitted Wed, 2006-10-04 00:01)

This is part three of a series. If you haven't already, you might be interested in reading previous articles.

In this tutorial, we are going to implement the hook_form_alter() function to test for duplicate entries based on a form field.

I'll be using the nodetracker content type that we built in previous tutorials for demonstration purposes, but the general method is applicable to any form or content type within Drupal.

Now that we have our nodetracker built, we have a small problem. What if we stumble across an interesting looking node, but can't remember if we've already added it to our tracker?

It would be great if upon submission, we could code our nodetracker to reject any entries that have the same node id as a previously submitted entry. There are a number of ways that we could accomplish this. We could implement hook_submit and check to see that the node being submitted doesn't already exist in the database when the node is being saved, or we could implement hook_validate to make the same check when the form contents are being validated.

But both of those solutions require us to have all the necessary form fields filled out and the submit button clicked before they come into effect.

What I want is some sort of immediately visible indication that I am trying to submit a drupal.org node that I have already added to my nodetracker in the past.

The solution? hook_form_alter

hook_form_alter() allows us to modify form contents at the time of rendering. It is the perfect solution for our problem.

We are going to put our code into a custom module. If that sounds scary, it shouldn't. Having your own module space to plug in individualized code for your website is generally a good idea. Sure, you can hack most stuff in at the template level, but that is a bit of a kludge and often a pain to maintain.

Create a file in your modules directory, and give it a recognizable name. The name of your website - or an abbreviation of that name - is a good idea. I've called mine wwdd.module.

<?php

function wwdd_form_alter($form_id, &$form) {

  if (
$form_id == 'content_nodetrack_node_form') {
    if (isset(
$form['field_nodetrack___nodeid'])) {
     
$nodeid =
       
$form['field_nodetrack___nodeid']
             [
'0']
             [
'value']
             [
'#default_value'];

     
$query = "SELECT n.field_nodetrack___nodeid_value FROM
                {node_content_nodetrack} n WHERE
                field_nodetrack___nodeid_value="
. $nodeid;

     
$result = db_fetch_object(db_query($query));

      if (
$result) {
       
$form['field_nodetrack___nodeid']
             [
'0']
             [
'value']
             [
'#default_value'] = 'DUPE!';
        unset(
$form['submit']);
        unset(
$form['preview']);
      }
    }
  }
}

?>

Ok. So what does this do, exactly?

function wwdd_form_alter($form_id, &$form) {

First we declare that any time Drupal is serving up a form, it should call our function in order to let us modify it if necessary.

  if ($form_id == 'content_nodetrack_node_form') {

Here we check to make sure that the form Drupal is serving up is, in fact, the form that we are interested in altering.

    if (isset($form['field_nodetrack___nodeid'])) {

And here we check to make sure that the nodeid field has actually been set to some value.

If you aren't sure exactly what the form id or the field id are for the particular form you might be working with, all you need to do is pull up the form (ie. through the "create content" page) and viewing the source. You will find all the forms and form fields labeled.

      $nodeid =
        $form['field_nodetrack___nodeid']
             ['0']
             ['value']
             ['#default_value'];

      $query = "SELECT n.field_nodetrack___nodeid_value FROM
                {node_content_nodetrack} n WHERE
                field_nodetrack___nodeid_value=" . $nodeid;

In the first section, we assign the value of the nodeid form field to the variable $nodeid. Because we've been using the prepopulate module to fill in these fields through the URL, when we are using our bookmarklet the #default_value of this field will already be set to the node number of the drupal.org node that we want to track.

In the second section, we are building a database query to pull up any nodes of the type "nodetrack - nodeid" where the node id value is the same as the node id number that we've passed in trhough the URL. Remember, we are checking for duplicate entries here.

      $result = db_fetch_object(db_query($query));

      if ($result) {
        $form['field_nodetrack___nodeid']
             ['0']
             ['value']
             ['#default_value'] = 'DUPE!';
        unset($form['submit']);
        unset($form['preview']);
      }

If the database returns any results, then we know we have a duplicate entry. For immediate visual feedback, I've decided that such cases should change the form entry textfield to "DUPE!" (Duplicate!), and just in case I'm not paying attention, also remove the submit and preview buttons from the form entirely.

So, let's see what happens when I try to add a new (unique) node to my nodetracker ...

Great. Just as it should be.

And if I try to add duplicate content?
In the last tutorial, I added node #260 (Installing Drupal) to my nodetracker. What happens if I try to add it again?

And if somehow I miss that glaring "DUPE!" yelling at me, I don't even have a submit button to click on. Perfect.

As I said, this isn't limited to the nodetracker by any means. These techniques can work just as well with any node type, or any form. The hook_form_alter function is a powerful beast. In fact, the entire Drupal hook system is incredibly well conceived and extremely robust.

Submitted by annuities for dummies (not verified) on Tue, 2009-03-17 11:46.

Heh the word dupe reminds me of the old d2 days where we would exploit a bug in code to generate infinite duplicated items.

Submitted by maureen (not verified) on Mon, 2009-03-23 01:09.

but how do you resubmit the form once you've fixed the field content?

Submitted by Dedicated Server (not verified) on Mon, 2009-03-30 17:24.
Submitted by Crystal (not verified) on Thu, 2009-04-16 20:19.

very usefull info at this post thanks!!

Affiliate Promotion

Submitted by Glow Rave (not verified) on Thu, 2009-04-30 06:06.

Drupal yet again. The great things about drupal that I am continuing to hear are simply wonderful. Your post was like the icing on the cake.I am really amazed with drupal. Thanks for the post.

Submitted by Anonymous (not verified) on Thu, 2009-05-07 01:47.
Submitted by zombo09 (not verified) on Thu, 2009-05-07 11:13.

Excellently written article, if only all bloggers offered the same content as you, the internet would be a much better place. Please keep it up! Cheers.
play roulette casino poker
play blackjack online video poker
movie download horse racing betting
iphone wallpapers forex trading systems
alternative energy blackjack casino
play roulette play craps
play slot machine bingo

Submitted by Flashing Novelties (not verified) on Tue, 2009-05-12 01:00.

Thanks for the wonderful series of tutorials. They were extremely helpful and easy to understand too. I hope to read a lot more of your posts on Drupal too. Keep it going.

Submitted by ugg boots (not verified) on Wed, 2009-05-13 10:44.

A pair of jordan shoes or ugg boots? I think you prefer air jordan shoes cos a pair of basketball shoes can help you much in the match. However, someone may saying like this: hey, give me some

Submitted by zack (not verified) on Sat, 2009-07-11 10:43.

Great things about drupal that I am continuing to hear are simply wonderful. Your post was like the icing on the cake.I am really amazed with drupal.

Submitted by rogerkk (not verified) on Tue, 2009-05-19 11:46.

Drupal Theme Developer, is a best Drupal Theme web developing firm in India. We build affordable web for best price. We develop Drupal projects for any web and web pages. We do, Drupal Outsourcing project, PHP & Mysql outsource web development.

Submitted by shower enclosure (not verified) on Fri, 2009-05-22 12:29.

this is ace thank you so much for info

jen x

Submitted by web hosting reviews (not verified) on Fri, 2009-05-29 11:24.

Yet,Another great post.
thanks :)
But your site is full of spam :(

Submitted by Alex (not verified) on Fri, 2009-07-10 16:11.

Drupal Theme Developer, is a best Drupal Theme web developing firm in India. We build affordable web for best price. We develop Drupal projects for any web and web pages. We do, Drupal Outsourcing project,

Submitted by Lucy (not verified) on Sun, 2009-07-19 08:12.

I am trying to submit a drupal.org node that I have already added to my nodetracker in the past.

Hosted By Dreamhost.com