Call Now 512.782.4514

CCK create automatically updating year select field

Aaron Hawkins Posted by Aaron Hawkins September 10, 2010
  • Share

When you want to create a list of years as a select field in CCK ranging from some set date in the past to the current date you have a couple of options. The first option would be to go through and manually enter every year from that date to present. This may be ok if you only are working with a date range of 3 or 4 years, but what if you want to list the years since the 1800s. Obviously that list would be painful to make, and what's worse you would have to make sure the site was updated every year by someone competent enough not to destroy the information. There is of course another solution. It takes a bit more thinking, but it is more resilient.

When creating your select field for your content type open the "PHP code" section, leave the normal "Allowed values list:" area blank, and enter the following code in the text area:

return year_list();

Then go either to your template.php file or to a custom module and enter the following code. (I recommend putting the code into a create a module that accompanies my theme development.)

// You can name this function whatever you want, just be consistent.
function year_list() {
  $year_array = array();
  // Substitute the start date that you want here.
  $year = 1960;
  // If you want the date to end in some other time besides the present then just set the $end variable to the date you desire.
  $end = (int)date('Y',time());
  while ($year <= $end) {
    $year_array[$year] = $year;
    $year++;
  }
  return $year_array;
}

What this does is it extracts year from the current date and creates a loop that starts at the date you set (1960 in this case) and stops at the present date. This means that no matter how many years go by the code will continue to work with no editing.

IMPORTANT: Don't skip the step of separating out the function from the input field. If you make a mistake in the code it is much easier to fix when it is in a file.

PixelClever newsletter

Stay informed on our latest news!

Syndiquer le contenu
Drupal Tutorals RSS

Drupal Tutorials

How to Declare a Views Template Inside of a Module

The usual rule of thumb in web development is that styling (css, javascript etc...) should be done primarily inside of themes....
[Read More]

How to Remove all SVN Folders from a Site

Svn has an annoying habit of adding thousands of folders and files into every level of your site when you put your code under...
[Read More]

How to turn off Vertical Tabs for a specific content type

I rarely do any heavy structural changes to node edit pages, so until today I hadn't ever had a reason to disable vertical tabs...
[Read More]
Drupal Tutorials
Drupal Blog RSS

Drupal Blog

Free, Unlimited, Private Git Hosting - The Holy Grail of Version Control

If I were paid minimum wage for the time I have spent investigating various git and svn hosting services (both free and paid) I...
[Read More]

Mac OSX + Drush = Time saved

The longer I have my mac the more things I find that drastically simplify my life as a Drupal developer. Now for fairness sake I...
[Read More]

New to Drupal? A word from the wise: Never say newbie!

I have nothing against new Drupal users on Drupal.org, even the most advanced Drupal developers were new to Drupal at one point...
[Read More]
Drupal Blog