March 19, 2010

Simple theme colour picker

Have you ever woken up in the middle of the night with a sudden urge to change the header colour of your Drupal site? Us neither, but one of our recent clients felt that they might and we wanted them to be as prepared as possible. At first we were going to go the color module route. Crafting some transparent png's and coding up some bits and bytes to make it all work with the theme we had already built. After investigating a little more though we found the color module to be a little overkill for this particular situation so we took a look at what we could do with the Zen theme and it's awesome theme-settings.php file.

We knew we could set a default header colour for our clients site in our zen sub-themes info file and we knew Drupal came with a pretty decent colour picker built in called farbtastic. Now we just had to figure out how to connect the two. Lo and behold, in the theme-settings.php file there is a commented out FAPI function for adding form elements to your sub themes configuration page.

Here's what we ended up doing.

We added this within the themename_settings function in our theme-settings.php file below the $settings = array_merge($defaults, $saved_settings); line and above the $form += zen_settings($saved_settings, $defaults). What this does is adds a farbtastic colour picker to the themes config page and handles the saving of the hex value.