jCalendar: A jQuery Based Calendar Goes The Drupal Way

While working on a Drupal based project where we needed a date picker for our forms, my good friend, and old drupaler, Gerhard, recommended me taking a look at Ted Serbinski's jCalendar, a jQuery based calendar.

I started hacking a quick version of jcalendar.module using some ideas of the very helpful jscalendar.module, which uses the jsCalendar javascript library, like using a special CSS class called via #attribute in the form element array to convert textfields into jcalendar enabled.

The jcalendar module uses hook_form_alter() to look for textfields with the add-jcalendar class, I had to use something different than jcalendar which is used later on the generated form elements by the jQuery code.

This is a sample of how to enable jcalendar from one of your textfields once you've installed the module:

// Define a range of years to show
$years = array('0' => 0) + drupal_map_assoc(range(2007,2020));

$plot_dates = array(
'2008-11-03',
'2007-04-01',
'2007-06-11',
); // YYYY-MM-DD

$form['firstdate'] = array(
'#type' => 'textfield',
'#title' => 'The first date',
'#default_value' => '2007-09-29',
'#weight' => -20,
'#attributes' => array('class' => 'add-jcalendar'),
'#jcalendar_year_range' => $years,
'#jcalendar_plot_dates' => $plot_dates,
'#description' => t('YYYY-MM-DD'),
);</code>

The #jcalendar_year_range custom property allows you to provide a range of years, look at the associative array $years, to show in the calendar. If the property is not passed jcalendar.module defaults to the current year plus 20.

#jcalendar_plot_dates, which is not working yet, should allow passing an array of dates to the calendar to plot. I'm not sure if this should be the job of the Drupal module or jCalendar's jQuery code.

This first version of jcalendar.module works, it converts your textfield to three select boxes, day, month, year, and a nice table based date picker, easy to theme via CSS, and once you have the right date you can do whatever you want with it. The original textfield is removed later so your form validation and processing functions should consider if jcalendar is enabled (a simple if (module_exists('jcalendar'))) to handle form elements in a different way.

So, what's the catch? Unfortunately, after I finished my tests and added a second date textfield to my form, I needed to choose a start date and an end date, then I realized jcalendar is duplicating its job for each textfield, it seems the jQuery code only supports one jcalendar per web page. I still need to review the comments in Ted's blog and talk to him to confirm this but I thought it would be a good idea posting what I've done so far in case any of you want to play with the code and improve it.

So here it is, download jcalendar.module and let us know if you get a way to work with more than one calendar per page or alternate ways to alter the original forms.

Newsletter Checkbox in Campaign Monitor Blog

I just got a message from David Greiner telling me he had posted about Newsletter Checkbox in Campaign Monitor's blog.

Newsletter Checkbox is a very simple module but gets the job done and has received good feedback from many users so I'm looking for the time to improve it and release a new version soon.

If you have some suggestion please add your comments.

Redirecting Users After Submitting a Form in Drupal 5

If you create web sites based on Drupal you know that controlling forms, how they are themed, validated and processed, is a highly valued skill for any serious developer. This article will discuss a few ways to redirect your users to another page after they've submitted a form, overriding Drupal's defaults.

Drupal 5 provides developers with a Forms API, a clever way to craft HTML forms using logical and extremely flexible arrays.

Drupal Forms API also allows developers to theme every little detail of a form. For simpler cases using the #prefix and #suffix properties can be enough, but if you need extreme power, and sooner or later you will, then you can use forms-exclusive theme functions and drupal_render().

I'll explain the basics of Forms API in a future article, if you can't wait I suggest taking a look at the Forms API Quickstart Guide, for now let's focus in redirecting users after form submission in Drupal.

In the beginning was the form function

Every form in Drupal starts as a function that creates an array, usually named $form. Then the function's name is passed to drupal_get_form().

As every function in a module, a form creation function should start with the name of the module, for example:

boogeeks_notify_form()

First, this assumes my module is named boogeeks, the first word in my function's name. I'm separating each word with an underscore and I suggest you do the same.

Second, I've chosen the verb notify to have a clear idea of what the form will do. I used something like this in a recent project for hacking a form where users opt-in to receive email alerts.

Third, I added form, helpful when you have many functions in your module and need a quick way to tell apart the ones creating forms.

Notice that the only requirement for naming your form function is the first one, starting with your module name, the other two are my suggestions for cleaner and easier to understand code.

Theme, validate and submit

