I have created a file field with multiple uploads allowed. Is there any way(except CSS) I can remove "Show row weights" link from file upload field? Or is there any way to remove weight drop down entirely from file field?
6 Answers
You need to disable/alter settings.tableDrag
function.
<?php
/**
* Implements hook_js_alter().
*/
function MODULENAME_js_alter(&$javascript) {
unset($javascript['misc/tabledrag.js']);
}
?>
-
2This removes the "Show row weights" link but when enabled it displays the Weight column. It's a good answer but a complete solution would include removal of the Weight column.– John81Jul 11, 2013 at 13:51
-
2This will remove the ability to reorder the rows entirely, not just remove the link at the top of the page. Adding this code will break the ability to order items. USE AT YOUR OWN RISK. Sep 1, 2016 at 1:52
The first part of your question is regarding removal of link "Show row weights" (or "Hide row weights" if dragging functionality is disabled) in a form where table rows are draggable, so I'm going to answer that.
"Show row weights" string appears in Drupal.tableDrag.prototype.showColumns()
method and "Hide row weights" appers in Drupal.tableDrag.prototype.hideColumns()
method that is included in tabledrag.js
file (see head in Drupal 7.x branch) so you need to override these methods in order to replace or remove these strings without losing row dragging functionality.
Place this code in a .js file that is loaded along with the form that uses tabledrag.js:
(function ($) {
// Copy hideColumns() method
var hideColumns = Drupal.tableDrag.prototype.hideColumns;
Drupal.tableDrag.prototype.hideColumns = function() {
// Call the original hideColumns() method
hideColumns.call(this);
// Remove the 'Show row weights' string
$('.tabledrag-toggle-weight').text('');
}
// Copy showColumns() method
var showColumns = Drupal.tableDrag.prototype.showColumns;
Drupal.tableDrag.prototype.showColumns = function () {
// Call the original showColumns() method
showColumns.call(this);
// Remove the 'Hide row weights' string
$('.tabledrag-toggle-weight').text('');
}
})(jQuery);
-
-
@Fawwad, the only side effect of the provided code is that toggle element text is hidden. Default
hideColumns()
andshowColumns()
methods are still invoked.– maijsMay 7, 2015 at 20:01 -
1It works but it's much easier to simply add to your css: .tabledrag-toggle-weight-wrapper, .tabledrag-toggle-weight, .tableresponsive-toggle-columns, .tableresponsive-toggle { height: 0 !important; font-size: 0 !important; color: transparent !important; display: none !important; } Yes, it seems a bit overkill but, depending on various themes, some might not be enough.– GáborMay 30, 2018 at 22:03
Here is a solution for Drupal 8 using hook_preprocess_field_multiple_value_form()
First find the field you want to remove re-ordering ability from:
/**
* Implements hook_preprocess_field_multiple_value_form().
*/
function MY_MODULE_preprocess_field_multiple_value_form(&$variables) {
if($variables['element']['#field_name'] == 'MY_FIELD_NAME') {
MY_MODULE_remove_table_reordering($variables['table']);
}
}
And then call a helper function to actually remove the tables re-ordering functionality:
/**
* Removes the ability for a table to be re-ordered.
*/
function MY_MODULE_remove_table_reordering(&$table) {
unset($table['#tabledrag']);
// Remove re-order header column
foreach($table['#header'] as $header_i => $header) {
if(is_object($header) && (string) $header == 'Order') {
unset($table['#header'][$header_i]);
}
}
// Loop table rows
foreach($table['#rows'] as &$row) {
// Remove draggable class from tr
$i = array_search('draggable', $row['class']);
if($i !== FALSE) {
unset($row['class'][$i]);
}
// Loop columns in row
foreach($row['data'] as $col_i => &$col) {
if(empty($col['class'])) {
continue;
}
// Remove td with drag handle
$i = array_search('field-multiple-drag', $col['class']);
if($i !== FALSE) {
unset($row['data'][$col_i]);
}
// Remove td with re-ordering select
$i = array_search('delta-order', $col['class']);
if($i !== FALSE) {
unset($row['data'][$col_i]);
}
}
}
}
You can also try a theme-override, as in https://api.drupal.org/api/drupal/modules!field!field.form.inc/function/theme_field_multiple_value_form/7
Paste the function to your theme's template.php, rename 'theme_' in the function name to your theme's name and clear the cache.
Find the line saying
drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
and comment it out.
I'm not sure about disabling for a single field, but I found myself wanting to disable the row dragging for an entire form.
monymirza suggested an answer to disable it entirely. However in D7, that approach is not reliable, because in some forms drupal_add_tabledrag() will be called after your hook_js_alter(). This is typical Drupal - everything is theoretically customizable, but in practice it's error prone and sensitive.
In the end I used the following hook_form_alter() to disable table drag for just one specific form:
<?php
/**
* Implements hook_form_alter()
*/
function sunflower_custom_form_alter(&$form, &$form_state, $form_id) {
if ($form_id == 'field_ui_field_overview_form' && $form['#bundle'] == 'sunflower_habitat') {
// The habitat form is wizardized based on row weights. This is a sensitive thing, and drupal's row weight javascript is not at all sensitive, completely changing all values when it feels like it. So we don't want that "feature" on this form.
// Disable it, if already enabled.
$javascript = &drupal_static('drupal_add_js', array());
unset($javascript['misc/tabledrag.js']);
// Prevent it from being enabled later.
$tabledrag_added = &drupal_static('drupal_add_tabledrag');
$tabledrag_added = TRUE;
}
}
?>
Adding on to moneymirza's answer to remove the weights and add a qualifyer, since we don't want to disable tabledrag.js
from the entire site.
<?php
/**
* Implements hook_js_alter().
*/
function MODULENAME_js_alter(&$javascript) {
// Use a more specific page condition as desired.
if(arg(0) == "node" && arg(2) == "edit") {
unset($javascript['misc/tabledrag.js']);
drupal_add_js(drupal_get_path('module', 'MODULENAME') . '/MODULENAME.js');
}
}
Then make a MODULENAME.js file:
(function ($) {
Drupal.behaviors.NoTabledragWeights = {
attach: function (context, settings) {
// Again, get more specific if desired.
$('td:nth-child(2),th:nth-child(2)').hide();
}
};
}(jQuery));
hook_form_alter