Drupal 7 CCK – Creating ‘Label Bulk Operations’

I have a new D7 site in the works and I found that I wanted the majority of my content type field labels to display: inline as opposed to the default: ‘above’.
Several of my content types have dozens and dozens of fields to I wanted a quick way to automate changing all the selectors at once.

I suppose ‘repetition and redundancy are the Aunt and uncle of invention’.

Here’s what I came up with:
image of the new fieldset and buttons

Currently two buttons in a fieldset are prepended to the cck field display management table.

  • Above to Inline
  • Inline to Above

Each one calls a script that accepts two vars:

  • fromVar
  • toVar

The buttons themselves have a .click function attached to them calling the script.
Usage is self-explanatory: Click ‘Above to Inline’ and all the labels currently set to ‘Above’ are changed to ‘Inline’.

The current script doesn’t address the hidden option, nor does it supply a ‘all’ option. But the script could be expanded.
It might be nice to make a little form with two select fields containing all the available options. But for now it does what I need and I can move on.

I’ve implemented the script using js_injector to be included on the following pages: “admin/structure/types/manage/*”

The script contains two Drupal.behaviors functions. One to add the buttons to the page on load, and another for updating the labels.

Here’s the script:

//Toggle text formats description
(function ($) {
  //CCK Manage Fields Label Bulk Operation
  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.cckManageFieldsLabelBulkOperation = {
    attach: function(context) {
      //run only if there is a context var
      if(typeof context !== 'undefined'){
        /*
            Check the type of the context var and type. Process if context = [object HTMLDocument]
            Note: when context == [object HTMLDocument] then this is page load. You may want some script to run only on page load. Some not and some all the time.
        */
        if(typeof context == 'object' && context.toString().indexOf('HTMLDocument')!=-1){
          //Page Load
 
          jQuery('table#field-display-overview').parent().prepend('<fieldset id="update_labels"><legend>Label Bulk Operations</legend></fieldset>');
          //create button
          jQuery('#update_labels').css('padding','0').append('<a href="" class="button above-to-inline">Above to Inline</a> ').css('padding','6px');
          //add click action to button
          jQuery('#update_labels .above-to-inline').click( function(){ Drupal.behaviors.updateLabels('above', 'inline') });
 
          //create button
          jQuery('#update_labels').append('<a href="" class="button inline-to-above">Inline to Above</a> ').css('padding','6px');
          //add click action to button
          jQuery('#update_labels .inline-to-above').click( function() {Drupal.behaviors.updateLabels('inline', 'above') });
 
 
        }else{
          //alert('Context; typeof:'+typeof context+' | Var: '+context+' | toString: '+context.toString()+' | toSource: '+context.toSource());
        }//end if page load or other
        //run these all on any context event
 
      }
    }
  };
})(jQuery);
Leave a Comment