Now that we have boogeeks_notify_form() taking charge of form creation we need a few additional functions, all of them named based on the original.

  • theme_boogeeks_notify_form(): Will take care of the looks of your form, this is the theming function.
  • boogeeks_notify_form_validate(): Will check if user entered information that validates according to rules established here.
  • boogeeks_notify_form_submit(): Will process the form and redirect the user to other page.

For this article's purposes we are interested in the third function, the one ending in _submit.

Where to go now?

Every form submission function, boogeeks_notify_form_submit() in our example, needs to return a value, this value is the url where the user will go after successfully submitting a form.

So, if you want to thank your user after opting in to receive email alerts then you can finish boogeeks_notify_form_submit() with something like:

return 'thankyou-for-subscribing';

That's the url of some page with the usual thank-you-we-love-you-dear-visitor stuff.

All Drupal provided forms have pre-defined values in their submit functions. Two well known and often used ones are user_login() and user_register(), used to create new accounts, both are created in user.module.

How to override default form redirection in Drupal

There are two ways to override default form redirection, with a destination parameter in a url and using the #redirect property of Forms API. After some testing, I know this could be obvious to hardcore Drupal developers but it's still useful for new comers, I determined the redirection processing flow is as follows:

  • The url returned from the form submit function,
  • which is overriden by #redirect,
  • which is overriden by destination passed in the url.

You'll need to modify a form using hook_form_alter() to use #redirect.

If you want to work with destination you'll have to include it in the url used to call the page showing your form.

Now you can take your users whenever you want after submitting a form, no matter if it's a Drupal provided form or one that you've coded.

Bonus: got a headache with redirection after registration?

Yeah, me too, at least until finding a small bug in user.module, maybe that's your case.

How To Setup FeedBurner StandardStats With Drupal

FeedBurner, the favorite feed management provider of many bloggers has just added site statistics to its StandardStats service.

Now you can use StandardStats to track not only your feed subscribers but your web site audience. But there's a couple of issues with Drupal sites. Let's see how to handle them.

New Subscribers For Your Newsletter Thanks To Drupal

A few days ago, when I published the first news about boogeeks (hint, the first one is Drupal related), many readers registered for my newsletter to get the word on launch day and know about promotions.

Some of them contacted me and a after a few email exchanges decided to register for the newsletter and asked me to add them to the subscribers list. It was obvious that a little checkbox on the contact form would help. That's what I did: a module to get new subscribers to my newsletter from Drupal's contact form.

Quickfile: A Drupal Module For Selling Digital Products

Quickfile, originally named ebook, is now officially a Drupal project.

Get Quickfile at Drupal.org and use it to sell any type of digital product (music, ebooks, pictures, software, to name a few). Your clients won't need to create an account at your Drupal site and installation and management is quite easy.

The module now works with versions 4.7.x and 5.0.x of Drupal and supports payments via Authorize.net and Paypal, which can be enabled and disabled at will.

I will support and update the module for both branches of Drupal.

Download the latest release of Quickfile from Drupal.org or get the most updated code via CVS from the contributions repository (recommended for those who know what CVS is and know how to hack Drupal code).

Thanks a lot to the nice team of developers and all the community at Drupal.org for accepting me as a contributor and for all their help during the programming and commiting process.

I hope you find the module useful and post your comments and suggestions for future versions here.

Entering Similar Lines of Text With vim

There's always something new about vim, or at least something that I learn everyday.

I was just coding a few form elements for Drupal and was getting tired of copying and pasting lines with similar text over a over again. I decided to take a look at vim's manual and found the magical CTRL+Y, exactly what I needed. Let's see how it works.

Joomla and Drupal: The best CMS of 2006

And the winner of the best open source CMS award 2006 is Joomla.

After counting more than 16000 votes and the selections of three judges, there was a tie in the first place between Joomla and Drupal, which had to be resolved by a fourth independent judge.

Joomla is considered to be very easy to install and have one of the largest and more active communities.

Drupal highlighted points were how long the project had been around, its secure code and clever node and permissions system.

What's The Best Open Source Content Management System of 2006?

In just a few hours Packt Publishing will announce the winner of the Open Source CMS Award, a contest where many users around the world voted to choose, together with a judging panel, the content management system with the highest levels of performance, usability, accessibility, ease of configuration, scalability and security.

Go to Hell. Courtesy of Drupal

Nick Lewis' ingenious and funny Road to Drupal Hell deserves to be read by every programmer wanting to get a free ticket to Drupal's hell.

Look out for that smart looking developer guy!