Syndicate

Feed

Using Ajax in Drupal 6

John K. VanDyk confirmed that Pro Drupal Development 2nd edition will be published early this summer. I don’t know about you but in Montreal it sure does not look like summer. We’re still in the dead of winter, and it’s one snowstorm after another.

Anywho, I got busy last night and, on a Drupal 6 install, I worked through chapter sweet seventeen of the Pro Drupal Development 1st edition book. The title of the chapter is "Using jQuery".

(Hey, here is an idea for a thread: name the one computer book you’ve actually read from cover to cover in your life. My answer: Pro Drupal Development, and probably just a few other books.)

You’ll find attached to this post a working digg-like module, different enough from the Drupal-5-compliant one provided with the book that I don’t believe I’ll receive any email about me committing copyright infringement.

So here are the eight changes you should bring to the module plus1 to make it work in Drupal 6.

But before we delve into this, here is something you should know. If you’ve worked through the example at the beginning of the chapter where we add jQuery code to a node, and you have not seen the first paragraph fade in as it did in Drupal 5, don't be alarmed. You’ll have to first hide the paragraph. When you think about it... the fact that this worked as is with an earlier version of the jQuery library is a bug.

In Drupal 5, after adding the following to a node, you would see the first paragraph fade in:

<?php
drupal_add_js('$(document).ready(function(){
  $("#one").fadeIn("slow");
  });',
  'inline'
  );
?>
<p id="one">Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>

In Drupal 6, you have to hide the paragraph first, then fade it in.

<?php
drupal_add_js('$(document).ready(function(){
  $("#one").hide().fadeIn("slow");
  });',
  'inline'
  );
?>
<p id="one">Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>

Or you may harness the full power of jQuery by selecting the first paragraph of the node’s content, and make that paragraph fade in for the duration of 5 seconds, ie: 5000 ms.

<?php
drupal_add_js('$(document).ready(function(){
  $(".content p:first").hide().fadeIn(5000);
  });',
  'inline'
  );
?>
<p>Paragraph one</p>
<p>Paragraph two</p>
<p>Paragraph three</p>

On with the show.

1. Open and edit your plus1.install file. Table creation for modules has been abstracted into a Schema API in Drupal 6.

In Drupal 5, we created our table {plus_1} this way:

<?php
// $Id$
/**
* Implementation of hook_install().
*/
function plus1_install() {
  switch (
$GLOBALS['db_type']) {
    case
'mysql':
    case
'mysqli':
     
db_query("CREATE TABLE {plus1_vote} (
        uid int NOT NULL default '0',
        nid int NOT NULL default '0',
        vote tinyint NOT NULL default '0',
        created int NOT NULL default '0',
        PRIMARY KEY (uid,nid),
        KEY score (vote),
        KEY nid (nid),
        KEY uid (uid)
      ) /*!40100 DEFAULT CHARACTER SET UTF8 */"
);
      break;
    case
'pgsql':
     
db_query("CREATE TABLE {plus1_vote} (
        uid int NOT NULL default '0',
        nid int NOT NULL default '0',
        vote tinyint NOT NULL default '0',
        created int NOT NULL default '0',
        PRIMARY KEY (uid,nid)
      );"
);
     
db_query("CREATE INDEX {plus1_vote}_score_idx ON {plus1_vote} (vote);");
     
db_query("CREATE INDEX {plus1_vote}_nid_idx ON {plus1_vote} (nid);");
     
db_query("CREATE INDEX {plus1_vote}_uid_idx ON {plus1_vote} (uid);");
      break;
  }
}
?>

In Drupal 6, we do this instead:

<?php
// $Id$
/**
* Implementation of hook_install().
*/
function plus1_install() {
 
// Create tables.
 
drupal_install_schema('plus1');
}
/**
* Implementation of hook_schema().
*/
function plus1_schema() {
 
$schema['plus1_vote'] = array(
   
'description' => t('The table used by the Plus1 module.'),
   
'fields' => array(
     
'uid' => array(
       
'description' => t('The primary identifier for the voter.'),
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => TRUE),
     
'nid' => array(
       
'description' => t('The node that gets a vote.'),
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => TRUE),
     
'vote' => array(
       
'description' => t('The vote.'),
       
'type' => 'int',
       
'size' => 'tiny',
       
'unsigned' => TRUE,
       
'not null' => TRUE),
     
'created' => array(
       
'description' => t('The timestamp of when the voter voted.'),
       
'type' => 'int',
       
'unsigned' => TRUE,
       
'not null' => TRUE)),
   
'primary key' => array(
     
'uid',
     
'nid'),
   
'indexes' => array(
     
'score' => array('vote')),
  );
  return
$schema;
}
/**
* Implementation of hook_uninstall().
*/
function plus1_uninstall() {
 
// Remove tables.
 
drupal_uninstall_schema('plus1');
}
?>

Note that we’re creating a composite key as primary key. The voter and the node he votes for will always come in a unique combination. In other words, you can give a thumbs up only once for a any given content.

2. We then modify our plus1.info file.

In Drupal 5:

