Updating select lists in bulk – w/ jquery, drupal

So I wrote this script 1 year and 1/2 ago about bulk label operations (Drupal 7 CCK – Creating ‘Label Bulk Operations’). Well the updateLabels() function from that code came in handy today.
I needed to update dozens of CCK formats from various defaults to editable. If you’ve used editable fields you know it can come in handy but if you have a lot of fields you’ll be in a carpel tunnel conundrum if you try to change then all.
So I pulled up the updateLabels() function opened the Firebug console and set all the various defaults to editable. And then back again.

I also used this method to quickly set all the render-able entity references this way and that and the taxonomy reference to plain text.
Pretty handy.

Here’s the code I used to set them. The code to change them back is commented out:

Drupal.behaviors.updateLabels = function(fromVal,toVal){
  if(typeof fromVal !== 'undefined' && typeof toVal !== 'undefined' ){
    //Set all 'above' labels to 'inline'
    jQuery(".form-select option:selected").each(
      function(){
        if(jQuery(this).val() == fromVal){
          jQuery(this).parent().children("option[value='"+toVal+"']").attr('selected', 'selected');
        }//end if
      }//end jquery each
    );
  }//end if typeof
}//end function updateLabels

/*
Drupal.behaviors.updateLabels('editable','text_default');
Drupal.behaviors.updateLabels('editable','text_default');
Drupal.behaviors.updateLabels('editable','viewfield_default');
Drupal.behaviors.updateLabels('editable','entityreference_entity_view');
Drupal.behaviors.updateLabels('editable','taxonomy_term_reference_plain');
Drupal.behaviors.updateLabels('editable','number_integer');
Drupal.behaviors.updateLabels('editable','list_default');
Drupal.behaviors.updateLabels('editable','field_collection_view');
Drupal.behaviors.updateLabels('editable','number_decimal');
*/

Drupal.behaviors.updateLabels('text_default','editable');
Drupal.behaviors.updateLabels('text_default','editable');
Drupal.behaviors.updateLabels('viewfield_default','editable');
Drupal.behaviors.updateLabels('entityreference_entity_view','editable');
Drupal.behaviors.updateLabels('taxonomy_term_reference_plain','editable');
Drupal.behaviors.updateLabels('number_integer','editable');
Drupal.behaviors.updateLabels('list_default','editable');
Drupal.behaviors.updateLabels('field_collection_view','editable');
Drupal.behaviors.updateLabels('number_decimal','editable');

Enjoy.

Leave a Comment