Skip to content
Article

Follow Up – How to Bulk Anything in Drupal without a Module

Following up on my previous post about bulk deleting filtered aliases, I found that I needed to force all the taxonomy terms to check the “automatically generate url” checkbox.

Therefore, I modified the previous script to a more generic format so we can reuse the same method for just about any bulk update process in Drupal.  Supply your own callbacks for any two-step form:

[javascript]
(function($){
drupalBulkDo = function(options){
var defaults = {
selector: ‘#selector’
, context: ‘#content’
, secondStep: ‘#the-second-step’
, onSecondStep: function($theform, $theaction){
// now “submit” the form
$.post( $theform.attr(‘action’), $theform.serialize(), function(){
console.log( ‘performed ‘ + $theaction.attr(‘href’) );
}); // submit (post)
}
};
options = $.extend({}, defaults, options);

$items = $(options.selector, options.context);

// loop delete links
$items.each(function(i,o){
// jquery
var $theaction = $(o);

// “click” action link (i.e. “delete”)
$.get( $theaction.attr(‘href’), function(data){
// get form for second step
var $theform = $(data).find( options.secondStep );

// do stuff for second step (i.e. fire delete link)
options.onSecondStep($theform, $theaction, defaults.onSecondStep); // pass the original callback, in case we want to reuse it
}); // click (get)
}); // each

};//– fn drupalBulkDo
})(jQuery);
[/javascript]

Now, from /admin/structure/taxonomy/MYVOCABULARY, we can bulk update multiple terms to use the “autogenerate url” checkbox. Note how the callback not only updates the checkbox in the form, but also reuses the default callback to post the form.
[javascript highlight=”7,8,9″]
(function($){
// update url path alias settings to “automatic” for taxonomy
drupalBulkDo({
selector: ‘a[id$=-edit]’
, context: ‘#taxonomy’
, secondStep: ‘#taxonomy-form-term’
, onSecondStep: function($theform, $theaction, originalCb){
$theform.find(‘#edit-path-pathauto’).attr(‘checked’, ‘checked’);
originalCb($theform, $theaction);
}
});
})(jQuery);
[/javascript]

For reference, the original method for bulk deleting filtered aliases becomes:
[javascript]
(function($){
drupalBulkDo({
selector: ‘li.delete a’
, secondStep: ‘#path-admin-delete-confirm’
});
})(jQuery);
[/javascript]

The Atlantic BT Manifesto

The Ultimate Guide To Planning A Complex Web Project