name = Plus 1
description = "A +1 voting widget for nodes. "
version = "$Name$"

In Drupal 6:

name = Plus 1
description = "A +1 voting widget for nodes."
core = 6.x

('Version' is deprecated in Drupal 6.)

3. We change our use of the menu hook in plus1.module.

In Drupal 5 we had:

/**
* Implementation of hook_menu().
*/
function plus1_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'plus1/vote',
      'callback' => 'plus1_vote',
      'type' => MENU_CALLBACK,
      'access' => user_access('rate content'),
    );
  }
  return $items;
}

In Drupal 6, we do the following:

/**
* Implementation of hook_menu().
*/
function plus1_menu() {
  $items['plus1/vote/%'] = array(
    'title' => 'Vote',
    'page callback' => 'plus1_vote',
    // 'plus1' is the 0th arg. in the path, 'vote' is the 1st, 
    // and the node id is the 2nd...
    // hence, we pass array(2) to 'page arguments'
    'page arguments' => array(2), // where my wildcard is
    'access arguments' => array('rate content'),
    // always use MENU_CALLBACK for ajax requests
    'type' => MENU_CALLBACK,
  );
  return $items;
}
?>

Note that this symbol: % is a wildcard for the node hii-dee.

4. We modify the callback function plus1_vote($nid).

We have a new and improved way to write JSON in Drupal 6. We use the new function drupal_json($var = NULL). It sets the header for the JavaScript output to 'Content-Type: text/javascript; charset=utf-8'.

In Drupal 5:

/* This print statement will return results 
 * to the jQuery's request.
 */
print drupal_to_js(array(
  'score' => $score,
  'voted' => t('You voted'),
  )
);

In Drupal 6:

/* This print statement will return results 
 * to the jQuery's request.
 */
print drupal_json(array(
  'score' => $score, 
  'voted' => t('You voted'),
  )
);

5. We register our theme function.

In Drupal 6, all modules must register all their theme functions via the new hook hook_theme().

Therefore, we add the following function to our plus1.module file.

/**
* Implementation of hook_theme().
*/
function plus1_theme() {
  return array(
    'plus1_widget' => array(
      'arguments' => array('nid', 'score', 'is_author', 'voted'),
    ),
  );
}

6. We modify our theme function theme_plus1_widget($nid, $score, $is_author, $voted) to account for the change in signature of the function l() in Drupal 6.

In Drupal 5:

<?php
$output
.= l(t('Vote'), "plus1/vote/$nid", array('class' => 'plus1-link'));?>

?>

In Drupal 6:

<?php
$output
.= l(t('Vote'), "plus1/vote/$nid", array('attributes' => array('class' => 'plus1-link')));
?>

7. We thoroughly clean up our act when it comes to our jQuery.

In the source code of our Drupal 5-compliant version, the file jquery.plus1.js contained this:

// $Id$
// Global killswitch: only run if we are in a supported browser.
if (Drupal.jsEnabled) {
  $(document).ready(function(){
    $('a.plus1-link').click(function(){
      var voteSaved = function (data) {
        var result = Drupal.parseJson(data);
        $('div.score').fadeIn('slow').html(result['score']);
        $('div.vote').html(result['voted']);
      }
      $.get(this.href, null, voteSaved);
      return false;
    });
  });
}

In Drupal 6, we will NOT use the method Drupal.parseJson — that function is deprecated in Drupal 6. We’ll use the jQuery.getJSON method instead.

// $Id$
// Global killswitch: only run if we are in a supported browser.
if (Drupal.jsEnabled) {
  $(function(){
    $('a.plus1-link').click(function(){
      $.getJSON(this.href, function(json){
        $('div.score').hide().fadeIn('slow').html(json.score);
        $('div.vote').html(json.voted);
      });
      return false;
    });
  });
}

8. Then we’ll go and read about this wonderful jQuery method jQuery.getJSON. For your convenience

This module has been committed to Drupal CVS. The link to the project page is http://drupal.org/project/plus1.
AttachmentSizeHitsLast download
plus1.tar12 KB408 years 28 weeks ago
Last edited by Caroline Schnapp about 7 years ago.

One file is attached to this posting. Login or register to download this file.


Comments

The condo’s facilities

The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Punggol. http://www.braungresham.com/2013/05/david-braun-begins-summer-risk-management-webinar-series-on-52313/

thank you for your wonderful

thank you for your wonderful sharing advice and tips on how to use Ajax Drupal 6 . much appreciated
Forestville EC

Corals at Keppel Bay

Wonderful article. Thanks for sharing.

Thanks for sharing with us

Thanks for sharing with us some direction on how to use Ajax Drupal 6 correctly.
Corals At Keppel Bay
D'pristine

I really adore your

I really adore your commitment to offer your readers such valuable information. Looking forward to read such informative content over and over again.
Corals At Keppel Bay
D'pristine

I just stumbled upon your

I just stumbled upon your chatty blog and wanted to say that I have really enjoyed reading your very well written blog posts. I will be your frequent visitor, that's for sure.

hank you for your wonderful

hank you for your wonderful sharing advice and tips on how to use Ajax Drupal 6 . much appreciated

