Screencap of tabledrag

Drupal developers tend to treat overriding theme functions as a last resort. However, it's sometimes necessary in order to eliminate default functionality provided by core.

One instance we recently found where the default theming was not to our client's expectations was the "unlimited" field widget (the widget is the form element on the edit screen, while a field formatter themes the output on the view screen). Unlimited widgets display with tabledrag re-ordering, without any option to disable it.

If you'd like to disable tabledrag re-ordering for unlimited field widgets, check out the solution below. For the sake of demonstration, I'm using a module named 'multiwidgetremove_tabledrag'.

Alter the Theme Registry

The first step is to change the theming function for multiplevalueform widgets:

Override the theme function

All multi-value field widgets use themefieldmultiplevalueform(), unless they're overridden elsewhere. We're going to override the default theme function using a new function named in theme registry alter:

This function just adds a condition that does new stuff for preprocessed elements, while leaving the Drupal defaults intact. The first if-statement looks for the first element to have a "nodrag" property. If it does, the code inside the if-statement builds the form element, albeit without the weights and tabledragging of the defaults.

Modify the Elements you want

The last step is to add an array row with the key "#nodrag" to the desired elements. You can use any logic you desire to do this, and even accomplish it at the widget level (if you're defining your own widget). If you just wish to modify an otherwise-default widget, hookfieldwidgetformalter() is a good choice:

As you can see, the example removes tabledrag from a field named 'fieldlongtest' specifically. This is the only part you'd have to edit if you were building this module yourself.

Wrapping Up

This solution took quite a bit of time and effort to finally pull off. I sincerely hope this post saves you some time if you have a similar problem in Drupal 7. Have a great day!

free 10 step drupal security audit guide