Drupal - How to remove "Show row weights" option on File Upload field?

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);

You need to disable/alter settings.tableDrag function.

<?php
/**
 * Implements hook_js_alter().
 */
function MODULENAME_js_alter(&$javascript) {
  unset($javascript['misc/tabledrag.js']);
}
?>

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]);
      }
    }
  }

}

Tags:

Files

7