In almost variety of

In almost variety of crafting as well as business presentation you wish to believe penning dissertation regarding the institutional framework (the university or), with your target market (that are checking out your personal composition).

The condo’s facilities

The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Bartley.
Kensington Square

Skypark Residences has full

Skypark Residences has full and unique facilities, which includes a guard house, clubhouse, Function Room & Indoor Gym Tennis Court, 50m Freeform Pool Pool Deck, Wading Pool, Splash Pool & Family Pool Jacuzzi & Hydro Spa, BBQ Area Dining and Play Fountain, Fitness Alcove & Children’s Playground and Garden Trail. The condo’s facilities provide full family entertainment needs for your family and loved ones. Indulge in a serene and tranquil lifestyle right in the heart of Woodlands.
Skypark Residences

With expected completion in

With expected completion in mid 2016, Sea Horizon EC comprises of 13 towers with 495 units and stands 13 storeys tall. Future residents will be able to access the development via Pasir Ris MRT.
Sea Horizon

The Glades has full and

The Glades has full and unique facilities, which includes a guard house, clubhouse, children’s playground, swimming pool, kid’s pool, function room, dining pavilion, playground, poolside BBQ, waterjet pool.
The Glades

comment

Nice blog. If you always attach positive emotions to the things you want, and never attach negative emotions to the things you don't, then that which you desire most will invariably come your way. Please do keep up the awesome job. Great blog!

The Tembusu

It's awesome to know that

It's awesome to know that John K. Vandyk confirmed that Pro Drupal development second edition will be published early this summer. Thanks for this information.
TP180 Guocoland
visit tp180 here for more info

It's awesome to know that

It's awesome to know that John K. Vandyk confirmed that Pro Drupal development second edition will be published early this summer. Thanks for this information.
TP180 Guocoland
visit tp180 here for more info

With such a short drive to

With such a short drive to the city area as well as the orchard and bugis area, entertainment for your love ones and family will come at a stone’s throw away in The Panorama. The Panorama ang mo kio

Good Post

Thank you for your well-thought content. I'm really at perform proper now! So I ought to go off with no reading through all I'd like. But, I place your web site on my RSS feed in order that I can read mor

Ingin Cepat Hamil
Buku
Tips
Cepat
Hamil
Buku Tips
Tips Cepat
Cepat Hamil
Hamil Buku
Buku Cepat
Buku Hamil

This is a remarkable post.

This is a remarkable post. This post gives truly quality information. I'm definitely going to look into it. It provides very useful tips. Thank you so much. Keep up the good work. jeunesse reserve

This is a remarkable post.

This is a remarkable post. This post gives truly quality information. I'm definitely going to look into it. It provides very useful tips. Thank you so much. Keep up the good work. jeunesse reserve

Sky Vue Condo is a new and

Sky Vue Condo is a new and upcoming new launch located in the Bishn area, nested right beside Bishan MRT. Sky Vue is within a short drive to Little India, Orchard and city area. With expected completion in mid 2017, it comprises of 694 units in total with 2 Blocks and 17 storeys. Sky Vue

Yuan Ching Road EC will be

Yuan Ching Road EC will be accessible via Lakeside MRT station as well many of the buses along Yuan Ching Road and Corporation Drive. Yuan Ching Road EC

Future residents will be

Future residents will be able to walk from The Hillford Condo to the upcoming Downtown Line Beauty World MRT. Also, nature awaits your family and friends at the nearby Dairy Farm Nature Park and the Bukit Batok Nature Park. Also, the ultimate nature awaits you at Bukit Timah Nature Reserve. The Hillford

Jurong West new Condo Launch by MCL And

Jurong West Condo is a 99-years leasehold Jurong West New Launch Condo development located at Jurong West Street 41. Jurong West Condo BY MCL land

wow

this is really something good!

Boardwalk Residences in Sengkang Fernvale Close

Boardwalk Residences will be accessible with Layar LRT Station as well as Sengkang Bus Interchange. It is also right beside Tampines Expressway(TPE). Boardwalk Residences is also near to Greenwich V and the Upcoming Seletar Mall. Boardwalk condo

Pet Grooming Centres in Singapore

It is not very easy to steal scenes from Jack Nicholson. But in critically acclaimed ’97 a pint-sized dog manages to do so. In the role of Verdell, an adorable dog became the fourth major character in the film. Overnight it became so famous that people started to ask about what kind of dog was that Brussels Griffon

Great

Nestled in the Mix of the Modern and Heritage locations, well-connected and fantastic location. Be intrigued by the wonders of
Highline Residences Tiong Bahru

Pengobatan Tradisional

Alexa Rank Checker

New Alexa Rank Checker online for your site position.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <css> <html> <javascript> <mysql> <php> <span> <a> <b> <i> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <sup> <sub> <dd> <del> <blockquote> <img> <q> <p> <div>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <css>, <html>, <javascript>, <mysql>, <php>, <rails>, <ruby>.

More information about formatting options

CAPTCHA
I have to wonder if you're a human spammer or a machine, or less likely someone who cares to leave his or her thoughts behind.