background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

I recently added an admin settings page to allow the user to select colors for node title labels. I quick Google search and I discovered this promising jQuery color picker library. Here's how I embedded it in a form.

First I unpacked the colorpicker.zip file and moved it into my theme folder. Next, I added a form callback function in my module.

<?php
// defining my form callback function
function _MYFORM() {
 
 
$form = array();

 
// include css & javascript for color picker
 
drupal_add_css(path_to_theme() . '/colorpicker/css/colorpicker.css');
 
drupal_add_js(path_to_theme() . '/colorpicker/js/colorpicker.js');

 
// define field ID
 
$formElementID = 'MYFORMELEMENT';

 
// create a javascript-friendly ID
 
$jsID = 'edit-' . str_replace('_','-',$formElementID);

 
// adding textfield input for color picker widget
 
$form[$formElementID] = array(
   
'#type' => 'textfield',
   
'#title' => t('MY FIELD TITLE'),
   
'#size' => 6,
   
'#maxlength' => 6,
   
'#suffix' => "
      <script type='text/javascript'>
        $(document).ready(function(){
          colorPickerAttach('#
$jsID');
        });
      </script>"
 
);

 
// add a javascript function to attach the color picker to a form element
 
$js = "
    function colorPickerAttach(which) {
      $(which).ColorPicker({
        onSubmit: function(hsb, hex, rgb) {
          $(which).val(hex);
        },
        onBeforeShow: function () {
          $(this).ColorPickerSetColor(this.value);
        }
      })
      .bind('keyup', function(){
        $(this).ColorPickerSetColor(this.value);
      });
    }
  "
;
 
drupal_add_js($js, 'inline');

  return
$form;

}
?>

Now, my form has a text input that uses a fancy color picker to choose the hex value. More documentation on the color picker can he found here.

Here's a screenshot of the color picker in action:

Eric.London's picture

At some point your design may call for a horizontal user login block. Luckily this can be accomplished with just a few lines of CSS. I added the following CSS to my theme:

.block #user-login-form { text-align: left; }
.block #user-login-form #edit-name-wrapper { float: left; margin-right: 10px; }
.block #user-login-form #edit-pass-wrapper { float: left; margin-right: 10px; }
.block #user-login-form #edit-submit { float: left; margin-top: 25px; }
.block #user-login-form .item-list { clear: both; text-align: right; }

Before:

After:

Eric.London's picture

I just created a new image to replace the generic submit button appearance, when I remembered the submit buttons are type submit, not type image. I decided to use some quick CSS instead. The trick is adding a background image to the submit button, and then *hiding* the button text by adding padding-top CSS that is greater than the height of the image, and adding "overflow: hidden" to ensure the text does not get pushed outside the button...

form#MYFORMID #edit-submit {
  height: 22px;
  width: 73px;
  background: url('images/button_submit.jpg') no-repeat;
  border: none;
  overfow: hidden;
  padding-top: 25px;
}

Eric.London's picture

My design called for each table's first column to be a different style. I was going to use the "first-child" CSS selector, but of course, it does not work in IE6. Here's the jQuery I added to my theme to set the class name for the first td in each table tr. NOTE: As you can see, I only wanted to work on views tables.

<?php
$(document).ready(function(){
  $(
'div.view div.view-content table tr').each(function(i) {
    $(
'td:first', this).addClass('firstColumn');
  });
});
?>

Syndicate content