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:

Here's how you can replace a form field's help message with a message that follows the user's mouse when they hover over a form input. This can be useful in forms that are large and complicated.

CSS for the floating message:

#floatingMessage, #floatingMessage.hidden {
  display: none;
}

#floatingMessage.visible {
  display: block;
  position: absolute;
  background-color: white;
  border: 2px solid black;
  padding: 5px;
  z-index: 7;
  max-width: 200px;
}

jQuery:

<?php
$(document).ready(function(){

 
// check to see if floatingMessage exists in DOM
 
if ($(document).find('#floatingMessage').length==0) {
    $(
'body').append("<div id='floatingMessage'></div>");
  }

  $(
'form#node-form div.form-item').each(function(){
   
result = $(this).find('div.description').length;
    if (
result) {
     
// hide description
     
$('div.description:first', this).hide();
           
      $(
this).hover(
        function(){

         
// get message html
         
description = $('div.description:first', this).html();

         
// set message html
         
$('#floatingMessage').html(description);

         
// set message position                   
         
$(this).mousemove(function(e){
            $(
'#floatingMessage').css({
             
top: e.pageY+15 + 'px',
             
left: e.pageX+15 + 'px',
            });   
          });

         
// show message
         
$('#floatingMessage').removeClass('hidden').addClass('visible');
               
        },
        function(){
                   
         
// hide message
         
$('#floatingMessage').removeClass('visible').addClass('hidden').html('');
                   
        }

      );
    }
  });
});
?>

Screenshot:

Eric.London's picture

Normally when there is a form input error, Drupal highlights the form input by adding a red border and changing the text color. Unfortunately, nothing happens for checkboxes and radio buttons. Here's a little jQuery to add the error class to the label directly above a set of checkboxes or radios when an error occurs.

<?php
$('form#node-form div.form-item').each(function(){
  var
result = 0;
 
result += $(this).find('div.form-radios input.form-radio.error').length;
 
result += $(this).find('div.form-checkboxes input.form-checkbox.error').length;
  if (
result) $(this).find('label:first').removeClass('error').addClass('error');
});
?>

Eric.London's picture

To improve usability, I wanted to place the user's cursor on the first form input of each page when the document loads. Here's the JQuery I used...

<?php
$(document).ready(function(){
  $(
"input").not("[@type='hidden']")[0].focus();
});
?>

Syndicate content